Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

[ Create a new account ]

Bash Cookbook

Posted by samzenpus on Wednesday August 13, @01:59PM
from the read-all-about-it dept.
Chad_Wollenberg writes "Anyone who has used a derivative of Unix over the past 20 years has used Bash, which stands for Borne Again Shell. The geek in all of us makes us want to extend our ability to rule the command line. To truly master a Unix environment, you need to know a shell, and Bash is easily the most popular of them. Any Unix/Linux/BSD administrator knows the power at your fingertips is fully extended by what you can do within the Bash environment, and all of us need the best recipes to get the job done." Keep reading for the rest of Chad's review.
Bash Cookbook
author Carl Albing, JP Vossen, Cameron Newham
pages 598
publisher O'Reilly
rating 9
reviewer Chad Wollenberg
ISBN 978-0-596-52678-8
summary A good book for intermediate and above users of Bash
Enter Bash Cookbook. Properly named for the series of O'reilly books that gives you valuable information on subjects in the form of recipes, this book was refreshing in that it was properly organized, and surprisingly contemporary, even citing Virtualized platforms as a way to try out different OS's for Bash. The book does a good job of pointing out the different operating systems that do run Bash, even citing Cygwin for Windows. They also use the POSIX standard, so that all of the examples are portable across platforms.

Bash Cookbook is by no means for the feint of heart. It seems that the book is meant for intermediate and above users of Bash. However, the first several chapters do a significant job of over viewing basic concepts of Bash navigation and combing simple commands. The book quickly changes gears to complex statements on how to get things done in Bash.

By Chapter 7, Bash Cookbook extends out of Bash commands and begins exploring combining the power of bash scripting with useful command such as grep, awk, and sed. To quote the authors, "if our scripting examples are going to tackle real-world problems, they need to use the wider range of tools that are actually used by real-world bash users and programmers." And that is exactly what they do. This chapter alone gave me the ability to do more in the command line environment simply by explaining the functions of the scripts put forth. That is something that any reader, intermediate to expert, can take from this book. The detailed explanations really do give everyone the ability to learn something about the commands, and the references to additional resources often lead me to the computer, looking up further details.

I found Chapter 11 to be very useful (pun intended) finally grasping some concepts on the find command that have previously escaped me. From Chapter 12 on, the book focuses on writing useful and complex scripts. This is where the book really begins to shine for the Unix enthusiast and system administrator. The scripts found in Chapter 12, and their elaborate descriptions begin to show the true power of Bash scripting, and how much you can automate. Chapter 14 is about securing your scripts, and is a heavy read, but well worth reading for any administrator that would be using their scripts in a production environment.

Just when you think this book has reached its limits, it gives very handy customization examples in Chapter 16 on how to configure and customize Bash. And also goes into common mistakes made by the novice user. Combine all of that with the Appendices for quick reference, and this book has not left my side since it arrived. While I would not recommend this book for the novice user, I would recommend this book to any system administrator that has to work with Unix or Linux. If nothing else, the examples given here are full of good, reusable code to make tasks easier in your day to day functions. Well done.

You can purchase Bash Cookbook from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • BASH != Bourne Shell (Score:5, Informative)

    by llamalad (12917) on Wednesday August 13, @02:02PM (#24587003)

    'sh' is the Bourne shell.
    'bash' is the Bourne Again SHell.

    They're not the same.

    • by laejoh (648921) on Wednesday August 13, @02:08PM (#24587105)
      It doesn't matter. If you code like I do you don't need to worry about the difference between /bin/sh and /bin/bash:

      <begin file A>
      #!/bin/bash

      perl <<EOP
      print "hello world\n";
      EOP
      <end file A>

      <begin file B>
      #!/bin/sh

      perl <<EOP
      print "hello world\n";
      EOP
      <end file B>

      See? Both work, it's so easy!
        • by CastrTroy (595695) on Wednesday August 13, @02:35PM (#24587649) Homepage
          The lack of structured data and live objects is a feature, not a bug. The fact that everything is a string, and that everything can be piped between all the different commands means that you can string together commands in new and exciting ways that nobody ever thought was possible. Making all the commands pass around different types of objects means that all the other commands have to be aware of all these other datatypes, and have to know how to handle them. If you want something with objects and structured data in the shell, then there's MS PowerShell. But maybe there's a reason it hasn't caught on yet.
          • by Tetsujin (103070) on Wednesday August 13, @03:13PM (#24588255) Homepage

            The lack of structured data and live objects is a feature, not a bug. The fact that everything is a string, and that everything can be piped between all the different commands means that you can string together commands in new and exciting ways that nobody ever thought was possible. Making all the commands pass around different types of objects means that all the other commands have to be aware of all these other datatypes, and have to know how to handle them.

            This is true of textual data as well - you're simply glossing over the complexity of serializing and re-parsing any non-trivial data structure in textual form... You've ignored the fact that for any two tools to work together, their assumptions about the structure with which data is encoded over the stream have to match.

            See, if your text-encoded data is simple enough that you can simply choose a character as a field delimiter and another as a record separator, it's easy to split your data into individual records and fields again. It gets a little harder if you want to provide for the possibility that your records may actually contain the delimiter characters (then you're into parsing - at least enough parsing to distinguish between an escaped character and an unescaped one) Setting up the format so you can really encapsulate anything - that reaches the point where it's worth having a tool whose only job is interfacing with this format...

            The problem here is that normally when you want to interpret some encoded form of a piece of data, you first translate it into something that's easier to work with. But in BASH (and most CLI shells, I believe) there is no "more convenient form" to which you can readily translate any moderately complex structure. (Consider, for instance, how you would implement an XML parser for Bash - as an external command it simply wouldn't work... it'd have to be done as a Bash plug-in module, and even then its capabilities would be limited. And then suppose you want to filter the parsed data and pass it to another process? You've got to re-encode it, and then the next process has to know how to decode this encoding (probably still XML) as well...

            I contend that the "encode everything as a string" mentality was an asset, due to computer limitations in the time period in which the convention started - but these days I think it's pretty limiting.

            If you want something with objects and structured data in the shell, then there's MS PowerShell. But maybe there's a reason it hasn't caught on yet.

            I think Powershell has been progressing (in terms of popularity, I mean) quite satisfactorily... The reason it hasn't caught on with me, however, is 'cause I want a Unix solution (that is, runs on Unix, and fairly Unix-styled), not a Windows one - and while I agree with the logic behind some of their design decisions (like the verb-noun convention for command names) I don't like the consequences (the language is much too verbose for my tastes...)

            I think it's a step in the right direction but not quite what I'm looking for.

            Assuming Powershell hasn't been embraced and taking that as a sign that one particular facet of its design was a bad idea is pretty laughable. Any new tool takes time to be adopted - and Powershell is a tool for a fairly small niche - CLI users on Windows.

        • by daedae (1089329) on Wednesday August 13, @02:36PM (#24587669)

          True story:

          A guy I go to school with (I'm in CS, he's in physics) used to talk about how he was a Bash wizard. Since he was generally talking about writing scripts to submit jobs to a PBS-based cluster, I assumed he meant just in terms of rapidly submitting a large variety of jobs. One day he complains to me that his simulations were slow and wanted me to look at it with him to help him speed them up. So I say fine, send me the file...

          He'd written a particle physics Monte Carlo sim using Bash and Linux command line tools (in particular, there were calls to bc everywhere).

      • In Ubuntu since edgy, sh has been symlinked to dash instead. This allowed for a much faster boot while breaking all the "sh" scripts that used bash-specific syntax.

        There was a bunch of whining about it when it happened but I think everyone fixed their scripts and shut up.

      • by Krishnoid (984597) on Wednesday August 13, @03:36PM (#24588663)
        Generally -- except in one notable case [ubuntu.com], and expected in Debian lenny [debian.org]. As you can imagine, this caused a lot of complaints. From what I skimmed there and other places, the attitude is that /bin/sh under Debian should be a fully POSIX-compliant shell, and if you want to use bashisms, start your program with #!/bin/bash.
        • by againjj (1132651) on Wednesday August 13, @03:32PM (#24588579)

          No, it doesn't. There are a couple of changes, like ~/.bashrc is not read, but mostly it stays as-is. Basically, if you invoke bash as sh, AND stay inside the sh syntax/commands, then bash works like sh.

          -sh-3.00$ /bin/sh
          sh-3.00$ help bind
          bind: bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq]
          [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
          Bind a key sequence to a Readline function or a macro, or set
          a Readline variable. The non-option argument syntax is equivalent
          to that found in ~/.inputrc, but must be passed as a single argument:
          bind '"\C-x\C-r": re-read-init-file'.
          [...]
          sh-3.00$ echo $PS1
          \s-\v\$
          sh-3.00$ export PS1='myprompt '
          myprompt export PS1='\s-\v\$ '
          sh-3.00$ exit
          exit
          -sh-3.00$

          Here, bind and PS1 (help maybe?) are both in bash but not sh.

  • by Tetsujin (103070) on Wednesday August 13, @02:03PM (#24587033) Homepage

    So, what, does this refer to people who act like they're going to rip your heart out of your chest, only it all turns out to be a ruse so they can kick you in the balls?

    • by khasim (1285) <brandioch.conner@gmail.com> on Wednesday August 13, @02:11PM (#24587187)

      Who already own "sed and awk" and buy a book that is supposedly about Bash scripting only to find out that the advanced section replicates what is in the sed and awk book. Which is very annoying.

      Not that it's a bad book. I just believe that it should have been more focused on Bash only scripting.

      If you want to learn about sed and awk, buy the sed and awk book. If you want to learn Bash scripting, there are a LOT of more useful sites online.

          • by CastrTroy (595695) on Wednesday August 13, @03:00PM (#24588075) Homepage
            I'm not sure where our opinions differ. sed and awk are both extremely powerful. bash is extremely powerful. One of the things that makes bash powerful is the existence of sed and awk. sed and awk don't require bash, and bash doesn't require sed and awk. Yet if you write a book on using bash, and leave out sed and awk, you would be doing your readers a great injustice.
  • Bourne-Again Shell (Score:5, Informative)

    by cos(0) (455098) <pmw+slashdot@qnan.org> on Wednesday August 13, @02:03PM (#24587037) Homepage

    Bourne Again Shell [gnu.org], not Borne.

    • by Ecuador (740021) on Wednesday August 13, @02:26PM (#24587479) Homepage

      Seriously now, this is like posting a LOTR review by someone who thinks it was written by RJ Tokelen. Or a Star Trek review from a fan of "the late Rod N. Barry".
      You gotta love our editors!

      Anyway, back to my review of Dark Night... Chris Nolon did it again!

    • by MarkusQ (450076) on Wednesday August 13, @04:09PM (#24589151) Journal

      And it's "faint of heart" not "feint of heart".

      And it would be "overviewing basic concepts" not "over viewing basic concepts" if "overview" were a verb.

      I made it as far as:

      I found Chapter 11 to be very useful (pun intended)

      before threwing in the trowl.

      --MarkusQ

  • by UltraMathMan (1139987) on Wednesday August 13, @02:04PM (#24587055)
    Not knocking the book, especially as I haven't read it, but I've found the Advanced Bash Scripting Guide (available free online) http://tldp.org/LDP/abs/html/ [tldp.org] extremely helpful on numerous occasions.
  • by gardyloo (512791) on Wednesday August 13, @02:06PM (#24587077)

    I may even buy the book based on the review.

    Leaving aside stuff like not for the feint of heart, which is just poor editing, what the hell does I found Chapter 11 to be very useful (pun intended) mean?

          Maybe it's the ultimate meta-pun, where there was no pun in the first place, but the author pointed out that one was intended, so one was slipstreamed into the statement.

  • Unix you say.. (Score:5, Informative)

    by The Moof (859402) on Wednesday August 13, @02:08PM (#24587113)
    As a BSD user (OpenBSD and FreeBSD), the only way I run into bash is to explicitly go and install it. Actually, the only place I have run into bash as a default install is on Linux.

    I run into alot more sh, ksh, csh, and tcsh.
  • by mea37 (1201159) on Wednesday August 13, @02:10PM (#24587131)

    Well, ok... Cookbook sucks!

    Oh, did I parse that wrong?

  • They can just enter "man bash" on the command line

  • I loved Bash (and was the maintained of the FreeBSD port of the Bash tab-completion for a while), but gave it up forever about a week after I tried Zsh [zsh.org]. For me, it's like "Bash done right", from associative arrays for easy scripting to tab-completion that's fast and doesn't pollute the namespace with thousands of tiny functions:

    $ zsh
    $ set | wc -l
    167
    $ bash
    $ set | wc -l
    7221

    Which leads me to ask: has anyone tried Zsh but then gone back to Bash? If so, why?