Creating New Commands

Earlier it was shown how to find out who is using the system using who and how to count the lines in a file by using wc. So, the number of people using the network can be found by

echo -n "Users logged in :" ; who | wc -l
Users logged in : 182

A semi-colon (;) allows several commands on one line.

This is a bit too long winded to use very often! The easiest thing to do here would be to create an alias for the above command. However, as a simple example, a new command, called howmany perhaps, could be created in its own right to perform the same function. This could be done by creating a command file or shell script, using any text editor, containing this sequence (see also section 7 on Bourne Shell Programming Structures for more detailed information about shell scripts).

The file howmany, containing the above composite command, can be executed by typing the filename. However, the file must first be set to have execute permission.

% ls -l howmany
-rw-r--r-- 1 mary 40 Jan 27 16:17 howmany

The file permission can be reset by using the command chmod u+x howmany. The command can now be run, however, the filename must be preceded with the characters ./ (leaving no space) .

% ./howmany

This is necessary until the command has been placed in a directory that Unix has been asked to search for executables. Conventionally new user-created commands are placed in the user's bin directory.