
Shell programming with bash: by example, by counter-example - ColinWright
http://matt.might.net/articles/bash-by-example/
======
fhd2
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.

~~~
ufo
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.

~~~
Sir_Cmpwn
I use fish.

[http://fishshell.com/](http://fishshell.com/)

Loop:

    
    
        for f in *.txt
            cat $f
        end

~~~
ufo
Really liking it so far. Thanks for the suggestion.

------
minimax
Modern Bash (a.k.a. the bash you get with Linux but not with OS X) even has
associative arrays similar to what you would call a dict in Python or a hash
in Perl.

    
    
        $ declare -A capitals
        $ capitals["Illinois"]="Springfield"
        $ capitals["California"]="Sacramento"
        $ echo ${capitals["California"]}
        Sacramento
        $ echo ${capitals["Illinois"]}
        Springfield
        $ for k in ${!capitals[@]} ; do
        >     echo "The capital of $k is ${capitals[$k]}"
        > done
        The capital of Illinois is Springfield
        The capital of California is Sacramento

~~~
gislifb
Nice, never knew about this. Cheers

------
VLM
A nice article. Needs some apps. My suggestion is in the mid-late 90s I was
making dough by writing CGI apps in BASH. Yes, BASH. lots of

#!/bin/bash

echo "Content-type: text/html"

echo

echo "<pre>"

(Run some CLI status type app here, or du -m a file or whatever)

echo "</pre>"

exit 0

Doing "web stuff" could be exciting for the learner.

(edited to add, doing "modern" web development in BASH might count as one of
the counterexamples, no matter how fun and easy it is)

~~~
mattmight
This is a great idea.

Since I'm using these as lecture notes for my class, I'll try to add it in
before I teach today.

~~~
VLM
Some fun ideas

If on a *nix platform try df for your cli app. This was "state of the art
remote monitoring" where I worked in the mid 90s. Also [http://whatever/cgi-
bin/uptime](http://whatever/cgi-bin/uptime) displayed about what you'd expect.
And the dmesg script, another handy one that is about what it sounded like.

Next step is there's a lot more HTML codes than <pre>. So put stuff in <h1>
etc. Before <blink> was censored I used that occasionally.

A fun next step is commands with mysterious and unclear outputs. Like grep.
Maybe grep a log file. Will you get no lines or a thousand lines? How do you
intend to inform the user that either the CGI crashed or there is in fact no
lines to output.

After that a bunch of if/then based on the output of grep -c

If its not fun to output text based on some if/then I'd try img tags. So now
something broken is a red down arrow. And a giant frameset of multiple
machines multiple tests is kind of an alert board, sort of. At least in 1997
or so.

You can make an interesting display of security problems by stashing temp
files and temp scripts and temp things in /tmp... what could possibly go wrong
with trusting the contents of something in /tmp, LOL?

Although I forget exactly how (its been awhile since BASH was a cutting edge
web framework) there's a way to access http parameters so you can do simple
forms that ask for something to search, then run grep on that passed
parameter, then format the grep output to an attractive level. And of course
you can pass "funny" parameters to the script which probably does no input
checking and introduce them to Bobby Tables meme and all that.

I didn't do this, but in the years before phpmyadmin I worked at a place that
basically had a script that ran whatever you passed it as a command for mysql.
As root. With no authentication. And of course a hard coded password in the
command line for anyone running "ps" to see. Yeah. It was one of those things
that the developers thought was awesome for dev work on the test server but
somehow snuck off the test server. That was a funny one, nothing bad happened,
but could have been bad.

This kind of stuff is a bit safer on a private lan than being tested out on
the internet, but everyone reading this probably knows that...

------
Kurtz79
I like how this article just presents you with the basics, but all that is
really needed to get up to speed quickly and with little fuss.

Most bash tutorials I have seen seem to be taking forever to get to the parts
I usually need a quick refresher of.

A very good quick start/reminder/advanced cheat sheet, good work.

~~~
mattmight
Thanks!

I created these as the lecture notes for my class this morning.

~~~
keithpeter
Well, that was a good morning's work.

Is this markdown -> HTML or pandoc or your own static generator?

~~~
mattmight
I write the HTML directly, and stitch it together with...bash scripts.

~~~
keithpeter
Should have guessed about the scripts :-)

------
GlitchMr
This is why I prefer [http://fishshell.com/](http://fishshell.com/) to bash.
Bash just has too much noise in my opinion (like then keyword).

~~~
barrkel
For simple ifs that don't require elses, I just use && or || as necessary:

    
    
        test -e "$file" || {
            echo "file not found: '$file'" >/dev/stderr
            exit 1
        }
    

etc.

------
lsiebert
See I think that there are at least two possible companion pieces to this
article that I'd love to read. There are a lot of things in bash that aren't
necessarily obviously useful to the novice bash programmer but become more
important when you spend some time writing things, like $? for tests _Common
use cases for some of the peculiar bash features with examples_ historical
explanation of how features developed were originally used

------
unhammer
You may want to warn that your "convert a folder of images from JPG to PNG in
parallel" might crash your machine. I didn't try it myself because I'd rather
not have 230 convert processes running at once …
[https://www.gnu.org/software/parallel/](https://www.gnu.org/software/parallel/)
is the typical solution to this kind of task.

------
drivers99
The linked relational shell programming article was really fun, informative,
and inspiring.

------
sebnukem2
Indispensible? What does it mean?

 _ed._ "indispensable" got it.

