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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Shell script mistakes

117 点作者 reinhardt超过 11 年前

7 条评论

barrkel超过 11 年前
The single biggest shell script mistake is not handling whitespace in file names correctly, and it&#x27;s almost impossible to do correctly if you have weird file names: embedded newlines, leading and trailing spaces, embedded tabs. Embedded quotes can be tricky too, especially if you&#x27;re writing a script that generates a script.<p>That bit, writing a script that generates a script, happens surprisingly often in bash. It&#x27;s cheaper to pipe a stream to sed that converts it into a shell command than it is to iterate over all the lines, and individually pluck out the arguments for the commands you want to execute. Leaving the script as something that outputs shell commands also lets you inspect what it does before committing to it (by piping it to bash).
评论 #6752840 未加载
评论 #6753898 未加载
BCM43超过 11 年前
I find that after a shell script gets to be over 3 or so lines, it&#x27;s easier to switch over to python or perl. Do others feel the same?
评论 #6751917 未加载
评论 #6751830 未加载
评论 #6752310 未加载
评论 #6751735 未加载
评论 #6751965 未加载
评论 #6751865 未加载
评论 #6752285 未加载
评论 #6752083 未加载
评论 #6756248 未加载
评论 #6752691 未加载
gwu78超过 11 年前
This example<p><pre><code> for $file in *;do wc -l $file;done </code></pre> could be reduced to<p><pre><code> for $file in *; { wc -c $file ;} </code></pre> in some POSIX-like shells.<p>Is the for loop even necessary?<p><pre><code> echo wc -l * |sh </code></pre> But...<p><a href="http://www.in-ulm.de/~mascheck/various/argmax/" rel="nofollow">http:&#x2F;&#x2F;www.in-ulm.de&#x2F;~mascheck&#x2F;various&#x2F;argmax&#x2F;</a>
bloat超过 11 年前
This is a great page in the same vein - bash specific, but quite a bit more comprehensive.<p><a href="http://mywiki.wooledge.org/BashPitfalls" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;BashPitfalls</a>
knweiss超过 11 年前
I recommend the shell script static analyzer ShellCheck: <a href="https://github.com/koalaman/shellcheck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck</a>
sateesh超过 11 年前
One of the subtle shell script mistake which I was unaware was that if a shell script is modified, currently running instances of the script might fail [1].<p>1. <a href="http://stackoverflow.com/questions/2285403" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;2285403</a>
memracom超过 11 年前
Let&#x27;s not forget unit testing. After all a shell script is code and code should be unit tested.<p><a href="https://code.google.com/p/shunit2/" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;shunit2&#x2F;</a>
评论 #6754696 未加载
评论 #6754162 未加载