Unix Shell Programming Third Edition phần 9 ppt

69 240 0
Unix Shell Programming Third Edition phần 9 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

$ In addition to the preceding substitutions, the shell also checks for a tilde after a colon (:) and performs tilde substitution on that as well (for PATH interpretation). Order of Search It's worthwhile listing the order of searching the shell uses when you type a command name: The shell first checks to see whether the command is a reserved word (such as for and do).1. If it's not a reserved word and is not quoted, the shell next checks its alias list, and if it finds a match, performs the substitution. If the alias definition ends in a space, it attempts alias substitution on the next word. The final result is then checked against the reserved word list, and if it's not a reserved word, the shell proceeds to step 3. 2. Next, the shell checks the command against its function list and executes it if found.3. The shell checks to see whether the command is a built-in command (such as cd and pwd).4. Finally, the shell searches the PATH to locate the command.5. If the command still isn't found, a "command not found" error message is issued.6. Compatibility Summary Table 15.4 summarizes the compatibility of the POSIX standard shell, the Korn shell, and Bash with the features described in this chapter. In this table, an "X" denotes a supported feature, "UP," an optional feature in the POSIX shell (these are also known as "User Portability" features in the POSIX shell specification), and "POS," a feature supported only by Bash when it is invoked with the name sh or with the posix command-line option, or after set –o posix is executed. Table 15.4. POSIX Shell, Korn Shell, and Bash Compatibility POSIX Shell Korn Shell Bash ENV file X X POS vi line edit mode X X X emacs line edit mode X X fc command X X X r command X !! X !string X Functions X X X local variables X X autoload via FPATH X Integer expressions with (( )) X X Integer data type X X integers in different bases X X 0xhexnumber, 0octalnumber X Aliases UP X X Arrays X X Job control UP X X cd - X X X cd old new X POSIX Shell Korn Shell Bash ~username, ~/ X X X ~+, ~- X X ~username, ~/ X X X ~+, ~- X X Exercises 1: Using only shell built-in commands, write a function that prints all filenames in a specified directory hierarchy. Its output should be similar to the output of the find command: $ myfind /users/pat /users/pat /users/pat/bin /users/pat/bin/ksh /users/pat/bin/lf /users/pat/bin/pic /users/pat/chapt1 /users/pat/chapt1/intro /users/pat/rje /users/pat/rje/file1 (Hint: Bash and Korn shell functions can be recursive.) 2: Write a shell function called octal that converts octal numbers given as command-line arguments to decimal numbers and prints them out, one per line: $ octal 10 11 12 8 9 10 $ (Hint for Korn shell users: If you assign a decimal number to a variable when it's declared—for example, typeset –i d=10#0—assignments to this variable from other bases are converted to decimal first.) 3: Modify the cdh function to filter out multiple occurrences of the same directory; for example: $ cdh –l 0 /users/pat $ cdh $ cdh $ cdh –l 0 /users/pat $ 4: Modify the cdh function to set the prompt (PS1) to show the current directory; for example: /users/pat: cdh /tmp /tmp: cdh /users/pat: 5: Modify the cdh function to allow the user to specify a partial name of a directory in the history file preceded by a dash: /etc: cdh –l 0 /users/pat 1 /tmp 2 /users/steve 3 /usr/spool/uucppublic 4 /usr/local/bin 5 /etc /etc: cdh –pub /usr/spool/uucppublic: cdh –bin /usr/local/bin: 6: (Bash users only) Add the Korn shell's cd old new feature to the cdh function. Appendix A. Shell Summary IN THIS APPENDIX Startup Commands Comments Parameters and Variables Command Re-entry Quoting Filename Substitution I/O Redirection Exported Variables and Subshell Execution Functions Job Control Command Summary This appendix summarizes the main features of the standard POSIX shell as per IEEE Std 1003.1- 2001. Startup The shell can be given the same options on the command line as can be specified with the set command. In addition, the following options can be specified: -c commands commands are executed. -i The shell is interactive. Signals 2, 3, and 15 are ignored. -s Commands are read from standard input. Commands The general format of a command typed to the shell is command arguments where command is the name of the program to be executed, and arguments are its arguments. The command name and the arguments are delimited by whitespace characters, normally the space, tab, and newline characters (changing the variable IFS affects this). Multiple commands can be typed on the same line if they're separated by semicolons (;). Every command that gets executed returns a number known as the exit status; zero is used to indicate success, and nonzero indicates a failure. The pipe symbol | can be used to connect the standard output from one command to the standard input of another, as in who | wc -l The exit status is that of the last command in the pipeline. Placing a ! at the beginning of the pipeline causes the exit status of the pipeline to be the logical negation of the last command in the pipeline. If the command sequence is terminated by an ampersand character (&),it is run asynchronously in the background. The shell displays the process id number and job id of the command at the terminal. Typing of a command can continue to the next line if the last character on the line is a backslash character (\). The characters && cause the command that follows to be executed only if the preceding command returns a zero exit status. The characters || cause the command that follows to be executed only if the preceding command returns a nonzero exit status. As an example, in who | grep "fred" > /dev/null && echo "fred's logged on" the echo is executed only if the grep returns a zero exit status. Comments If a word begins with the character #, the shell treats the remainder of the line as a comment and simply ignores it. [...]... standard error Exported Variables and Subshell Execution Commands other than the shell' s built-in commands are normally executed in a "new" shell, called a subshell Subshells cannot change the values of variables in the parent shell, and they can only access variables from the parent shell that were exported to them—either implicitly or explicitly—by the parent If the subshell changes the value of one of... If the monitor option of the set command is turned on, the shell prints a message when each job finishes If you still have jobs when you try to exit the shell, a message is printed to alert you of this If you immediately try to exit again, the shell exits The monitor option is enabled by default for interactive shells Stopping Jobs If the shell is running on a system with job control, and the monitor... brought to the foreground When the shell exits, all stopped jobs are killed Command Summary This 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 POSIXcompliant system They are built in to Bash and the Korn shell and are so often used in shell scripts that we decided to list... echo "Unknown option";; esac case $choice in [1 -9] ) valid=TRUE;; *) echo "Please choose a number from 1 -9" ;; esac The cd Command General 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... MAIL The name of a file that the shell periodically checks for the arrival of mail If new mail arrives, the shell displays a You have mail message See also MAILCHECK and MAILPATH MAILCHECK The number of seconds specifying how often the shell is to check for the arrival of mail in the file in MAIL or in the files listed in MAILPATH The default is 600 A value of 0 causes the shell to check before displaying...Parameters and Variables There are three different "types" of parameters: shell variables, special parameters, and positional parameters Shell Variables A shell variable name must start with an alphabetic or underscore (_) character, and can be followed by any number of alphanumeric or underscore characters Shell variables can be assigned values on the command line by writing: variable=value... of these variables and wants to have its own subshells know about it, it must explicitly export the variable before executing the subshell When the subshell finishes execution, any variables that it may have set are inaccessible by the parent The ( ) Construct If one or more commands are placed inside parentheses, those commands will be executed in a subshell The { ; } Construct If one or more commands... redirected to the file errors More on Shell Variables A shell variable can be placed into the environment of a command by preceding the command name with the assignment to the parameter on the command line, as in PHONEBOOK=$HOME/misc/phone rolo Here the variable PHONEBOOK will be assigned the indicated value and then placed in rolo's environment The environment of the current shell remains unchanged, as if... current shell (functions can't be exported) The function definition can span as many lines as necessary A return command can be executed to cause execution of the function to be terminated without also terminating the shell (see the return command description) For example, nf () { ls | wc -l; } defines a function called nf to count the number of files in your current directory Job Control Shell Jobs... operators sizeof, ++, and may be available in your shell implementation but are not required by the standard Examples y=$((22 * 33)) z=$((y * y / (y - 1))) Filename Substitution After parameter substitution (and command substitution) is performed on the command line, the shell looks for the special characters *, ?, and [ If they're not quoted, the shell searches the current directory, or another directory . command-line option, or after set –o posix is executed. Table 15.4. POSIX Shell, Korn Shell, and Bash Compatibility POSIX Shell Korn Shell Bash ENV file X X POS vi line edit mode X X X emacs line. it's not a reserved word, the shell proceeds to step 3. 2. Next, the shell checks the command against its function list and executes it if found.3. The shell checks to see whether the command. standard shell, the Korn shell, and Bash with the features described in this chapter. In this table, an "X" denotes a supported feature, "UP," an optional feature in the POSIX shell

Ngày đăng: 13/08/2014, 15:21

Tài liệu cùng người dùng

Tài liệu liên quan