shellcheck

如何写出安全的、基本功能完善的Bash脚本

廉价感情. 提交于 2020-12-28 01:13:56
每个人或多或少总会碰到要使用并且自己完成编写一个最基础的Bash脚本的情况。真实情况是,没有人会说“哇哦,我喜欢写这些脚本”。所以这也是为什么很少有人在写的时候专注在这些脚本上。 我本身也不是一个Bash脚本专家,但是我会在本文中跟你展示一个最基础最简单的安全脚本模板,会让你写的Bash脚本更加安全实用,你掌握了之后肯定会受益匪浅。 为什么要写Bash脚本 其实关于Bash脚本最好的解释如下: The opposite of "it's like riding a bike" is "it's like programming in bash". A phrase which means that no matter how many times you do something, you will have to re-learn it every single time. — Jake Wharton (@JakeWharton) December 2, 2020 意思就是,跟骑自行车相反,无论做了多少次,每次都感觉像重新学一样。 但是Bash脚本语言和其他一些广受欢迎的语言,例如JavaScript一样,他们不会轻易突然消失,虽然Bash脚本语言不太可能成为业界的主流语言,但实际他就在我们周围,无处不在。 Bash就像继承了shell的衣钵一样

Why are && and || preferred to -a and -o

二次信任 提交于 2019-12-20 00:59:09
问题 When writing if blocks in bash, shellcheck tells me that && and || are preferred to using -a and -o . Why? It is faster, or just simply a stylistic preference to make scripts look cleaner? The specific message I get is: ^-- SC2166: Prefer [ p ] || [ q ] as [ p -o q ] is not well defined. 回答1: From the POSIX specification for test: 4 arguments: The results are unspecified. [OB XSI] [Option Start] On XSI-conformant systems, combinations of primaries and operators shall be evaluated using the

What does this error mean? (SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.)

旧街凉风 提交于 2019-12-10 13:06:04
问题 I'm writing a script to generate draft posts for my blog. After running ShellCheck, I keep seeing this error pop up. What does this mean and can someone provide an example? SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects. In addition, I'm not sure what I need to do in order to pass the value of $title to the "Title" field in the post's YAML... #!/bin/bash # Set some variables var site_path=~/Documents/Blog drafts_path=~/Documents/Blog/_drafts title="$title" #

How to portability use “${@:2}”?

ⅰ亾dé卋堺 提交于 2019-12-09 21:18:20
问题 On Allow for ${@:2} syntax in variable assignment they say I should not use "${@:2}" because it breaks things across different shells, and I should use "${*:2}" instead. But using "${*:2}" instead of "${@:2}" is nonsense because doing "${@:2}" is not equivalent to "${*:2}" as the following example: #!/bin/bash check_args() { echo "\$#=$#" local counter=0 for var in "$@" do counter=$((counter+1)); printf "$counter. '$var', "; done printf "\\n\\n" } # setting arguments set -- "space1 notspace"

How to portability use “${@:2}”?

血红的双手。 提交于 2019-12-04 17:31:57
On Allow for ${@:2} syntax in variable assignment they say I should not use "${@:2}" because it breaks things across different shells, and I should use "${*:2}" instead. But using "${*:2}" instead of "${@:2}" is nonsense because doing "${@:2}" is not equivalent to "${*:2}" as the following example : #!/bin/bash check_args() { echo "\$#=$#" local counter=0 for var in "$@" do counter=$((counter+1)); printf "$counter. '$var', "; done printf "\\n\\n" } # setting arguments set -- "space1 notspace" "space2 notspace" "lastargument"; counter=1 echo $counter': ---------------- "$*"'; counter=$((counter