5 ms·
Shell programming with bash: by example, by counter-example
- VLM 13y agoA 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 13y agoThis 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 13y agoSome 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...
- deleted 13y ago[deleted]
- fhd2 13y agoNot 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).
- Kurtz79 13y agoI 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 13y agoThanks! I created these as the lecture notes for my class this morning.
- keithpeter 13y agoWell, that was a good morning's work. Is this markdown -> HTML or pandoc or your own static generator?
- mattmight 13y agoI write the HTML directly, and stitch it together with...bash scripts.
- keithpeter 13y agoShould have guessed about the scripts :-)
- TempleOSV2 13y agoI made a JIT/AOT HolyC compiler. The command-line feeds into the C compiler. It compiles each function immediately after it is entered. If there is a statement outside a function, it compiles and executes it immediately. Dir("*"); I added C++ default args. Dir(); I made parens optional. Dir; I don't have cout. Instead, a string statement gets sent to PrintF. "Hello World\n"; I made 3 part conditionals. if (13<=age<20) "Teenager\n"; else "Not teenager\n"; Learn more: http://www.templeos.org/Wb/Doc/HolyC.html http://www.templeos.org/Wb/Doc/HolyC.html Watch the videos. http://www.templeos.org/Wb/Accts/TS/Wb2/Tutorials.html http://www.templeos.org/Wb/Accts/TS/Wb2/Tutorials.html Read the example code. http://www.templeos.org/Wb/Accts/TS/Wb2/Games.html http://www.templeos.org/Wb/Accts/TS/Wb2/Games.html I wrote both front and back end. I also wrote an assembler and unassembler. It is 20,000 lines of code http://www.templeos.org/Wb/Compiler http://www.templeos.org/Wb/Compiler
- GlitchMr 13y agoThis 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 13y agoFor 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.
- minimax 13y agoModern 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 13y agoNice, never knew about this. Cheers
- sebnukem2 13y agoIndispensible? What does it mean? ed. "indispensable" got it.
- drivers99 13y agoThe linked relational shell programming article was really fun, informative, and inspiring.
- lsiebert 13y agoSee 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 13y agoYou 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.