sh/Bash腳本靜態分析和lint工具:ShellCheck

jopen 9年前發布 | 25K 次閱讀 lint

sh/Bash腳本靜態分析和lint工具:ShellCheck。它不僅主要關注shell腳本典型初中級語法錯誤和shell輸出的神秘的錯誤信息或者奇怪行為的缺陷,也能報告一些高級導致后來故障的高級問題。
terminal.png

What does ShellCheck check?

Here is an incomplete list of things ShellCheck warns about and suggests improvements to:

Unquoted globs for find/grep
find . -name *.ogg
Constant test expressions
[[ n != 0 ]]
Assigning arrays to strings
args="$@"
Redirecting into source file
sed -e 's/foo/bar/g' file > file
Existence checks of globs
[[ -e *.mpg ]]
Globs in regex context
grep '*foo*' file
PS1 colors not in \[..\]
PS1='\e[0;32m\$\e[0m '
Prematurely terminated find -exec
find / -exec touch {} && echo {} \;
Literal quotes in arguments
verbose='--verbose="true"' cmd $verbose
Assignment in subshells
echo foo | read bar
echo $bar
Confusing time(1) for builtin
time --format=%s sleep 10
~ in quotes
rm "~/my file.txt"
Single, quoted 'for' argument
for f in "*.ogg" do rm $f; done
Arithmetic truncation
echo $((n/180*100))
Functions used externally
f() { rm file; }; sudo f
Unused variables
var=World; echo "Hello " var
Looping over ls output
for f in $(ls *.ogg) do ...; done
Arguments in aliases
alias archive='mv $1 /backup'
Referencing arrays as strings
files=(foo bar); echo "$files"
Unquoted command expansion
tar cf file$(date).tar dir
$ in for loop variables
for $var in foo bar baz; do echo "$var"; done
Unquoted $@
touch $@
Unicode quotes
rm file
Attempted redirection of stdout+stderr
cmd 2>&1 > /dev/null
Attempted indirect assignment
var$n="Hello"
Attempted indirect reference
echo ${var$n}
Variables in single quotes
echo 'Path is $PATH'
Comparing numbers with < or >
[[ $n > 0 ]]
Unsupported [ ] operators
[ foo =~ re ]
Using ~ in $PATH
PATH="$PATH:~/bin"
Quoted =~ regex
[[ $foo =~ "fo+" ]]
Opportunities for grep -q
[[ -z $(find /tmp | grep mpg) ]]
Tautology due to spacing
[[ $foo==0 ]]
Variable brace expansion (Bash)
echo {1..$n}
Commands eating loop input
while read host; do ssh "$host" uptime; done < file
Decimals arithmetics
echo $((3.14*r*r))
Comma separated arrays
var=(1, 2, 3)
Misused 'exec'
exec foo; echo "Done!"
Globs that could become options
touch ./-l; ls *
Common shebang errors
#!/bin/bash -x -e
Variables in printf format
printf "Hello $name"
Numerical comparison of strings
[[ $1 -eq "shellcheck" ]]
Prefix assignments in args
var=42 echo $var
Implicit precedence in find
find . -name '*.bak' -o -name '*~' -delete
Useless use of echo
echo $(date)
Repetitive redirections
cmd1 >> file; cmd2 >> file; cmd3 >> file;
Redirecting sudo
sudo echo 'alias sl=ls' >> /etc/profile
[] around ranges in tr
tr '[a-z]' '[A-Z]'
Misquoted traps
trap "echo \"Runtime: ${SECONDS}s\"" exit
&& in [ .. ]
[ $n && $m ]
Singlequote closed by apostrophe
echo 'Don't forget to run foo --update!'
Attempting to escape ' in ''
var='Don\'t try this at home'
Misused char class globs
ls *[:digit:].txt
Concatenating strings and arrays.
printf "%s\n" "Command line was $@."
Positional parameter misreference
echo "Argument 10 is $10"
cd-and-back antipattern
for d in *; do cd "$d"; cmd; cd ..; done
Missing semicolons in loops
for f in *.mp3 do true; done
$ in assignments
$foo=42
Aliases expanding early
alias whereami="echo $PWD"
Spaces in assignment
var = 42
Features shebang may not support
#!/bin/sh echo {1..10}
Spurious $()/``
cmd='echo foo'; `$cmd`;

項目主頁:http://www.baiduhome.net/lib/view/home/1419234081734

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!