Skip to content

Commit 4cd7628

Browse files
authored
Merge pull request #3011 from sertonix/busybox-3003
Fix SC3003, SC3036 and SC3045 for busybox shell
2 parents cd6fdee + 6d2f3d8 commit 4cd7628

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/ShellCheck/Checks/ShellSupport.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ prop_checkBashisms118 = verify checkBashisms "#!/bin/busybox sh\nxyz=1\n${!x*}"
212212
prop_checkBashisms119 = verify checkBashisms "#!/bin/busybox sh\nx='test'\n${x^^[t]}" -- SC3059
213213
prop_checkBashisms120 = verify checkBashisms "#!/bin/sh\n[ x == y ]"
214214
prop_checkBashisms121 = verifyNot checkBashisms "#!/bin/sh\n# shellcheck shell=busybox\n[ x == y ]"
215+
prop_checkBashisms122 = verify checkBashisms "#!/bin/dash\n$'a'"
216+
prop_checkBashisms123 = verifyNot checkBashisms "#!/bin/busybox sh\n$'a'"
217+
prop_checkBashisms124 = verify checkBashisms "#!/bin/dash\ntype -p test"
218+
prop_checkBashisms125 = verifyNot checkBashisms "#!/bin/busybox sh\ntype -p test"
219+
prop_checkBashisms126 = verifyNot checkBashisms "#!/bin/busybox sh\nread -p foo -r bar"
220+
prop_checkBashisms127 = verifyNot checkBashisms "#!/bin/busybox sh\necho -ne foo"
215221
checkBashisms = ForShell [Sh, Dash, BusyboxSh] $ \t -> do
216222
params <- ask
217223
kludge params t
@@ -229,7 +235,8 @@ checkBashisms = ForShell [Sh, Dash, BusyboxSh] $ \t -> do
229235

230236
bashism (T_ProcSub id _ _) = warnMsg id 3001 "process substitution is"
231237
bashism (T_Extglob id _ _) = warnMsg id 3002 "extglob is"
232-
bashism (T_DollarSingleQuoted id _) = warnMsg id 3003 "$'..' is"
238+
bashism (T_DollarSingleQuoted id _) =
239+
unless isBusyboxSh $ warnMsg id 3003 "$'..' is"
233240
bashism (T_DollarDoubleQuoted id _) = warnMsg id 3004 "$\"..\" is"
234241
bashism (T_ForArithmetic id _ _ _ _) = warnMsg id 3005 "arithmetic for loops are"
235242
bashism (T_Arithmetic id _) = warnMsg id 3006 "standalone ((..)) is"
@@ -321,7 +328,11 @@ checkBashisms = ForShell [Sh, Dash, BusyboxSh] $ \t -> do
321328

322329
bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
323330
| t `isCommand` "echo" && argString `matches` flagRegex =
324-
if isDash
331+
if isBusyboxSh
332+
then
333+
when (not (argString `matches` busyboxFlagRegex)) $
334+
warnMsg (getId arg) 3036 "echo flags besides -n and -e"
335+
else if isDash
325336
then
326337
when (argString /= "-n") $
327338
warnMsg (getId arg) 3036 "echo flags besides -n"
@@ -330,6 +341,7 @@ checkBashisms = ForShell [Sh, Dash, BusyboxSh] $ \t -> do
330341
where
331342
argString = concat $ oversimplify arg
332343
flagRegex = mkRegex "^-[eEsn]+$"
344+
busyboxFlagRegex = mkRegex "^-[en]+$"
333345

334346
bashism t@(T_SimpleCommand _ _ (cmd:arg:_))
335347
| getLiteralString cmd == Just "exec" && "-" `isPrefixOf` concat (oversimplify arg) =
@@ -443,10 +455,10 @@ checkBashisms = ForShell [Sh, Dash, BusyboxSh] $ \t -> do
443455
("hash", Just $ if isDash then ["r", "v"] else ["r"]),
444456
("jobs", Just ["l", "p"]),
445457
("printf", Just []),
446-
("read", Just $ if isDash then ["r", "p"] else ["r"]),
458+
("read", Just $ if isDash || isBusyboxSh then ["r", "p"] else ["r"]),
447459
("readonly", Just ["p"]),
448460
("trap", Just []),
449-
("type", Just []),
461+
("type", Just $ if isBusyboxSh then ["p"] else []),
450462
("ulimit", if isDash then Nothing else Just ["f"]),
451463
("umask", Just ["S"]),
452464
("unset", Just ["f", "v"]),

0 commit comments

Comments
 (0)