5 ms·
Not bad, the first time I've seen most that matters about bash scripting in one place. (I miss [[ ]] though.) That said, I try not to do bash scripting. It's a
by fhd2 13y ago
Not bad, the first time I've seen most that matters about bash scripting in one place. (I miss [[ ]] though.)
That said, I try not to do bash scripting. It's always been a boiling-the-frog thing for me: You start with a cute script with a couple of lines and then you make it more robust and add some more stuff and before you know, you've written a non-trivial program in an awfully quirky programming language.
But I'm still happy I've been there and know my way around bash fairly well. Makes for some pretty powerful quick one liners.
- mattmight 13y agoI was on the fence about adding [[ ]]. I think I'll add it in a revision. I also agree on bash being quirky. I tell my class that the gold standard is a bash script that works 99% of the time.
- zx2c4 13y agoSeconded for [[ and ]]. Also { and } (with semi colons) vs ( and ), for sub processes would be nice to add.
- lsiebert 13y agoI'd also note that your discussion of scope doesn't mention local variables are possible in bash, see: http://www.tldp.org/LDP/abs/html/localvar.html http://www.tldp.org/LDP/abs/html/localvar.html
- barrkel 13y agoI think it might be lacking content on idioms. Things like 'while read -r line; do {stuff with $line}; done' These idioms have issues for their most common uses (e.g. dealing with filenames with embedded newlines), but they do work 99% of the time.
- unhammer 13y agoThey work 100 % of the time if you do it right. An example: find . -type f -name foo -print0 | while read -rd ''; do grep -l bar "${REPLY}" done find separates its output with the NUL byte when you do -print0, and -d '' means read NUL-separated input. Of course, in this case it would be better to use: find . -type f -name foo -print0 | xargs -0 -I{} grep -l bar '{}' but if you've got several things to do for each file, the first one makes sense (or you can put those several things in a script, and use xargs).
- unhammer 13y agohttp://mywiki.wooledge.org/BashFAQ/031 http://mywiki.wooledge.org/BashFAQ/031 is a great reference. If your guide is for bash and not sh, then I would say don't teach them [, just do [[. It's safer, since it won't glob or split words, you don't need quoting, and you don't need to escape operators like < and >. Maybe it's can be seen as more difficult since it's a sublanguage (doesn't use the regular syntax rules), but so are (( and $(( which you've discussed at length, and which are typically less useful than [[ in bash.
- ufo 13y agoWhat to use instead of bash though? I hate having to worry about all the little quirks in bash but most other languages make simple things like input redirection and piping commands awfully verbose.
- bakul 13y agorc is pretty minimal and consistent (available in plan9ports).
- chongli 13y agoHaskell! Seriously, people do shell scripting in Haskell. There's actually a pretty nice library designed for it called Shelly: https://github.com/yesodweb/Shelly.hs/blob/master/README.md#usage https://github.com/yesodweb/Shelly.hs/blob/master/README.md#...
- dllthomas 13y agoShelly is great! Still, there is some minimum complexity before something is better done in it, and porting a small bash script that has grown from a tiny bash script is pretty trivial.
- jevinskie 13y agoCheck out IPC::Run for Perl. [0] Perl was already pretty nice with it's backticks but IPC::Run takes it another step further and allows you to do all sorts of redirection/piping/etc with the single 'run' subroutine. If you want an 'expect' sort of use, you can do that too. [0]: http://search.cpan.org/~toddr/IPC-Run-0.92/lib/IPC/Run.pm http://search.cpan.org/~toddr/IPC-Run-0.92/lib/IPC/Run.pm
- Sir_Cmpwn 13y agoI use fish. http://fishshell.com/ http://fishshell.com/ Loop: for f in *.txt cat $f end
- ufo 13y agoReally liking it so far. Thanks for the suggestion.
- dllthomas 13y agoI try to keep my scripts extensions of what I'd type at the prompt - often quite directly, yanking it from my history when I realise I need to do something frequently or pulling a line into my editor with with edit-and-execute-command when it gets big (for a single line) and saving it off somewhere. Once it gets above about 10 lines, it's usually time for something else, though I've boiled a few frogs in my day...