TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What I learned from others' shell scripts

173 pointsby fizerkhanalmost 12 years ago

17 comments

chrismorganalmost 12 years ago
I don&#x27;t like the `require_curl` example.<p>Here&#x27;s what is done there:<p><pre><code> OK=0 FAIL=1 function require_curl() { which curl 2&gt;&amp;1 &gt; &#x2F;dev&#x2F;null if [ $? -eq 0 ] then return $OK fi return $FAIL } </code></pre> (`2&gt;&amp;1 &gt; &#x2F;dev&#x2F;null` drops stdout and writes stderr to stdout—not what was meant. `which` doesn&#x27;t write to stderr, so I drop that part.)<p>That can be shortened significantly by using the return code directly in the if branch:<p><pre><code> function require_curl() { if which curl &gt; &#x2F;dev&#x2F;null then return $OK fi return $FAIL } </code></pre> Or by just using the return code directly:<p><pre><code> function require_curl() { which curl &gt; &#x2F;dev&#x2F;null return $? } </code></pre> And as it will return the return code of the last statement executed:<p><pre><code> function require_curl() { which curl &gt; &#x2F;dev&#x2F;null }</code></pre>
评论 #6210445 未加载
评论 #6211028 未加载
评论 #6217106 未加载
评论 #6210958 未加载
评论 #6210375 未加载
评论 #6210349 未加载
评论 #6210635 未加载
mordaealmost 12 years ago
My eyes bleed. So many unquoted expansions!<p><pre><code> APP_ROOT=`dirname $0` filename=`basename $filepath .html` </code></pre> should read:<p><pre><code> APP_ROOT=`dirname &quot;$0&quot;` filename=`basename &quot;$filepath&quot; .html` </code></pre> Plus if your script supports both `--version` and `--help` with proper formats, you can easily generate a manual page with `help2man`. The help output example is far from that. Also, he does not mention getopt at all.<p>The magic line for that is:<p><pre><code> eval &quot;set -- $(getopt -o hV -l help,version -- &quot;${@}&quot;)&quot; || exit $? echo &quot;${*}&quot; </code></pre> It does this:<p><pre><code> $ .&#x2F;test foo bar --help --version --help --version -- foo bar </code></pre> Which can be parsed using a `while` loop with `shift`.<p>For other tips on shell scripting best practices I recommend Gentoo ebuilds. They tend to be quite well written. And always read scripts from other people before running them yourself.
评论 #6210673 未加载
评论 #6210506 未加载
praptakalmost 12 years ago
Shell scripts have their use but at the point when your script needs output coloring or printing debug information, it is time to switch to Python.<p>Edit: Place a general disclaimer to weaken the absolute tone of the above statement. Rule of thumb, exceptions apply, yadda, yadda. In other words, I somewhat agree with most responses that disagreed with the statement above :-)
评论 #6210346 未加载
评论 #6210652 未加载
评论 #6210439 未加载
评论 #6210743 未加载
评论 #6210304 未加载
评论 #6210478 未加载
评论 #6211648 未加载
评论 #6210696 未加载
arnehormannalmost 12 years ago
Huh, no mention of &quot;set -e -u&quot;? Do yourself a favor and follow this, too: <a href="http://www.davidpashley.com/articles/writing-robust-shell-scripts/" rel="nofollow">http:&#x2F;&#x2F;www.davidpashley.com&#x2F;articles&#x2F;writing-robust-shell-sc...</a>
评论 #6210822 未加载
评论 #6210815 未加载
评论 #6210739 未加载
captn3m0almost 12 years ago
A good gem I found recently was to use the big version of the command line flags. So instead of seeing -s -q 1, you should use arguments like --max-depth. Increases the readability of scripts by a huge margin.
评论 #6210585 未加载
derekp7almost 12 years ago
My favorite discovery is how to efficiently do RPC style calls in BASH. Let&#x27;s say you have some functions and variables you want to execute on a remote server. Just do the following:<p><pre><code> ssh remotehost &quot; $(declare -p var1 var2 var3) $(declare -f func1 func2 remotemain) remotemain&quot; </code></pre> In this example, var1, var2, var3 and func1, func2 are support variables&#x2F;functions for the function &quot;remotemain&quot;. This pushes all those to the remote side, then calls remotemain.
评论 #6212210 未加载
评论 #6211718 未加载
louwrentiusalmost 12 years ago
A while ago I was working on a bash function library. The library itself may not be that usefull but it also contains some examples on how to color output and move the cursor around. <a href="http://code.google.com/p/bsfl/" rel="nofollow">http:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;bsfl&#x2F;</a>
lelfalmost 12 years ago
<p><pre><code> function require_curl() { which &quot;curl&quot; &gt; &#x2F;dev&#x2F;null; } function require_curl() { which -s &quot;curl&quot;; } function debug { ((DEBUG)) &amp;&amp; echo &quot;&gt;&gt;&gt; $*&quot;; } </code></pre> Last one is bash, which -s is BSD&#x27;ish
评论 #6210450 未加载
评论 #6210418 未加载
pavsalmost 12 years ago
As someone who is learning PHP as his first programming language, I am surprised to see similarity in terms to syntax with shell scripts. Are there other popular language with php-like sytax?
评论 #6210831 未加载
评论 #6213125 未加载
essrinnalmost 12 years ago
Shell scripts frequently fail to handle directory names with spaces in them. Example from the linked article: APP_ROOT=`dirname $0`<p>This is not going to do what you expect if the current script is run via a command with spaces in one of the preceding directories&#x27; name.<p>It should be: APP_ROOT=&quot;`dirname &quot;$0&quot;`&quot;<p>Even this would fail if your immediate parent directory&#x27;s name ended with a newline character, but should handle any other whitespace without issue.
评论 #6210480 未加载
louwrentiusalmost 12 years ago
Browse this website and you will learn a lot about bash. Opinionated at times, but that&#x27;s what I like. If you use Bash scripting you&#x27;ve might encountered this site already, but for what it&#x27;s worth: <a href="http://mywiki.wooledge.org/BashFAQ" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;BashFAQ</a>
ballardalmost 12 years ago
I wrote a silly script for noob openbsders like yours&#x27; truly: it builds the usage from functions&#x27; comments in the script on-the-fly. <a href="https://gist.github.com/6120072" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;6120072</a>
jalcinealmost 12 years ago
Might inject those `tput` color commands into my shell<p>To those curious: <a href="https://github.com/jalcine/dotfiles" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jalcine&#x2F;dotfiles</a>
jfbalmost 12 years ago
All this article does is make me wish there were an alternative to typical Borne shell garbage. No sane person would ask for a wretched pile of hacks like this.
评论 #6213114 未加载
arek2almost 12 years ago
Shouldn&#x27;t it be:<p>which curl &gt;&#x2F;dev&#x2F;null 2&gt;&amp;1<p>?<p>(what I learned from man bash)
评论 #6210440 未加载
评论 #6210430 未加载
评论 #6210326 未加载
评论 #6210342 未加载
xxtjaxxalmost 12 years ago
<a href="http://mywiki.wooledge.org/Bashism" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;Bashism</a>
herbigalmost 12 years ago
You&#x27;ve got a trailing comma in your about json.