News for hackers. Stuff that matters.
Re:Shell programming with bash: by example, by counter-example by fhd2 on Monday January 13, 2014 @12:16PM (#7051890)
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.

Re:Shell programming with bash: by example, by counter-example by mattmight on Monday January 13, 2014 @12:24PM (#7051945)
I 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.

Re:Shell programming with bash: by example, by counter-example by zx2c4 on Monday January 13, 2014 @12:51PM (#7052111)
Seconded for [[ and ]]. Also { and } (with semi colons) vs ( and ), for sub processes would be nice to add.
Re:Shell programming with bash: by example, by counter-example by lsiebert on Tuesday January 14, 2014 @07:09AM (#7056569)
I'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
Re:Shell programming with bash: by example, by counter-example by barrkel on Tuesday January 14, 2014 @08:21AM (#7056816)
I 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.

Re:Shell programming with bash: by example, by counter-example by unhammer on Friday January 17, 2014 @03:01AM (#7074792)
They 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).
Re:Shell programming with bash: by example, by counter-example by unhammer on Friday January 17, 2014 @02:50AM (#7074763)
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.

Re:Shell programming with bash: by example, by counter-example by ufo on Monday January 13, 2014 @02:22PM (#7052690)
What 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.
Re:Shell programming with bash: by example, by counter-example by jevinskie on Monday January 13, 2014 @10:30PM (#7055196)
Check 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

Re:Shell programming with bash: by example, by counter-example by Sir_Cmpwn on Tuesday January 14, 2014 @01:36AM (#7055724)
I use fish.

http://fishshell.com/

Loop:

    for f in *.txt
        cat $f
    end
Re:Shell programming with bash: by example, by counter-example by ufo on Tuesday January 14, 2014 @06:57AM (#7056517)
Really liking it so far. Thanks for the suggestion.
Re:Shell programming with bash: by example, by counter-example by chongli on Monday January 13, 2014 @10:05PM (#7055103)
Haskell! 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#...

Re:Shell programming with bash: by example, by counter-example by dllthomas on Monday January 13, 2014 @10:09PM (#7055126)
Shelly 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.
Re:Shell programming with bash: by example, by counter-example by bakul on Monday January 13, 2014 @09:32PM (#7055013)
rc is pretty minimal and consistent (available in plan9ports).
Re:Shell programming with bash: by example, by counter-example by dllthomas on Monday January 13, 2014 @10:12PM (#7055140)
I 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...