Ubuntu 16.04
Bash 4.3.48
shellcheck当源文件表示脚本顶部的$variables时,为什么会失败?
这是一个简单的脚本:
#!/bin/bash
. .sourcefile
echo "Today is ${day}."
Run Code Online (Sandbox Code Playgroud)
这是我的源文件:
day="Monday"
Run Code Online (Sandbox Code Playgroud)
以下是来自的回复shellcheck:
me@myserver:~$ shellcheck start.sh
In start.sh line 5:
echo "Today is ${day}."
^-- SC2154: day is referenced but not assigned.
Run Code Online (Sandbox Code Playgroud)
有没有办法让shellcheck知道$变量是在源文件中?
@Dash-o 解释了以下程序:
首先,在源文件声明上方的行中添加source 指令,如下所示:
# shellcheck source=/path/to/sourcefile
#!/bin/bash
# shellcheck source=./.sourcefile
. .sourcefile
echo "Today is ${day}."
Run Code Online (Sandbox Code Playgroud)
接下来,在脚本之前shellcheck使用-x选项执行,如下所示:
shellcheck -x start.sh
me@myserver:~$ shellcheck …Run Code Online (Sandbox Code Playgroud) 我想将值通过管道传输到 bash 脚本中,该脚本将使用 jq 更改 json 文件中的值。我已经为此工作了一段时间,但我无法克服第一组错误。
这是我的简单 json 文件:
{
"0000000": {
"pogo": "AJHVUYKJBOIHKNNLNM"
},
"7000000": {
"pogo": "PPPVUYKJBOIHKNNLNM"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的脚本
#!/bin/bash
#-- pass into script
pgid="${1}"
tpogo="${2}"
file="file.json"
#-- tmp files
tmp_dir="$(mktemp -d -t 'text.XXXXX' || mktemp -d 2>/dev/null)"
tmp_input1="${tmp_dir}/temp_input1.txt"
if [ ! -n "$2" ]; then
{ echo "Check your command as you are missing a variable ..."; echo "Example: script.sh "00000001" "jvkkjbkjbd" ..."; }
exit 1;
fi
jq -r ".${pgid}.pogo = \"${tpogo}\"" "$file" > "$tmp_input1" …Run Code Online (Sandbox Code Playgroud) Ubuntu 16.04
Bash 4.4
我想删除第 n 行的最后 n 个字符。这是一个简单的文件,每行的最后 4 个字符是数字 4。
root@0o0o0o0o0 ~/.ssh # cat remove.txt
00000000004444
55555555555555555555555554444
222222222222222224444
000033334444
111114444
Run Code Online (Sandbox Code Playgroud)
要从我可以执行的每一行中删除最后 4 个字符 sed -i 's/....$//' remove.txt
root@0o0o0o0o0 ~/.ssh # sed -i 's/....$//' remove.txt
root@0o0o0o0o0 ~/.ssh # cat remove.txt
0000000000
5555555555555555555555555
22222222222222222
00003333
11111
Run Code Online (Sandbox Code Playgroud)
但是,如果我想从第 4 行中删除最后 4 个字符,删除 3,那么文件将如下所示:
0000000000
5555555555555555555555555
22222222222222222
0000
11111
Run Code Online (Sandbox Code Playgroud)