Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Complex Commands

You can use the who command to gather information about yourself when you execute it as follows:

$ who am i
ranga      pts/0        Dec  9 08:49
$

This tells me the following information:

  My username is ranga.
  I am logged in to the terminal pts/0.
  I logged in at 8:49 on Dec 9.


This command also introduces the concept of a complex command, which is a command that consists of a command name and a list of arguments.


Arguments are command modifiers that change the behavior of a command. In this case, the command name is who, and the arguments are am and i.


When the who command runs as a simple command, it displays information about everyone who is logged in to a UNIX system. The output that is generated when a command runs as a simple command is called the default behavior of that command.

The arguments am and i change the behavior of the who command to list information about you only. In UNIX, most commands accept arguments that modify their behavior.

The formal syntax for a complex command is:

$ command argument1 argument2 argument3 ... argumentN

Here, command is the name of the command you want to execute, and argument1 through argumentN are the arguments you want to give command.

Compound Commands

One of the most powerful features of UNIX is the capability to combine simple and complex commands together to obtain compound commands.


A compound command consists of a list of simple and complex commands separated by the semicolon character (;). An example of a complex command is
$ date ; who am i ;
Wed Dec  9 10:10:10 PST 1998
ranga      pts/0        Dec  9 08:49
$

Here, the compound command consists of the simple command date and the complex command who am i. As you can see from the output, the date command executes first, followed by the who am i command. When you give a compound command, each of the individual commands that compose it execute in order.

In this example, the complex command behaves as if you typed the commands in the following order:

$ date
Wed Dec  9 10:25:34 PST 1998
$ who am i
ranga      pts/0        Dec  9 08:49
$

The main difference between executing commands in this fashion and using a complex command is that in a complex command you do not get the prompt back between the two commands.

The formal syntax for a complex command is:

$ command1 ; command2 ; command3 ; ... ; commandN ;

Here, command1 through commandN are either simple or complex commands. The order of execution is command1, followed by command2, followed by command3, and so on. When commandN finishes executing, the prompt returns.

Command Separators


The semicolon character (;) is treated as a command separator, which indicates where one command ends and another begins.

If you don’t use it to separate each of the individual commands in a complex command, the computer will not be able to tell where one command ends and the next command starts. If you execute the previous example without the first semicolon

$ date who am i

an error message similar to the following will be produced:

date: bad conversion

Here, the date command thinks that it is being run as a complex command with the arguments who, am, and i. The date command is confused by these arguments and displays an error message. When using complex commands, remember to use the semicolon character.

You can also terminate individual simple and complex commands using the semicolon character. For example, the commands

$ date

and$ date ;

produce the same output due to the order in which commands execute.

In the first case, the simple command date executes, and the prompt returns.

In the second case, the computer thinks that a complex command is executing. It begins by executing the first command in the complex command. In this case, it is the date command. When this command finishes, the computer tries to execute the next command. Because no other commands are left to execute, the prompt returns.


Note:  
You will frequently see the semicolon used to terminate simple and complex commands in scripts.Because the semicolon is required to terminate commands in other languages, such as C, Perl, and Java, many script programmers use it the same way in scripts. No extra overhead is incurred by using the semicolon in this manner.


Previous Table of Contents Next