< 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 StatusAfter 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
7.3.2 Multiple Commands at the Command LineA 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 CommandsCommands 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 CommandsWith 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 BackgroundNormally, 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
|
< Day Day Up > |