TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Defensive Bash programming

127 点作者 urlwolf将近 11 年前

12 条评论

vog将近 11 年前
The article mentions so many topics, but misses almost all important ones.<p>* First of all, use proper quoting. There are so many possibilities for file names, command line arguments, etc. that every unquoted usage of a variable is essentially a security risk.<p>* Then, start your script with &quot;set -e&quot;, which stops the script whenever one of the commands fail, instead of blindly continuing and messing things up. This is the most important option for robust shell scripts.<p>* Also use &quot;set -u&quot; which makes the script stop on undefined variables. This includes $1, $2, etc., so it provides checks for missing arguments for free.<p>* In addition to &quot;set -e&quot;, also set &quot;set -o pipefail&quot;, otherwise a pipe will only break if the last command fails, while with &quot;set -o pipefail&quot; the pipe fails whenever any command of the pipe fails.<p>* After that, you may continue with spacing issues in &quot;for&quot; loops, and that you should not pipe the &quot;find&quot; output directly (instead, use either &quot;-print0&quot; + &quot;xargs -0&quot;, or use &quot;-exec&quot;), and similar stuff.<p>When you got all of this right, and only then!, you may start worrying about the (relatively) minor issues mentioned in the article.
评论 #7816397 未加载
评论 #7817542 未加载
评论 #7817667 未加载
mooism2将近 11 年前
The first rule of defensive bash programming should be: quote everything. Incredibly, the article doesn&#x27;t mention quoting at all, doesn&#x27;t even use it silently in examples.
评论 #7815374 未加载
评论 #7815881 未加载
评论 #7817471 未加载
cthalupa将近 11 年前
Generally, bash is for quick and dirty things I want to automate. I&#x27;ll go to perl or python if I need anything more complex.<p>The amount of effort put into these examples is already way higher than my personal sniff test for &quot;Should I be doing this in something besides bash?&quot;
评论 #7815771 未加载
borplk将近 11 年前
If you do have the choice, the first rule of defensive bash programming is to not program in bash. But otherwise it was a great article.
danielweber将近 11 年前
I freaking love &quot;set -x&quot; and wish every language had an equivalent. When I switch from bash to another language I miss &quot;set -x&quot; deep in my soul.
评论 #7815951 未加载
评论 #7817971 未加载
评论 #7821951 未加载
rquirk将近 11 年前
You can use the pipe | as a continuation at the end of a line. No need to use the backslash escape then. i.e. you never should need to use | \ at the end of a line.
评论 #7815821 未加载
nemasu将近 11 年前
Defining a function for is_file as [[ -f $blah ]]; seems like defining a function for var++ as increment_one() ...
评论 #7815399 未加载
q3k将近 11 年前
And yet he pipes from ls...
评论 #7816144 未加载
评论 #7817133 未加载
dozzie将近 11 年前
Yes, of course. Use more bashisms, wait for bash to change its behaviour in those bashisms (it already happened and was not that rare) and happy debugging.
评论 #7815477 未加载
Sir_Cmpwn将近 11 年前
I thought that UPPER_CASE variables were a bad idea? Doesn&#x27;t the bash world generally accept that you should use uppercase only for enviornment variables, and lowercase for variables in the script&#x27;s context?
评论 #7820862 未加载
pertinhower将近 11 年前
Is threesome kind of secret code embedded in the typos?
iamthebest将近 11 年前
Corporate gateway seems to not like this domain:<p>This web site ( www.kfirlavi.com ) has been blocked because it has been determined by Web Reputation Filters to be a security threat to your computer or the corporate network. This web site has been associated with malware&#x2F;spyware. Reputation Score for www.kfirlavi.com: -7.1<p>Reputation scores can range from -10 (worst) through 10 (best).
评论 #7815497 未加载