|
Command SummaryThis section summarizes the shell's built-in commands. Actually, some of these commands (such as echo and test) may not be built in to the shell but must be provided as a utility by a POSIX-compliant system. They are built in to Bash and the Korn shell and are so often used in shell scripts that we decided to list them here anyway. The following commands are organized alphabetically for easy reference. The : CommandGeneral Format: : This is essentially a null command. It is frequently used to satisfy the requirement that a command appear. Example
The : command returns an exit status of zero. The . CommandGeneral Format: . file The "dot" command causes the indicated file to be read and executed by the shell, just as if the lines from the file were typed at that point. Note that file does not have to be executable, only readable. Also, the shell uses the PATH variable to find file. Example
The preceding command causes the shell to search the current PATH for the file progdefs. When it finds it, it reads and executes the commands from the file. Note that because file is not executed by a subshell, variables set and/or changed within file remain in effect after execution of the commands in file is complete. The alias CommandGeneral Format: alias name=string [name=string ...] The alias command assigns string to the alias name. Whenever name is used as a command, the shell substitutes string, performing command-line substitution after string is in place. Examples
If an alias ends with a blank, the word following the alias is also checked to see whether it's an alias. The format
causes the alias for name to be printed out. alias with no arguments lists all aliases. alias returns an exit status of zero unless a name is given (as in alias name) for which no alias has been defined. The bg CommandGeneral Format: bg job_id If job control is enabled, the job identified by job_id is put into the background. If no argument is given, the most recently suspended job is put into the background. Example
The break CommandGeneral Format: break Execution of this command causes execution of the innermost for, while, or until loop to be immediately terminated. Execution continues with the commands that immediately follow the loop. If the format
is used, where n is an integer greater than or equal to 1, execution of the n innermost loops is automatically terminated. The case CommandGeneral Format:
The word value is successively compared against patl, pat2, ..., patn until a match is found. The commands that appear immediately after the matching pattern are then executed until a double semicolon (;;) is encountered. At that point, execution of the case is terminated. If no pattern matches value, none of the commands inside the case are executed. The pattern * matches anything and is often used as the last pattern in a case as the "catchall" case. The shell metacharacters * (match zero or more characters), ? (match any single character), and [...] (match any single character enclosed between the brackets) can be used in patterns. The character | can be used to specify a logical ORing of two patterns, as in patl | pat2 which means to match either patl or pat2. Examples
The cd CommandGeneral Format: cd directory Execution of this command causes the shell to make directory the current directory. If directory is omitted, the shell makes the directory specified in the HOME variable the current directory. If the shell variable CDPATH is null, directory must be a full directory path (for example, /users/steve/documents) or relative to the current directory (for example, documents, ../pat). If CDPATH is non-null and directory is not a full path, the shell searches the colon-delimited directory list in CDPATH for a directory containing directory. Examples
An argument of - causes the shell to make the previous directory the current directory. The pathname of the new current directory is printed out. Examples
The cd command sets the shell variable PWD to the new current directory, and OLDPWD to the previous directory. The continue commandGeneral Format: continue Execution of this command from within a for, while, or until loop causes any commands that follow the continue to be skipped. Execution of the loop then continues as normal. If the format
is used, the commands within the n innermost loops are skipped. Execution of the loops then continue as normal. The echo CommandGeneral Format: echo args This command causes args to be written to standard output. Each word from args is delimited by a blank space. A newline character is written at the end. If args is omitted, the effect is to simply skip a line. Certain backslashed characters have a special meaning to echo as shown in Table A.8.
Remember to quote these characters so that the echo command interprets them and not the shell. Examples
The eval CommandGeneral Format: eval args Execution of this command causes the shell to evaluate args and then execute the results. This is useful for causing the shell to effectively "double-scan" a command line. Example
The exec CommandGeneral Format: exec command args When the shell executes the exec command, it initiates execution of the specified command with the indicated arguments. Unlike other commands executed as a new process, command replaces the current process (that is, no new process is created). After command starts execution, there is no return to the program that initiated the exec. If just I/O redirection is specified, the input and/or output for the shell is accordingly redirected. Examples
The exit CommandGeneral Format: exit n Execution of exit causes the current shell program to be immediately terminated. The exit status of the program is the value of the integer n, if supplied. If n is not supplied, the exit status is that of the last command executed prior to the exit. An exit status of zero is used by convention to indicate "success," and nonzero to indicate "failure" (such as an error condition). This convention is used by the shell in evaluation of conditions for if, while, and until commands, and with the && and || constructs. Examples
Note that executing exit from a login shell has the effect of logging you off. The export CommandGeneral Format: export variables The export command tells the shell that the indicated variables are to be marked as exported; that is, their values are to be passed down to subshells. Examples
Variables may be set when exported using the form
So lines such as
can be rewritten as
The output of export with a �p argument is a list of the exported variables and their values in the form
or
if variable has been exported but not yet set. The false CommandGeneral Format: false The false command simply returns a nonzero exit status. The fc CommandGeneral Format: fc -e editor -lnr first last
The fc command is used to edit commands in the command history. A range of commands is specified from first to last, where first and last can be either command numbers or strings; a negative number is taken as an offset from the current command number; a string specifies the most recently entered command beginning with that string. The commands are read into the editor and executed upon exit from the editor. If no editor is specified, the value of the shell variable FCEDIT is used; if FCEDIT is not set, ed is used. The -l option lists the commands from first to last (that is, an editor is not invoked). If the -n option is also selected, these commands are not preceded by command numbers. The -r option to fc reverses the order of the commands. If last is not specified, it defaults to first. If first is also not specified, it defaults to the previous command for editing and to -16 for listing. The �s option causes the selected command to be executed without editing it first. The format
causes the command first to be re-executed after the string old in the command is replaced with new. If first isn't specified, the previous command is used, and if old=new isn't specified, the command is not changed. Examples
The fg CommandGeneral Format: fg job_id If job control is enabled, the job specified by job_id is brought to the foreground. If no argument is given, the most recently suspended job, or the job last sent to the background is brought to the foreground. Example
The for CommandGeneral Format:
Execution of this command causes the commands enclosed between the do and done to be executed as many times as there are words listed after the in. The first time through the loop, the first word�wordl�is assigned to the variable var and the commands between the do and done executed. The second time through the loop, the second word listed�word2�is assigned to var and the commands in the loop executed again. This process continues until the last variable in the list�wordn�is assigned to var and the commands between the do and done executed. At that point, execution of the for loop is terminated. Execution then continues with the command that immediately follows the done. The special format
indicates that the positional parameters "$1", "$2", ... are to be used in the list and is equivalent to
Example
The getopts CommandGeneral Format: getopts options var This command processes command-line arguments. options is a list of valid single letter options. If any letter in options is followed by a :, that option takes a following argument on the command line, which must be separated from the option by at least one whitespace character. Each time getopts is called, it processes the next command-line argument. If a valid option is found, getopts stores the matching option letter inside the specified variable var and returns a zero exit status. If an invalid option is specified (that is, one not listed in options), getopts stores a ? inside var and returns with a zero exit status. It also writes an error message to standard error. If an option takes a following argument, getopts stores the matching option letter inside var and stores the following command-line argument inside the special variable OPTARG. If no arguments are left on the command line, getopts stores a ? inside var and writes an error message to standard error. If no more options remain on the command line (that is, if the next command-line argument does not begin with a -), getopts returns a nonzero exit status. The special variable OPTIND is also used by getopts. It is initially set to 1 and is adjusted each time getopts returns to indicate the number of the next command-line argument to be processed. The argument -- can be placed on the command line to specify the end of the command-line arguments. getopts supports stacked arguments, as in
which is equivalent to
Options that take following arguments may not be stacked. If the format
is used, getopts parses the arguments specified by args rather than the command-line arguments. Example
The hash CommandGeneral Format: hash commands This command tells the shell to look for the specified commands and to remember what directories they are located in. If commands is not specified, a list of the hashed commands is displayed. If the format
is used, the shell removes all commands from its hash list. Next time any command is executed, the shell uses its normal search methods to find the command. Examples
The if CommandGeneral Format:
commandt is executed and its exit status tested. If it is zero, the commands that follow up to the fi are executed. Otherwise, the commands that follow up to the fi are skipped. Example
If the grep returns an exit status of zero (which it will if it finds $sys in the file sysnames), the echo command is executed; otherwise it is skipped. The built-in command test is often used as the command following the if. Example
An else clause can be added to the if to be executed if the command returns a nonzero exit status. In this case, the general format of the if becomes
If commandt returns an exit status of zero, the commands that follow up to the else are executed, and the commands between the else and the fi are skipped. Otherwise, commandt returns a nonzero exit status and the commands between the then and the else are skipped, and the commands between the else and the fi are executed. Example
In the preceding example, if line has zero length, the echo command that displays the message I couldn't find $name is executed; otherwise, the echo command that displays the value of line is executed. A final format of the if command is useful when more than a two-way decision has to be made. Its general format is
commandl, command2, ..., commandn are evaluated in order until one of the commands returns an exit status of zero, at which point the commands that immediately follow the then (up to another elif, else, or fi) are executed. If none of the commands returns an exit status of zero, the commands listed after the else (if present) are executed. Example
The jobs CommandGeneral Format: jobs The list of active jobs is printed. If the -l option is specified, detailed information about each job, including its process id, is listed as well. If the -p option is specified, only process ids are listed. If an optional job_id is supplied to the jobs command, just information about that job is listed. Example
The kill CommandGeneral Format: kill -signal job The kill command sends the signal signal to the specified process, where job is a process id number or job_id, and signal is a number or one of the signal names specified in <signal.h> (see the description of trap later in the chapter). kill �l lists these names. A signal number supplied with the �l option lists the corresponding signal name. A process id used with the �l option lists the name of the signal that terminated the specified process (if it was terminated by a signal). The �s option can also be used when a signal name is supplied, in which case the dash before the name is not used (see the following example). If signal isn't specified, TERM is used. Examples
Note that more than one process id can be supplied to the kill command on the command line. The newgrp CommandGeneral Format: newgrp group This command changes your real group id (GID) to group. If no argument is specified, it changes you back to your default group. Examples
If a password is associated with the new group, and you are not listed as a member of the group, you will be prompted to enter it. newgrp �l changes you back to your login group. The pwd CommandGeneral Format: pwd This command tells the shell to print your working directory, which is written to standard output. Examples
The read CommandGeneral Format: read vars This command causes the shell to read a line from standard input and assign successive whitespace-delimited words from the line to the variables vars. If fewer variables are listed than there are words on the line, the excess words are stored in the last variable. Specifying just one variable has the effect of reading and assigning an entire line to the variable. The exit status of read is zero unless an end-of-file condition is encountered. Examples
Note in the final example that any leading whitespace characters get "eaten" by the shell when read. You can change IFS if this poses a problem. Also note that backslash characters get interpreted by the shell when you read the line, and any that make it through (double backslashes will get through as a single backslash) get interpreted by echo if you display the value of the variable. A �r option to read says to not treat a \character at the end of a line as line continuation. The readonly CommandGeneral Format: readonly vars This command tells the shell that the listed variables cannot be assigned values. These variables may be optionally assigned values on the readonly command line. If you subsequently try to assign a value to a readonly variable, the shell issues an error message. readonly variables are useful for ensuring that you don't accidentally overwrite the value of a variable. They're also good for ensuring that other people using a shell program can't change the values of particular variables (for example, their HOME directory or their PATH). The readonly attribute is not passed down to subshells. readonly with a �p option prints a list of your readonly variables. Example
The return CommandGeneral Format: return n This command causes the shell to stop execution of the current function and immediately return to the caller with an exit status of n. If n is omitted, the exit status returned is that of the command executed immediately prior to the return. The set CommandGeneral Format: set options args This command is used to turn on or off options as specified by options. It is also used to set positional parameters, as specified by args. Each single letter option in options is enabled if the option is preceded by a minus sign (-), or disabled if preceded by a plus sign (+). Options can be grouped, as in
which enables the f and x options. Table A.9 summarizes the options that can be selected.
Shell modes are turned on or off by using the -o and +o options, respectively, followed by an option name. These options are summarized in Table A.10.
The command set -o without any following options has the effect of listing all shell modes and their settings. The shell variable $- contains the current options setting. Each word listed in args is set to the positional parameters $1, $2, ..., respectively. If the first word might start with a minus sign, it's safer to specify the -- option to set to avoid interpretation of that value. If args is supplied, the variable $# will be set to the number of parameters assigned after execution of the command. Examples
The shift CommandGeneral Format: shift This command causes the positional parameters $1, $2, ..., $n to be "shifted left" one place. That is, $2 is assigned to $1, $3 to $2, ..., and $n to $n-1. $# is adjusted accordingly. If the format
is used instead, the shift is to the left n places. Examples
The test CommandGeneral Format:
or
The shell evaluates condition and if the result of the evaluation is TRUE, returns a zero exit status. If the result of the evaluation is FALSE, a nonzero exit status is returned. If the format [ condition ] is used, a space must appear immediately after the [ and before the ]. condition is composed of one or more operators as shown in Table A.11. The -a operator has higher precedence than the -o operator. In any case, parentheses can be used to group subexpressions. Just remember that the parentheses are significant to the shell and so must be quoted. Operators and operands (including parentheses) must be delimited by one or more spaces so that test sees them as separate arguments. test is often used to test conditions in an if, while, or until command. Examples
The times CommandGeneral Format: times Execution of this command causes the shell to write to standard output the total amount of time that has been used by the shell and by all its child processes. For each, two numbers are listed: first the accumulated user time and then the accumulated system time. Note that times does not report the time used by built-in commands. Example
The trap CommandGeneral Format: trap commands signals This command tells the shell to execute commands whenever it receives one of the signals listed in signals. The listed signals can be specified by name or number. trap with no arguments prints a list of the current trap assignments. If the first argument is the null string, as in
the signals in signals are ignored when received by the shell. If the format
is used, processing of each signal listed in signals is reset to the default action. Examples
Table A.12 lists values that can be specified in the signal list.
The shell scans commands when the trap command is encountered and again when one of the listed signals is received. This means, for example, that when the shell encounters the command
it substitutes the value of count at that point, and not when one of the signals is received. You can get the value of count substituted when one of the signals is received if you instead enclose the commands in single quotes:
The true CommandGeneral Format: true This command returns a zero exit status. The type CommandGeneral Format: type commands This command prints information about the indicated commands. Examples
The umask CommandGeneral Format: umask mask umask sets the default file creation mask to mask. Files that are subsequently created are ANDed with this mask to determine the mode of the file. umask with no arguments prints the current mask. The �S option says to produce symbolic output. Examples
The unalias CommandGeneral Format: unalias names The alias's names are removed from the alias list. The �a option says to remove all aliases. The unset CommandGeneral Format: unset names This causes the shell to erase definitions of the variables or functions listed in names. Read-only variables cannot be unset. The �v option to unset specifies that a variable name follows, whereas the �f option specifies a function name. If neither option is used, it is assumed that variable name(s) follow. Example
The until CommandGeneral Format:
commandt is executed and its exit status tested. If it is nonzero, the commands enclosed between the do and done are executed. Then commandt is executed again and its status tested. If it is nonzero, the commands between the do and done are once again executed. Execution of commandt and subsequent execution of the commands between the do and done continues until commandt returns a zero exit status, at which point the loop is terminated. Execution then continues with the command that follows the done. Note that because commandt gets evaluated immediately on entry into the loop, the commands between the do and done may never be executed if it returns a zero exit status the first time. Example
The preceding loop continues until the grep returns a zero exit status (that is, finds jack in who's output). At that point, the loop is terminated, and the echo command that follows is executed. The wait CommandGeneral Format: wait job This command causes the shell to suspend its execution until the process identified as job finishes executing. Job can be a process id number or a job_id. If job is not supplied, the shell waits for all child processes to finish executing. If more than one process id is listed, wait will wait for them all to complete. wait is useful for waiting for processes to finish that have been sent to the background for execution. Example
The variable $! can be used to obtain the process id number of the last process sent to the background. The while CommandGeneral Format:
commandt is executed and its exit status tested. If it is zero, the commands enclosed between the do and done are executed. Then commandt is executed again and its status tested. If it is zero, the commands between the do and done are once again executed. Execution of commandt and subsequent execution of the commands between the do and done continues until commandt returns a nonzero exit status, at which point the loop is terminated. Execution then continues with the command that follows the done. Note that because commandt gets evaluated immediately on entry into the loop, the commands between the do and done may never be executed if it returns a nonzero exit status the first time. Example
|
|
No comments:
Post a Comment