Ubuntu 16.04
重击 4.3.48
shellcheck
当源文件代表脚本顶部的 $variables时,为什么会失败?
这是一个简单的脚本:
#!/bin/bash
. .sourcefile
echo "Today is ${day}."
这是我的源文件:
day="Monday"
以下是来自的回复shellcheck
:
me@myserver:~$ shellcheck start.sh
In start.sh line 5:
echo "Today is ${day}."
^-- SC2154: day is referenced but not assigned.
有没有办法让shellcheck
知道$variables在源文件中?
这是我为在 Ubuntu 16.04 上完成这项工作所做的工作
@Dash-o 解释了以下过程:
首先,在源文件声明上方的行中添加source 指令,如下所示:
# shellcheck source=/path/to/sourcefile
#!/bin/bash
# shellcheck source=./.sourcefile
. .sourcefile
echo "Today is ${day}."
接下来,在脚本之前shellcheck
使用-x选项执行,如下所示:
shellcheck -x start.sh
me@myserver:~$ shellcheck -x start.sh
当使用-x选项时,shellcheck
将遵循源指令下面直接声明的源文件。当我执行命令时,我收到以下错误:
me@myserver:~$ shellcheck -x start.sh
unrecognized option `-x'
Usage: shellcheck [OPTIONS...] FILES...
-e CODE1,CODE2.. --exclude=CODE1,CODE2.. exclude types of warnings
-f FORMAT --format=FORMAT output format
-s SHELLNAME --shell=SHELLNAME Specify dialect (bash,sh,ksh)
-V --version Print version information
@Bayou 提到我的操作系统需要更新版本的shellcheck
. 我检查了我的 Ubuntu 16.04 安装。我的服务器有shellcheck
0.3.7,这是 Ubuntu 16.04 必须提供的最新版本,所以我从shellcheck
开发人员那里获取了最新的二进制文件并安装了它。
me@myserver:~$ mkdir .work && cd .work
me@myserver:~/.work$ wget -q https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.x86_64.tar.xz
me@myserver:~/.work$ ls
shellcheck-stable.linux.x86_64.tar.xz
me@myserver:~/.work$ tar xvf shellcheck-stable.linux.x86_64.tar.xz
shellcheck-stable/README.txt
shellcheck-stable/LICENSE.txt
shellcheck-stable/shellcheck
me@myserver:~/.work$ sudo chown root: shellcheck-stable/shellcheck
me@myserver:~/.work$ sudo mv /usr/bin/shellcheck .
me@myserver:~/.work$ sudo mv shellcheck-stable/shellcheck /usr/bin/
me@myserver:~/.work$ cd ../
me@myserver:~$ rm -rf .work/
me@myserver:~$ shellcheck -V
ShellCheck - shell script analysis tool
version: 0.7.1
license: GNU General Public License, version 3
website: https://www.shellcheck.net
我运行了命令shellcheck -x start.sh
,我收到了零错误。
me@myserver:~$ shellcheck -x start.sh
然后我被迫shellcheck
给我一些错误。我添加cat $myVariable
到脚本的末尾。
me@myserver:~$ echo "cat \$myVariable" >> start.sh
me@myserver:~$ cat start.sh
#!/bin/bash
# shellcheck source=./.sourcefile
. .sourcefile
echo "Today is ${day}."
cat $myVariable
我测试了我的理论并shellcheck
遵循了我的源文件并给了我预期的错误。
me@myserver:~$ shellcheck -x start.sh
In start.sh line 7:
cat $myVariable
^---------^ SC2154: myVariable is referenced but not assigned.
^---------^ SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
cat "$myVariable"
For more information:
https://www.shellcheck.net/wiki/SC2154 -- myVariable is referenced but not ...
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
然后我在shellcheck
没有-x选项的情况下执行,令我惊讶的是,我收到了以下错误:
me@myserver:~$ shellcheck start.sh
In start.sh line 4:
. .sourcefile
^---------^ SC1091: Not following: ./.sourcefile was not specified as input (see shellcheck -x).
In start.sh line 6:
echo "Today is ${day}."
^----^ SC2154: day is referenced but not assigned.
In start.sh line 7:
cat $myVariable
^---------^ SC2154: myVariable is referenced but not assigned.
^---------^ SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
cat "$myVariable"
For more information:
https://www.shellcheck.net/wiki/SC2154 -- day is referenced but not assigned.
https://www.shellcheck.net/wiki/SC1091 -- Not following: ./.sourcefile was ...
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
所以shellcheck
更新到最新版本并且没有-x选项,也会给你一个错误,说明你在命令行上错过了-x选项。所以我决定在我的 start.sh中注释掉sourcefile 指令
me@myserver:~$ sed -i '3s/./#\ &/' start.sh
me@myserver:~$ cat start.sh
#!/bin/bash
# # shellcheck source=./.sourcefile
. .sourcefile
echo "Today is ${day}."
cat $myVariable
现在看看怎么shellcheck
想
me@myserver:~$ shellcheck -x start.sh
In start.sh line 7:
cat $myVariable
^---------^ SC2154: myVariable is referenced but not assigned.
^---------^ SC2086: Double quote to prevent globbing and word splitting.
Did you mean:
cat "$myVariable"
For more information:
https://www.shellcheck.net/wiki/SC2154 -- myVariable is referenced but not ...
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
哇哦?因此,我的简单脚本似乎确实不需要sourcefile 指令,或者 shellcheck 仍然看到sourcefile 指令被数字符号注释掉,因为数字符号已经启动了sourcefile 指令?因此,我完全删除了sourcefile 指令,并注释掉了最后一行引用了未在源文件中分配的myVariable变量。
me@myserver:~$ sed -i '3d' start.sh
me@myserver:~$ sed -i '$d' start.sh
me@myserver:~$ cat start.sh
#!/bin/bash
. .sourcefile
echo "Today is ${day}."
现在看看什么shellcheck
报告:
me@myserver:~$ shellcheck -x start.sh
-x选项没有错误。现在要检查在 Ubuntu 16.04 上具有最新版本的简单 shell 脚本的顶部是否不需要sourcefile 指令,我在没有-x选项的情况下执行。shellcheck
shellcheck
me@myserver:~$ shellcheck start.sh
In start.sh line 3:
. .sourcefile
^---------^ SC1091: Not following: .sourcefile was not specified as input (see shellcheck -x).
In start.sh line 5:
echo "Today is ${day}."
^----^ SC2154: day is referenced but not assigned.
For more information:
https://www.shellcheck.net/wiki/SC2154 -- day is referenced but not assigned.
https://www.shellcheck.net/wiki/SC1091 -- Not following: .sourcefile was no...
因此,简而言之,如果shellcheck
没有关注您的源文件,请 shellcheck
从开发人员的 github 更新 - ( https://github.com/koalaman/shellcheck ) 并通过使用-x选项 通知shellcheck
有一个源文件,如下所示:
shellcheck -x script.sh
我希望这对某人有所帮助,因为这个网站每天都对我有所帮助!