home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Unix Power ToolsUnix Power ToolsSearch this book

Chapter 35. Shell Programming for the Uninitiated

35.1. Writing a Simple Shell Program

A shell script need be no more than a command line saved in a file. For example, let's assume that you'd like a compact list of all the users who are currently logged in on the system.

A command like this might do the trick:

% who | cut -c1-8 | sort -u | pr -l1 -8 -w78 -t

A list of logged-in users should come out in columns, looking something like this:

abraham  appleton biscuit  charlie  charlott fizzie   howard   howie
hstern   jerry    kosmo    linda    ocshner  peterson root     ross
sutton   yuppie

We used four Unix commands joined with pipes:

  1. who (Section 2.8) gives a list of all users.

  2. cut -c1-8 (Section 21.14) outputs columns 1-8 of the who output -- the usernames.

  3. sort -u (Section 22.6) puts names in order and takes out names of users who are logged on more than once.

  4. pr -l1 -8 -w78 -t (Section 21.15, Section 45.6) takes the list of usernames, one per line, and makes it into 8 columns on 78-character-wide lines. (The -l1 is the lowercase letter L followed by the digit 1.)

If you wanted to do this frequently, wouldn't it be better if all you had to do was type something like:

% loggedin

to get the same result? Here's how:

  1. Start a text editor on a new file named loggedin.

  2. If your system supports the special #! notation (Section 36.2) (and it probably does), the first line of the script file should be:


    Otherwise, leave the first line blank. (When the first line of a script is blank, most shells will start a Bourne shell to read it. Section 36.2 has more information.)

    I think that the second line of a shell script should always be a comment to explain what the script does. (Use more than one line, if you want.) A comment starts with a hash mark (#); all characters after it on the line are ignored. Oh, and try to make sure there's a bit of whitespace between the comment character and the actual comment; that's a pet peeve of mine:

    # loggedin - list logged-in users, once per user, in 8 columns

    Put this on the third line, just like you did on the command line:

    who | cut -c1-8 | sort -u | pr -l1 -8 -w78 -t
  3. Save the file and leave the editor. You've just written a shell script.

  4. Next, you need to make the shell script executable. The chmod (Section 50.5) (change mode) command is used to change permissions on a file. The plus sign followed by an x (+x) makes the file executable:

    % chmod +x loggedin
  5. If your login shell (Section 3.4) is csh or tcsh, you'll need to reset its command search table. To do that, type:

    rehash Section 27.6

    % rehash
  6. Finally, try the script. Just type its name and it should run:

    % loggedin

    If that doesn't run, your current directory may not be in your shell's command search path (Section 35.6, Section 35.7). In that case, try this:

    % ./loggedin

    If it still doesn't work, and you started the first line of your script with #!, be sure that the Bourne shell's pathname on that line (like /bin/sh) is correct. Another common error is to swap the # and !, so check that, too. You should get an error like this, if that is the problem, although the script may itself run as well, depending on your system:

    !#/bin/sh: No such file or directory
  7. If you want to run the script from somewhere other than the current directory, or if you want other programs and scripts you write to be able to use it, you need to put it in a directory that's in your search path and/or change your search path (Section 27.6). If you're the only person who plans to use the script, you should put it in your personal bin directory (Section 7.4). Otherwise, you might ask your system administrator if there's a systemwide directory for local commands.

-- JP

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.