A shell script need be no more than a complex 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:
who
(51.4)
gives a list of all users.
cut -c1-8
(35.14)
outputs columns 1-8 of the who output - the
usernames.
If your system doesn't have cut, use the command
colrm 9
(35.15).
The
sort -u
(36.6)
puts names in order and takes out names of users
who are logged on more than once.
The
pr -l1 -8 -w78 -t
(35.17, 43.7)
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:
Start your favorite text editor (Emacs, vi, whatever) on a new file named loggedin.
If your system supports the
special #!
notation (44.4),
the first line of the script file should be:
#!/bin/sh
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. Articles 45.2 and 45.6 have 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:
# 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
(As I explained earlier, you might need colrm instead of cut.)
Save the file and leave the editor. You've just written a shell script.
Next, you need to make the shell script executable.
The
chmod (22.7)
(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
If your account uses the C shell, you'll need to reset its command search table. To do that, type:
rehash |
% |
---|
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. 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.
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 (8.7). If you're the only person who plans to use the script, you should put it in your personal bin directory (4.2). Otherwise, you might ask your system administrator if there's a systemwide directory for local commands.
-