鱼C论坛

 找回密码
 立即注册
查看: 2227|回复: 0

[好文转载] 给大家介绍一个shell代码检查工具

[复制链接]
发表于 2016-8-27 17:19:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

首先关于shellcheck的安装,有三种方法:
1、直接copy相应shellcheck的可执行文件到bin目录下
2、更换带有shellcheck软件包的源(/etc/apt),然后apt-get update,安装即可
3、进行编译安装,因为shellcheck是用Cabal管理和建立的,所以使用apt-get 安装cabal-install进行编译安装

而关于shellcheck的具体作用:

#### Quoting(引号)

ShellCheck can recognize several types of incorrect quoting:


  1.     echo $1                           # Unquoted variables
  2.     find . -name *.ogg                # Unquoted find/grep patterns
  3.     rm "~/my file.txt"                # Quoted tilde expansion
  4.     v='--verbose="true"'; cmd $v      # Literal quotes in variables
  5.     for f in "*.ogg"                  # Incorrectly quoted 'for' loops
  6.     touch $@                          # Unquoted $@
  7.     echo 'Don't forget to restart!'   # Singlequote closed by apostrophe
  8.     echo 'Don\'t try this at home'    # Attempting to escape ' in ''
  9.     echo 'Path is $PATH'              # Variables in single quotes
  10.     trap "echo Took ${SECONDS}s" 0    # Prematurely expanded trap
复制代码



#### Conditionals

ShellCheck can recognize many types of incorrect test statements.

  1.     [[ n != 0 ]]                      # Constant test expressions
  2.     [[ -e *.mpg ]]                    # Existence checks of globs
  3.     [[ $foo==0 ]]                     # Always true due to missing spaces
  4.     [[ -n "$foo " ]]                  # Always true due to literals
  5.     [[ $foo =~ "fo+" ]]               # Quoted regex in =~
  6.     [ foo =~ re ]                     # Unsupported [ ] operators
  7.     [ $1 -eq "shellcheck" ]           # Numerical comparison of strings
  8.     [ $n && $m ]                      # && in [ .. ]
  9.     [ grep -q foo file ]              # Command without $(..)

复制代码


#### Frequently misused commands

ShellCheck can recognize instances where commands are used incorrectly:

  1.     grep '*foo*' file                 # Globs in regex contexts
  2.     find . -exec foo {} && bar {} \;  # Prematurely terminated find -exec
  3.     sudo echo 'Var=42' > /etc/profile # Redirecting sudo
  4.     time --format=%s sleep 10         # Passing time(1) flags to time builtin
  5.     while read h; do ssh "$h" uptime  # Commands eating while loop input
  6.     alias archive='mv $1 /backup'     # Defining aliases with arguments
  7.     tr -cd '[a-zA-Z0-9]'              # [] around ranges in tr
  8.     exec foo; echo "Done!"            # Misused 'exec'
  9.     find -name \*.bak -o -name \*~ -delete  # Implicit precedence in find
  10.     f() { whoami; }; sudo f           # External use of internal functions
复制代码




#### Common beginner's mistakes

ShellCheck recognizes many common beginner's syntax errors:

  1.     var = 42                          # Spaces around = in assignments
  2.     $foo=42                           # $ in assignments
  3.     for $var in *; do ...             # $ in for loop variables
  4.     var$n="Hello"                     # Wrong indirect assignment
  5.     echo ${var$n}                     # Wrong indirect reference
  6.     var=(1, 2, 3)                     # Comma separated arrays
  7.     echo "Argument 10 is $10"         # Positional parameter misreference
  8.     if $(myfunction); then ..; fi     # Wrapping commands in $()
  9.     else if othercondition; then ..   # Using 'else if'

复制代码



#### Style

ShellCheck can make suggestions to improve style:

  1.     [[ -z $(find /tmp | grep mpg) ]]  # Use grep -q instead
  2.     a >> log; b >> log; c >> log      # Use a redirection block instead
  3.     echo "The time is `date`"         # Use $() instead
  4.     cd dir; process *; cd ..;         # Use subshells instead
  5.     echo $[1+2]                       # Use standard $((..)) instead of old $[]
  6.     echo $(($RANDOM % 6))             # Don't use $ on variables in $((..))
  7.     echo "$(date)"                    # Useless use of echo
  8.     cat file | grep foo               # Useless use of cat

复制代码



#### Data and typing errors

ShellCheck can recognize issues related to data and typing:


  1.     args="$@"                         # Assigning arrays to strings
  2.     files=(foo bar); echo "$files"    # Referencing arrays as strings
  3.     printf "%s\n" "Arguments: $@."    # Concatenating strings and arrays.
  4.     [[ $# > 2 ]]                      # Comparing numbers as strings
  5.     var=World; echo "Hello " var      # Unused lowercase variables
  6.     echo "Hello $name"                # Unassigned lowercase variables
  7.     cmd | read bar; echo $bar         # Assignments in subshells
复制代码




#### Robustness

ShellCheck can make suggestions for improving the robustness of a script:

  1.     rm -rf "$STEAMROOT/"*            # Catastrophic rm
  2.     touch ./-l; ls *                 # Globs that could become options
  3.     find . -exec sh -c 'a && b {}' \; # Find -exec shell injection
  4.     printf "Hello $name"             # Variables in printf format
  5.     for f in $(ls *.txt); do         # Iterating over ls output
  6.     export MYVAR=$(cmd)              # Masked exit codes
复制代码




#### Portability

ShellCheck will warn when using features not supported by the shebang. For example, if you set the shebang to `#!/bin/sh`, ShellCheck will warn about portability issues similar to `checkbashisms`:


  1.     echo {1..$n}                     # Works in ksh, but not bash/dash/sh
  2.     echo {1..10}                     # Works in ksh and bash, but not dash/sh
  3.     echo -n 42                       # Works in ksh, bash and dash, undefined in sh
  4.     trap 'exit 42' sigint            # Unportable signal spec
  5.     cmd &> file                      # Unportable redirection operator
  6.     read foo < /dev/tcp/host/22      # Unportable intercepted files
  7.     foo-bar() { ..; }                # Undefined/unsupported function name
  8.     [ $UID = 0 ]                     # Variable undefined in dash/sh
  9.     local var=value                  # local is undefined in sh
复制代码




#### Miscellaneous

ShellCheck recognizes a menagerie of other issues:

  1.     PS1='\e[0;32m\$\e[0m '            # PS1 colors not in \[..\]
  2.     PATH="$PATH:~/bin"                # Literal tilde in $PATH
  3.     echo {1..$n}                     # Works in ksh, but not bash/dash/sh
  4.     echo {1..10}                     # Works in ksh and bash, but not dash/sh
  5.     echo -n 42                       # Works in ksh, bash and dash, undefined in sh
  6.     trap 'exit 42' sigint            # Unportable signal spec
  7.     cmd &> file                      # Unportable redirection operator
  8.     read foo < /dev/tcp/host/22      # Unportable intercepted files
  9.     foo-bar() { ..; }                # Undefined/unsupported function name
  10.     [ $UID = 0 ]                     # Variable undefined in dash/sh
  11.     local var=value                  # local is undefined in sh
复制代码




#### Miscellaneous



  1.     echo {1..$n}                     # Works in ksh, but not bash/dash/sh
  2.     echo {1..10}                     # Works in ksh and bash, but not dash/sh
  3.     echo -n 42                       # Works in ksh, bash and dash, undefined in sh
  4.     trap 'exit 42' sigint            # Unportable signal spec
  5.     cmd &> file                      # Unportable redirection operator
  6.     read foo < /dev/tcp/host/22      # Unportable intercepted files
  7.     foo-bar() { ..; }                # Undefined/unsupported function name
  8.     [ $UID = 0 ]                     # Variable undefined in dash/sh
  9.     local var=value                  # local is undefined in sh
复制代码




#### Miscellaneous

ShellCheck recognizes a menagerie of other issues:

  1.     PS1='\e[0;32m\$\e[0m '            # PS1 colors not in \[..\]
  2.     PATH="$PATH:~/bin"                # Literal tilde in $PATH
  3.     rm “file”                         # Unicode quotes
  4.     echo "Hello world"                # Carriage return / DOS line endings
  5.     var=42 echo $var                  # Expansion of inlined environment
  6.     #!/bin/bash -x -e                 # Common shebang errors
  7.     echo $((n/180*100))               # Unnecessary loss of precision
  8.     ls *[:digit:].txt                 # Bad character class globs
  9.     sed 's/foo/bar/' file > file       # Redirecting to input
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 17:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表