Slashdot Mirror


Wicked Cool Shell Scripts

norburym writes with a review of Wicked Cool Shell Scripts - 101 Scripts for Linux, Mac OS X, and UNIX Systems. "This incredibly fun book (really!), written by Dave Taylor, a veteran UNIX, Solaris and Mac OS X author, is chock full of 101 scripts to customize the UNIX (Bourne) shell." Read on for the rest. Wicked Cool Shell Scripts - 101 Scripts for Linux, Mac OS X, and UNIX Systems author Dave Taylor pages 368 publisher No Starch Press rating 10 reviewer Mary Norbury-Glaser ISBN 1593270127 summary 101 Scripts for Linux, Mac OS X, and UNIX Systems

Chapters are divided into an array of topics sure to catch the attention of any UNIX based system user: The Missing Code Library, Improving on User Commands, Creating Utilities, Tweaking Unix, System Administration: Managing Users, System Administration: System Maintenance, Web and Internet Users, Webmaster Hacks, Web and Internet Administration, Internet Server Administration, Mac OS X Scripts, and Shell Script Fun and Games.

In true "cookbook" fashion, each hack is numbered and divided into The Code, How It Works, Running the Script, The Results and Hacking the Script. Throughout, the author clearly describes the syntax and functionality of each script, often with additional notes in How It Works detailing the syntax process and interesting asides. But Hacking the Script is what gives Wicked Cool Shell Scripts true value; where applicable, the author uses this section to describe script modifications to achieve a variety of alternative real world, practical results. This additional section alone easily triples the total number of scripts the reader is exposed to.

This book enables the reader to get "up close and personal" with their UNIX based system and explore the possibilities afforded by becoming intimate with the command line interface. The reader will find themselves easily propelled into the world of scripting, thanks entirely to Dave Taylor's ability to take what some might describe as a fairly dry topic and translate it into a logical and user friendly construct. Just reading through the table of contents is inspiring and intriguing; did you know you could write a script to retrieve movie info from IMDb? or track the value of your stock portfolio? or that you can use a very simple script to check spelling on your web pages?

Sysadmins and webmasters will find this book fundamentally critical to day-to-day operations; there are dozens of invaluable, customizable scripts highlighted in this book to enable professionals to save time and add simple, elegant solutions to annoying issues in their work environment. User account management, rotating log files, cron scripts, web page tweaks, apache passwords, synchronizing via ftp, etc. are all eminently useful and tweakable.

Geeky home users will discover they can use these scripts to work with files and directories, create spell-checking utilities, calculate loan payments, create summary listings of their iTunes libraries, and of course, play games. Many of the sysadmin scripts would also be of interest to the power user: analyzing disk usage, killing processes by name and backing up directories, to name a few. Both types of users will find this book inspiring and truly fun!

One of the secret pleasures of a technical book reviewer is finding those wonky bits of code that suffer from misplaced or missing punctuation, misspelled words and other basic typographic errors inherent in the book publishing process. I randomly selected many of these scripts to try out in the process of doing this review and...dang, haven't found any errata yet. But be sure to check out the errata page on Dave Taylor's web site for any that more astute readers may find (there were none, as of this writing).

Also be sure to take a closer look at Dave's shell script library, which lists additional scripts that didn't make the cut for the book. As convenient as it is to download the entire script library, I would like to stress the value of buying the book, which will provide you with invaluable instruction and guidance in understanding the syntax of the scripts and it also illustrates how making small but significant tweaks can modify the output to match your specific needs.

(A special nod of appreciation to Dave Taylor's Tintin references!)

You can purchase Wicked Cool Shell Scripts - 101 Scripts for Linux, Mac OS X, and UNIX Systems from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

22 of 569 comments (clear)

  1. Hmm. by markov_chain · · Score: 5, Funny

    did you know you could write a script to retrieve movie info from IMDb?

    Please please tell me it's not

    #!/bin/sh
    wget 'http://imdb.com/title/tt0151804/'

    --
    Tsunami -- You can't bring a good wave down!
    1. Re:Hmm. by Anonymous Coward · · Score: 5, Funny

      I wonder if he also includes a script for completing TPS reports.

  2. When the only tool you have is a hammer... by robslimo · · Score: 5, Insightful

    ...everything looks like a nail

    Actually, I agree that shell scripting is a powerful tool and well worth understanding. But 'a loan calculator' script? Gee, once you know the formula, a coupla minutes in a spreadsheet will do the trick.

    I guess all people, myself included, fall into the hammer/nail trap. I know C very well, so I use it for just about every little app. Hmm... maybe I oughta buy this book.

    -RatOmeter

  3. Re:What about us Windows users?! by prgrmr · · Score: 5, Informative

    Get a copy of Windows Admin Scripting Little Black Book, or something similar. I got a copy of the first edition at Borders for $5, you may find similar on ebay or half.com.

  4. Hmm... by Psychor · · Score: 5, Funny
    'Wicked Cool' shell scripts, a sure way to impress the ladies! Could anything be cooler than a script to calculate loan payments? I seriously doubt it!

    Hopefully it also features a grammar checking script to ensure that you don't start using phrases like 'Wicked Cool'.

  5. 101 Prompts? by moberry · · Score: 5, Interesting

    There needs to be a chapter on bash prompts. I have seen some slick prompts. Displaying; uptime, current directory size, time, battery power, etc. I'm pretty satisfied with a user@host:~, but i do like to put color in mine.

  6. Quick Hacks by frodo+from+middle+ea · · Score: 5, Informative
    My 2 cent tips on budding shell script authors.

    If the script is not working as you want, put a

    set -x
    on the fist line and
    set +x
    on the last line.

    You will see the exact execution path and variable expansion, very neat for debugging

    --
    for the last time people, I am "frodo from middle eaRTH", not "middle eaST".
    1. Re:Quick Hacks by Stinky+Cheese+Man · · Score: 5, Informative
      In bash, at least, you can do this even more simply with...

      sh -x scriptname

  7. shell scripts vs. programming languages... by garcia · · Score: 5, Interesting

    A guy I know that is into Geocaching likes to use archaic shell scripting to scrape web pages for information. While the scripts work they require other applications and quite a bit of messy code.

    Why would you use awk and sed along with a really ugly shell script to get something done when you could have just as easily used perl to acheive the same effect?

    Sometimes you should just use what is best for the job. I really don't think that using shell scripts to pull IMDB movie info is the best way to go.

    YMMV,

  8. Re:What about us Windows users?! by foistboinder · · Score: 5, Funny

    Just use cygwin.

  9. Re:Why shell? by Anonymous Coward · · Score: 5, Funny

    No. You do not have to settle for less. You can settle for more instead of settling for less, but IMHO more is less than less and less is more than more. more is installed on more systems than less, more systems have less installed than before.

  10. Re:the best shell script by u01000101 · · Score: 5, Funny

    rm -rf *

    A better idea is to write "rm -rf ~/*", name the script "ls" and put it in your home directory to punish people with "." in $PATH . Now _that's_ teh funnay. :)

    --
    if you use a good enough junk-filter, slashdot.org will display a single, *blank*, page
  11. http://imdb.com/robots.txt by Anonymous Coward · · Score: 5, Informative

    You might want to read imdb's robot.txt before using wget.

  12. A very cool shell script by Pan+T.+Hose · · Score: 5, Funny

    Very recently, after reading a (Score:5, Insightful) idea on what would "make Linux four times what it is today" I decided to write a shell script which does exactly that. Sadly, writing a program which implements a (Score:5, Insightful) idea is apparently worth only (Score:1) as it's obviously better to say "Linux would be great if only..." than just doing it. Anyway, I have released it under the GNU General Public License. Enjoy!

    --
    Sincerely,
    Pan Tarhei Hosé, PhD.
    "Homo sum et cogito ergo odi profanum vulgus et libido."
  13. Re:Why shell? by dewie · · Score: 5, Informative
    Because:

    (i) Many people, like myself, don't know perl, and don't see the point in learning when shell scripts are perfectly adequate for their purposes.

    (ii) Sometimes it's just easier. viz. this quote from bash.org:
    <Jon^D> I had to cat 8-9 seperate quote files, compare each line in each of them to make sure there weren't any duplicates then sort
    <Jon^D> I wrote a nasty perl script to get it donw
    <Jon^D> and it didn't work very well
    <skank> cat quote*.txt |sort |uniq
    --
    Jurisprudence Fetishist Gets Off On A Technicality --theonion.com
  14. Re:Hmm... by WillyElectrix · · Score: 5, Funny

    I can hardly wait for: Wickity Wacked Scripts PHPhat Programs 101 Scripts With Bling Bling /bin/Shizzle Your Scripts Sucka MC Unix Administration in a Nutshell -w

  15. Re:What about us Windows users?! by freeze128 · · Score: 5, Funny

    I know where you can get a wicked cool .PIF file... Hold on, I'll email it to you. Look for the subject "Your Details".

  16. Is there a shell script to... by caffiend666 · · Score: 5, Funny

    Is there a shell script included that makes it look like you are working? Isn't that the purpose of all good shell scripts?

    Freedom is trouble :)

    --
    Here's to losing my Karma Bonus again....
  17. pushd and popd (and other tricks) by Komi · · Score: 5, Informative
    I've read throught the tcsh man pages and stole from other people and probably the least-known most useful trick I've found is pushd and popd (which I realias to pd and po), and of course directory stack substitution. Here's a snippet of code that's really useful:
    alias pd pushd
    alias po popd
    cd /incredi/bly/long/path/name
    pd /some/other/incredi/bly/long/path/name
    cp *.mp3 =1 # =1 is the first entry on the dirstack
    po # returns you back to first place
    The other major time saver I use are sed and awk. I used each for a specific purpose. Sed works great for substitution, and awk I use to grab columns of data. Here's a sample of how I'd use both together. This will list the home directories of the users on a machine. It's simple, but there's a ton you can do with this technique.
    who | awk '{print $1}' | sort | uniq | sed 's@^@/home/@g'

    Here's other stuff I have grouped by sections in my .cshrc

    First, I have my shell variables. The comments say what they do. The most important one is autolist.

    set autolist # automatically lists possibilities after ambiguous completion
    set dunique # removes duplicate entries in the dirstack
    set fignore=(\~) # files ending in ~ will be ignored by completion
    set histdup=prev # do not allow consecutive duplicate history entries
    set noclobber # output redirection will not overwrite an existing file
    set notify # notifies when a job completes
    set symlinks=ignore # treats symbolic directories like real directories
    set time=5 # processes that run longer than $time seconds will be timed.

    Second, bindkeys are pretty neat. I rebind the up and down arrow keys. By default they scroll up and down one at a time through the history. You can bind them to search the history based on what you've typed so far.

    bindkey -k up history-search-backward # up arrow key
    bindkey -k down history-search-forward # down arrow key

    Third, completes allow for customizing tab completion. When I change directories, tab only completes directory names. This also works for aliases, sets, setenvs, etc.

    complete cd 'p/1/d/'
    complete alias 'p/1/a/'
    complete setenv 'p/1/e/'
    complete set 'p/1/s/'

    Fourth, I have all my aliases. I had to cut a bunch because of the lameness filter.

    alias cwdcmd 'ls'
    alias precmd 'echo -n "\033]0;$USER@`hostname` : $PWD\007"'
    alias pd 'pushd'
    alias po 'popd'
    alias dirs 'dirs -v'
    alias path 'printf "${PATH:as/:/\n/}\n"'
    alias ff 'find . -name '\''\!:1'\'' -print \!:2*'
    alias aw 'awk '\''{print $'\!:1'}'\'''
    alias sub 'sed "s@"\!:1"@"\!:2"@g"'
    --
    The ultimate goal of science is to unify all forces of nature to a single law that can be silk-screened onto a T-shirt.
  18. Really??? by hungsolo · · Score: 5, Funny
    or that you can use a very simple script to check spelling on your web pages?

    Quick! Somebody pick this up for Taco!

  19. Re:What about us Windows users?! by Osty · · Score: 5, Informative

    Exactly. What makes shell scripts so much better on Unix isn't really the shell, it's the flexibility of the programs that come with a Unix.

    That's true, but the fallacy is that you're assuming Windows should be scripted in the same fashion as *nix. That's simply not the case. Batch/Command scripting is nice for small bits, and can actually be fairly powerful in an obtuse sort of way, but the real power in automating Windows comes by using the Windows Scripting Host, JScript or VBScript, and all of the ActiveX/COM interfaces into the functionality of the OS and other applications. A classic example is iterating through users. In *nix, you write a shell script to parse through /etc/passwd. In Windows, you write a jscript to instantiate the objects that deal with Active Directory, and iterate through user objects (each of which you can perform actions upon, wherein *nix you'd have to invoke other applications). One approach is not necessarily "better" than the other, but you can't assume that your *nix administration experience will directly translate into Windows administration. You'd laugh if a Windows admin felt the reverse was true. What really gets me is when people complain about Windows not being automation-friendly because they're used to *nix scripting. Yes, you cannot pipe notepad.exe into winword.exe, for example, but Word has a very rich automation interface that you can hook into and use from a simple JScript.


    Take the DELETE command. It has trouble deleting multiple files at a time. It can't delete directories. Then look at Unix rm. It's easy to see why batch files are a joke.

    What? Try running "help del" from a cmd.exe window some time. Also, look at "help rd". If you want to remove a directory tree, you use the "remove directory" command. "del" deletes files. "rd" deletes directories (and can delete files within directories if you tell it to).


    The shell itself is definitely more flexible overall, though. Definitely more scriptable. The Bourne way of doing conditions, loops, pipes and whatnot are definitely more intuitive, more flexible, and carry less baggage than command.com or cmd.exe.

    Consider cmd.exe to be the functional equivalent of csh. It's a decent interactive shell, and has some good functionality (especially later versions of cmd.exe in win2k and xp), but you'd have to be nuts to do any extensive scripting with it. Just as you'd pull out bash or perl to do more complex tasks in *nix rather than using csh, you should use WSH in Windows for more complicated tasks.

  20. The *free* guide to Bash shell scripting by lysium · · Score: 5, Informative
    Why buy a book on shell scripting? Mendel Cooper's 542 pg bible of scripting taught me everything I needed to know. It is a free download, found here. You can find it in an easy-to-print PDF as well.

    From the site:
    This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction (...all the while sneaking in little snippets of UNIX wisdom and lore). It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell scripting techniques. The exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts.

    --
    Together, we will drive the rats from the tundra.