当前位置: 首页 > 工具软件 > ShellCheck > 使用案例 >

centos7中安装Shellcheck

钦景胜
2023-12-01

目录

 

一、安装shellcheck

二、使用shellcheck

三、使用shellcheck检测

四、官方说明网站


一、安装shellcheck


!!!由于发现网上各种复制粘贴,而且安装繁琐,特此写个博客,请转载着附上原网站。

#安装扩展源

yum -y install epel-release

#安装shellcheck

yum install ShellCheck

#提示安装成功如下

Installed:
  ShellCheck.x86_64 0:0.3.8-1.el7                                                                                                                                           

Dependency Installed:
  ghc-ShellCheck.x86_64 0:0.3.8-1.el7        ghc-array.x86_64 0:0.4.0.1-26.4.el7     ghc-base.x86_64 0:4.6.0.1-26.4.el7         ghc-bytestring.x86_64 0:0.10.0.2-26.4.el7  
  ghc-containers.x86_64 0:0.5.0.0-26.4.el7   ghc-deepseq.x86_64 0:1.3.0.1-26.4.el7   ghc-directory.x86_64 0:1.2.0.1-26.4.el7    ghc-filepath.x86_64 0:1.3.0.1-26.4.el7     
  ghc-json.x86_64 0:0.7-4.el7                ghc-mtl.x86_64 0:2.1.2-27.el7           ghc-old-locale.x86_64 0:1.0.0.5-26.4.el7   ghc-parsec.x86_64 0:3.1.3-31.el7           
  ghc-pretty.x86_64 0:1.1.1.0-26.4.el7       ghc-regex-base.x86_64 0:0.93.2-29.el7   ghc-regex-tdfa.x86_64 0:1.1.8-11.el7       ghc-syb.x86_64 0:0.4.0-35.el7              
  ghc-text.x86_64 0:0.11.3.1-2.el7           ghc-time.x86_64 0:1.4.0.1-26.4.el7      ghc-transformers.x86_64 0:0.3.0.0-34.el7   ghc-unix.x86_64 0:2.6.0.1-26.4.el7    

 


二、使用shellcheck


测试

vim test.txt

#!/bin/sh
for n in {1..$RANDOM}
do
  str=""
  if (( n % 3 == 0 ))
  then
    str="fizz"
  fi
  if [ $[n%5] == 0 ]
  then
    str="$strbuzz"
  fi
  if [[ ! $str ]]
  then
    str="$n"
  fi
  echo "$str"
done

 


三、使用shellcheck检测


[root@nacos ~]# shellcheck jia.sh 

In jia.sh line 2:
for n in {1..$RANDOM}
         ^-- SC2039: In POSIX sh, brace expansion is not supported.
             ^-- SC2039: In POSIX sh, RANDOM is not supported.


In jia.sh line 5:
  if (( n % 3 == 0 ))
     ^-- SC2039: In POSIX sh, standalone ((..)) is not supported.


In jia.sh line 9:
  if [ $[n%5] == 0 ]
       ^-- SC2039: In POSIX sh, $[..] in place of $((..)) is not supported.
       ^-- SC2007: Use $((..)) instead of deprecated $[..]
              ^-- SC2039: In POSIX sh, == is not supported.


In jia.sh line 11:
    str="$strbuzz"
         ^-- SC2154: strbuzz is referenced but not assigned.


In jia.sh line 13:
  if [[ ! $str ]]
     ^-- SC2039: In POSIX sh, [[ ]] is not supported.

此时说明shellcheck可以使用

 


四、官方说明网站


https://github.com/koalaman/shellcheck

https://github.com/koalaman/shellcheck/tree/v0.7.1#installing

 

 类似资料: