7.3. The Command Line

 <  Day Day Up  >  

After logging on, the shell displays its primary prompt, a dollar sign by default. The shell is your command interpreter. When the shell is running interactively, it reads commands from the terminal and breaks the command line into words. A command line consists of one or more words (tokens), separated by whitespace (blanks and/or tabs), and terminated with a newline, which is generated by pressing Enter. The first word is the command and subsequent words are the command's arguments. The command may be a UNIX executable program such as ls or pwd , a built-in command such as cd or test , or a shell script. The command may contain special characters , called metacharacters, that the shell must interpret while parsing the command line. If a command line is long and you want to continue typing on the next line, the backslash character, followed by a newline, will allow you to continue typing on the next line. The secondary prompt will appear until the command line is terminated.

7.3.1 The Exit Status

After a command or program terminates, it returns an exit status to the parent process. The exit status is a number between 0 and 255. By convention, when a program exits, if the status returned is 0, the command was successful in its execution. When the exit status is nonzero, the command failed in some way. The shell status variable, ? , is set to the value of the exit status of the last command that was executed. Success or failure of a program is determined by the programmer who wrote the program.

Example 7.8.
 1   $ grep "john" /etc/passwd  john:MgVyBsZJavd16s:9496:40:John Doe:/home/falcon/john:/bin/sh  2   $  echo $?    3   $ grep  "nicky" /etc/passwd  4   $  echo $?   1  5   $ grep  "scott" /etc/passsswd  grep:  /etc/passsswd: No such file or directory  6   $  echo $?   2  

EXPLANATION

  1. The grep program searches for the pattern john in the /etc/passwd file and is successful. The line from /etc/passwd is displayed.

  2. The ? variable is set to the exit value of the grep command. Zero indicates success.

  3. The grep program cannot find user nicky in the /etc/passwd file.

  4. If the grep program cannot find the pattern, it returns an exit status of 1.

  5. The grep program fails because the /etc/passsswd file cannot be opened.

  6. If grep cannot find the file, it returns an exit status of 2.

7.3.2 Multiple Commands at the Command Line

A command line can consist of multiple commands. Each command is separated by a semicolon, and the command line is terminated with a newline.

Example 7.9.
 $  ls; pwd; date  

EXPLANATION

The commands are executed from left to right, one after the other, until the newline is reached.

Grouping Commands

Commands may also be grouped so that all of the output is either piped to another command or redirected to a file.

Example 7.10.
 $  (ls ; pwd; date) > outputfile  

EXPLANATION

The output of each of the commands is sent to the file called outputfile . The spaces inside the parentheses are necessary.

7.3.3 Conditional Execution of Commands

With conditional execution, two command strings are separated by the special metacharacters, double ampersands ( && ) and double vertical bars ( ). The command on the right of either of these metacharacters will or will not be executed based on the exit condition of the command on the left.

Example 7.11.
 $  cc prgm1.c o prgm1 && prgm1  

EXPLANATION

If the first command is successful (has a 0 exit status), the command after the && is executed; that is, if the cc program can successfully compile prgm1.c , the resulting executable program, prgm1 , will be executed.

Example 7.12.
 $  cc prog.c 2> err  mail bob < err  

EXPLANATION

If the first command fails (has a nonzero exit status), the command after the is executed; that is, if the cc program cannot compile prog.c , the errors are sent to a file called err , and user bob will be mailed the err file.

7.3.4 Commands in the Background

Normally, when you execute a command, it runs in the foreground, and the prompt does not reappear until the command has completed execution. It is not always convenient to wait for the command to complete. By placing an ampersand ( & ) at the end of the command line, the shell will return the shell prompt immediately and execute the command in the background concurrently. You do not have to wait to start up another command. The output from a background job will be sent to the screen as it processes. Therefore, if you intend to run a command in the background, the output of that command might be redirected to a file or piped to another device, such as a printer, so that the output does not interfere with what you are doing.

The $! variable contains the PID number of the last job put in the background.

Example 7.13.
 1   $  man sh  lp&  2  [1] 1557  3   $  kill -9 $!  

EXPLANATION

  1. The output of the man command (the manual pages for the sh command) is piped to the printer. The ampersand at the end of the command line puts the job in the background.

  2. There are two numbers that appear on the screen: the number in square brackets indicates that this is the first job to be placed in the background; the second number is the PID, or the process identification number of this job.

  3. The shell prompt appears immediately. While your program is running in the background, the shell is waiting for another command in the foreground. The ! variable evaluates to the PID of the job most recently put in the background. If you get it in time, you will kill this job before it goes to the print queue.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net