Advanced Bash Scripting(tldp.org) |
Advanced Bash Scripting(tldp.org) |
For other applications it makes sense to use your scripting language of choice (libraries, etc.) but in any situation where you need absolute portability or availability on an otherwise bare system, bash is the way to go.
There are whole projects like the Arch Linux Installation Framework (AIF) written in bash for this reason.
Named arrays, quick string handling and the [[ ]] test syntax alone make this worth reading, and that's just scratching the surface.
Unfortunately a lot of the stuff has not been updated in 5 yrs or so - but it is still fun to read through from time to time.
It is not exactly a BASH guide but another good guide is "UNIX for Poets" - I think there have been a number of news.ycombinator posts regarding it. This is a good intro for the text manipulation tools you would tend to use in a BASH script.
Interestingly, this is why Debian uses dash: A smaller shell is more portable.
I have to work in Solaris, SUSE SLES, some other linux where I don't know what they run, and OS X and I don't have root on most of those. bash seems to work the same everywhere and always seems to be there when I get access to a newly provisioned environment.
I write scripts like large programs from scratch: decomposition, error handling, proper logging, unit testing, packaging, etc.
I wrote my own library with few useful modules, which is designed for strict mode (bash-modules on assembla), which I recommend to use even for trivial programs.
I have no problems with bash.
I have no trouble understanding how complex programs can be written in bash, but I have never understood why.
At this point, I wished I had started in python.
To answer maven911, I usually do LAMP stack development but pick up all kinds of technologies along the way.
Long bash scripts are less readable and maintainable than python scripts.
prog1 2> prog1.log | prog2 2> prog1.log | prog3
in Python is not nearly so succinct as in the shell (using only Python's standard library). And don't screw up waiting for processes or flushing buffers, things that the shell takes care of for you AFAIK.On the other hand, things like string or date manipulation are easier in Python than in the shell.
I personally only use Bash when the script is just a trivial translation of commands I enter.
I use it like I use 'perldoc perl' or http://docs.python.org/ or https://docs.djangoproject.com/en/1.3/ or https://developer.mozilla.org/en/JavaScript/Guide or http://docs.jquery.com/Main_Page or http://www.dpawson.co.uk/xsl/sect2/sect21.html etc.
Once you grok it, it is a pretty excellent tool for testing, monitoring, coordinated pieces, etc. Besides Solaris, I can typically sit down at any type of machine or embedded device and write a fairly non-trivial script without having to ask, "what version of python is installed?" or "is it hooked up to the outside world, so I can install these libraries?".
There's power in availability and commonality even if you give up some readability.
http://mywiki.wooledge.org/BashGuide
Check also the list of Bash online tutorials:
If your Solaris machines have /bin/bash available, consider yourself very lucky.
dash is also portable in the sense that you need it to be: it implements the POSIX shell spec and not much more, so it's likely that you have access to all its features when you need to use another shell as well.
In my scripts, I declare and describe command line interface of each script, so it is not hard to rewrite script in an other language when necessary. About 5% of my shell scripts were rewritten in an other language (Perl, Java, C). I had no problem with that.
Other developers and admins are happy with my software, I even got some internal awards.
My documentation/code ratio is about 7/4 (7 lines of comments and documentation per 4 lines of code), so it is easy to catch design errors - they are hard to describe in documentation in most cases. :-)
I am avoiding debugger as much as I can (lack of builtin debugger in bash helps me a lot :-)), so my software handles errors in most cases and error messages are _very_ descriptive.
I have no problems with bash, perl, C, Java, JavaScript, etc.
I hope, it is temporary problem. ;-)