Shell Program
时间:2006-10-18 来源:anima
Shell Program
pages 67 - 82 in Kernel Projects for Linux by G. Nutt.Details: Support for background execution, command line operators and input/output redirection Due date: Thursday 4/22/2004 by 22:00 (10:00pm).
Description of tweak, implementation details, and examples
- Your shell should be able to support command line input with the following operators:
- && (logical AND)
- command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.
- command1 && command2
- || (logical OR)
- command1 || command2
command2 is executed if, and only if, command1 returns a non-zero exit status.
- command1 || command2
- ; (semicolumn)
- command1 ; command2
commands separated by a ';' are executed sequentially; the shell waits for each command to terminate in turn.
- command1 ; command2
- & (process backgrounding)
- command1 &
If a command is terminated by the control operator `&' , the shell executes the command asynchronously in a subshell. This is known as executing the command in the background . The shell does not wait for the command to finish, and the return status is 0 (true).
- command1 &
- && (logical AND)
- Input/Output redirection: Your shell should be able to use files as input or output for commands. The redirection symbols "<" and ">" should be used in order to redirect the input and output of a command respectively. For example:
sort < test.txt
This command line will run the "sort" command and use the contents of the file "test.txt" as its input (instead of the standard input, which is usually the keyboard).
ls > files.txt
This command line will run the "ls" command and use the file "files.txt" to write its output (instead of the standard output, which is by default the terminal).
sort < test.txt &
command1 && command2 > output.txt
Note that I/O redirection is always associated with the command it follows and NOT with the whole command line.
- command_line -> ε
- command_line -> command
- command_line -> command (whitespace)+ &
- command -> (whitespace)* program
- command -> (whitespace)* program (whitespace)+ symbol (whitespace)+ command
- symbol -> ||
- symbol -> &&
- symbol -> ;
- program -> path program_name (argument)*
- program -> path program_name (argument)* > file
- program -> path program_name (argument)* < file
- program -> path program_name (argument)* > file < file
- program -> path program_name (argument)* < file > file
- path -> ε
- path -> ./ (dir/)*
- path -> / (dir/)*
- argument -> ε
- argument -> (whitespace)+ argument
In the above grammar "ε" means "empty string", (A)* means "A repeated 0 or more times", and (A)+ means "at least one A".
- "No such file or directory" - when a file doesn't exist e.g. when it supposed to be used for stdin redirection
- "Permission denied" - open fails for a redirection
- "Syntax error" - when the command line is not recognized as legitimate input
- "Command not found" - when the user typed a command that is not found in the PATH
# find /bin -name ls -print
# cd /etc
# ls && cal
# ls > test.txt
# sort < test.txt
# ls -l /bin || find /lib > test2.txt
# ls -l /bin ; ps -ef ; sort -nr < test2.txt
# find /lib > /tmp/find.txt &
(From URL: http://www.cs.ucla.edu/~pefstath/cs111/p1s04.htm)