自学内容网 自学内容网

Linux shellcheck工具

安装工具

        通过linux yum源下载,可能因为yum源的问题找不到软件包,或者下载的软件包版本太旧。

 

ShellCheck的源代码托管在GitHub上(推荐下载方式):

GitHub - koalaman/shellcheck: ShellCheck, a static analysis tool for shell scripts

 

 

        对下载的可执行程序压缩包进行解压,将shellcheck可执行命令加入环境变量:拷贝至/bin/下,以方便在任何位置都可以通过tab键补齐shellcheck并使用。

使用shellcheck

#!/bin/bash  

# 设置编译器和编译选项  
COMPILER=arm-linux-gnueabihf-g++ #CPP编译器
COMPILE_FLAGS_JSONCPP="-L /usr/local/lib /usr/local/lib/libjsoncpp.a"  #链接 jsoncp库
COMPILE_FLAGS_CGICC="-I /usr/local/cgicc-3.2.20/include -L /usr/local/cgicc-3.2.20/lib -lcgicc"  #链接 cgicc库
COMPILE_FLAGS_SQLITE3="-I /usr/local/sqlite3/include/ -L /usr/local/sqlite3/lib/ -lsqlite3"  #链接 sqlite3库
COMPILE_FLAGS_OPENSSL="-I /usr/local/arm-openssl-3.0/include/ -L /usr/local/arm-openssl-3.0/lib64/ -lcrypto"  #链接 crypto库

UPHOLD_TOOLS=uphold_tools
cd $UPHOLD_TOOLS
$COMPILER -o $UPHOLD_TOOLS.cgi $UPHOLD_TOOLS.cpp $COMPILE_FLAGS_CGICC $COMPILE_FLAGS_JSONCPP $COMPILE_FLAGS_OPENSSL
if [ $? -ne 0 ]; then
echo "Error compiling $UPHOLD_TOOLS"
exit 1  
fi
SYSTEM_REBOOT=system_reboot
$COMPILER -o $SYSTEM_REBOOT.cgi $SYSTEM_REBOOT.cpp $COMPILE_FLAGS_CGICC $COMPILE_FLAGS_JSONCPP
if [ $? -ne 0 ]; then
echo "Error compiling $SYSTEM_REBOOT"
exit 1  
fi
cd ..
#拷贝.cgi和.sh到radio-gateway中
find $UPHOLD_TOOLS -type f \( -name "*.cgi" -o -name "*.sh" \) -exec cp {} ../radio-gateway/cgi-bin/$UPHOLD_TOOLS \;
if [ $? -ne 0 ]; then  
echo "Error copy $UPHOLD_TOOLS"
exit 1
fi

#拷贝web源代码到radio-gateway中
cp -r ../web ../radio-gateway/
rm -rf ../radio-gateway/web/index.html
cp ../web/index.html ../radio-gateway/

echo "Compilation and copy successful."

优化建议:

[root@localdomain home]# shellcheck compile.sh 

In compile.sh line 7:
COMPILE_FLAGS_SQLITE3="-I /usr/local/sqlite3/include/ -L /usr/local/sqlite3/lib/ -lsqlite3"  #链接 sqlite3库
^-------------------^ SC2034 (warning): COMPILE_FLAGS_SQLITE3 appears unused. Verify use (or export if used externally).


In compile.sh line 11:
cd $UPHOLD_TOOLS
^--------------^ SC2164 (warning): Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Did you mean: 
cd $UPHOLD_TOOLS || exit


In compile.sh line 12:
$COMPILER -o $UPHOLD_TOOLS.cgi $UPHOLD_TOOLS.cpp $COMPILE_FLAGS_CGICC $COMPILE_FLAGS_JSONCPP $COMPILE_FLAGS_OPENSSL
                                                 ^------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.
                                                                      ^--------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.
                                                                                             ^--------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
$COMPILER -o $UPHOLD_TOOLS.cgi $UPHOLD_TOOLS.cpp "$COMPILE_FLAGS_CGICC" "$COMPILE_FLAGS_JSONCPP" "$COMPILE_FLAGS_OPENSSL"


In compile.sh line 13:
if [ $? -ne 0 ]; then
     ^-- SC2181 (style): Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.


In compile.sh line 18:
$COMPILER -o $SYSTEM_REBOOT.cgi $SYSTEM_REBOOT.cpp $COMPILE_FLAGS_CGICC $COMPILE_FLAGS_JSONCPP
                                                   ^------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.
                                                                        ^--------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
$COMPILER -o $SYSTEM_REBOOT.cgi $SYSTEM_REBOOT.cpp "$COMPILE_FLAGS_CGICC" "$COMPILE_FLAGS_JSONCPP"


In compile.sh line 19:
if [ $? -ne 0 ]; then
     ^-- SC2181 (style): Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.


In compile.sh line 23:
cd ..
^---^ SC2103 (info): Use a ( subshell ) to avoid having to cd back.


In compile.sh line 26:
if [ $? -ne 0 ]; then  
     ^-- SC2181 (style): Check exit code directly with e.g. 'if ! mycmd;', not indirectly with $?.

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- COMPILE_FLAGS_SQLITE3 appears unu...
  https://www.shellcheck.net/wiki/SC2164 -- Use 'cd ... || exit' or 'cd ... |...
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
[root@localdomain home]# 

原文地址:https://blog.csdn.net/hao745580264_dawei/article/details/142879597

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!