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:

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.