Unix Shell Programming Third Edition phần 5 pptx

69 213 0
Unix Shell Programming Third Edition phần 5 pptx

Đ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

> do > run $file > done request id is laser1-33 (standard input) request id is laser1-34 (standard input) request id is laser1-35 (standard input) request id is laser1-36 (standard input) $ The four words memo1, memo2, memo3, and memo4 will be assigned to the variable file in order, and the run program will be executed with the value of this variable as the argument. Execution will be just as if you typed in the four commands: $ run memo1 request id is laser1-33 (standard input) $ run memo2 request id is laser1-34 (standard input) $ run memo3 request id is laser1-35 (standard input) $ run memo4 request id is laser1-36 (standard input) $ Incidentally, the shell permits filename substitution in the list of words in the for, meaning that the previous loop could have also been written this way: for file in memo[1-4] do run $file done And if you wanted to run all the files in your current directory through run, you could type for file in * do run $file done If the file filelist contains a list of the files that you want to run through run, you can type files=$(cat filelist) for file in $files do run $file done to run each of the files, or, more succinctly, for file in $(cat filelist) do run $file done If you found that you were using the run program often to process several files at once, you could go inside the run program and modify it to allow any number of files to be passed as arguments to the program. $ cat run # # process files through nroff version 2 # for file in $* do tbl $file | nroff -rom -Tlp | lp done $ Recall that the special shell variable $* stands for all the arguments typed on the command line. So if you executed the new version of run by typing run memo1 memo2 memo3 memo4 the $* in the for's list would be replaced by the four arguments memo1, memo2, memo3, and memo4. Of course, you could also type run memo[1-4] to achieve the same results. The $@ Variable While we're on the subject of $*, let's look at it in a bit more detail. We'll write a program called args that displays all the arguments typed on the command line, one per line. $ cat args echo Number of arguments passed is $# for arg in $* do echo $arg done $ Now to try it: $ args a b c Number of arguments passed is 3 a b c $ args 'a b' c Number of arguments passed is 2 a b c $ In the second case, even though a b was passed as a single argument to args, the $* in the for command was replaced by the shell with a b c, which is three words. Thus the loop was executed three times. Whereas the shell replaces the value of $* with $1, $2, , if you instead use the special shell variable "$@" it will be replaced with "$1", "$2", . The double quotes are necessary around $@ because without them this variable behaves just like $*. Go back to the args program and replace the $* with "$@": $ cat args echo Number of arguments passed is $# for arg in "$@" do echo $arg done $ Now try it: $ args a b c Number of arguments passed is 3 a b c $ args 'a b' c Number of arguments passed is 2 a b c $ args Try it with no arguments Number of arguments passed is 0 $ In the last case, no arguments were passed to the program. So the variable "$@" was replaced by nothing. The net result is that the body of the loop was not executed at all. The for Without the List A special notation is recognized by the shell when writing for commands. If you write for var do command command done (note the absence of the in), the shell automatically sequences through all the arguments typed on the command line, just as if you had written for var in "$@" do command command done Here's the third and last version of the args program: $ cat args echo Number of arguments passed is $# for arg do echo $arg done $ args a b c Number of arguments passed is 3 a b c $ args 'a b' c Number of arguments passed is 2 a b c $ The while Command The second type of looping command to be described in this chapter is the while. The format of this command is while command t do command command done command t is executed and its exit status tested. If it's zero, the commands enclosed between the do and done are executed. Then command t is executed again and its exit status tested. If it's zero, the commands enclosed between the do and done are once again executed. This process continues until command t returns a nonzero exit status. At that point, execution of the loop is terminated. Execution then proceeds with the command that follows the done. Note that the commands between the do and done might never be executed if command t returns a nonzero exit status the first time it's executed. Here's a program called twhile that simply counts to 5: $ cat twhile i=1 while [ "$i" -le 5 ] do echo $i i=$((i + 1)) done $ twhile Run it 1 2 3 4 5 $ The variable i is used as the counting variable and is initially set equal to 1. Then the while loop is entered. It continues execution as long as i is less than or equal to 5. Inside the loop, the value of i is displayed at the terminal. Then it is incremented by one. The while loop is often used in conjunction with the shift command to process a variable number of arguments typed on the command line. The next program, called prargs, prints each of the command-line arguments one per line. $ cat prargs # # Print command line arguments one per line # while [ "$#" -ne 0 ] do echo "$1" shift done $ prargs a b c a b c $ prargs 'a b' c a b c $ prargs * addresses intro lotsaspaces names nu numbers phonebook stat $ prargs No arguments $ While the number of arguments is not equal to zero, the value of $1 is displayed and then a shift executed. Recall that this shifts down the variables (that is, $2 to $1, $3 to $2, and so on) and also decrements $#. When the last argument has been displayed and shifted out, $# will equal zero, at which point execution of the while will be terminated. Note that if no arguments are given to prargs (as was done in the last case), the echo and shift are never executed because $# is equal to zero as soon as the loop is entered. [...]... logging on, we'll have it check only once every minute To do this, you have to know about a command called sleep that suspends execution of a program for a specified number of seconds So the Unix command (this isn't a shell built-in) sleep n suspends execution of the program for n seconds At the end of that interval, the program resumes execution where it left off—with the command that immediately follows... -m option was selected If it wasn't, the message is written to standard output; otherwise, it's mailed to steve $ mon sandy -m Usage: mon [-m] user -m means to be informed by mail $ mon -m sandy & [1] 54 35 $ vi newmemo Work continues you have mail $ mail From steve Wed Aug 28 17:44:46 EDT 2002 sandy has logged on ?d $ Of course, we could have written mon to accept the -m option as either the first or... consists of a set of rules as outlined in the Utility Argument Syntax section of the POSIX standard Also note that the old version of mon could have been executed as follows: $ mon sandy | mail steve & [1] 55 22 $ to achieve the same net result as adding the -m option Two last points before leaving the discussion of mon: First, you'll probably always want to run this program in the background It would be... from the shell. ) The second time getopts is executed, getopts stores the character t inside option, stores the next command-line argument (600) inside OPTARG, sets OPTIND to three, and returns a zero exit status The case command then matches the character t stored inside option The code associated with that case copies the value of 600 that was stored in OPTARG into the variable interval The third time... for the removal of the specified file 5: Write a program called collect that runs in the background and counts the number of users logged in at the end of each interval and also the number of processes run during that interval Allow the interval to be specified with a -t option (see the previous exercise), with the default 10 minutes Use the fact that the special shell variable $! is set to the process... case where the process number loops back around to 1 after the maximum is reached So collect -t 900 > stats & should start up collect to gather the desired statistics every 15 minutes and write them into the file stats 6: Write a shell program called wgrep that searches a file for a given pattern, just as grep does For each line in the file that matches, print a "window" around the matching line That... file and where the pattern matches the last line of the file 7: Modify wgrep to take an optional -w option that specifies the window size; so wgrep -w 3 UNIX text should print three lines before and after each line from text that contains the pattern UNIX 8: Modify wgrep to take a variable number of filenames as arguments Precede each output line with the name of the file in which the match occurs (as... in the variable y It follows from this that the command read text reads and stores an entire line into the shell variable text A Program to Copy Files Let's put the read command to work We'll write a simplified version of the cp command that will be a bit more user friendly than the standard Unix one We'll call it mycp, and we'll have it take two arguments: the source file and the destination file... $runner fi $ mon -m Missing user name! $ mon -x fred Illegal option mon: illegal option x Usage: mon [-m] [-t n] user -m means to be informed by mail -t means check every n secs $ mon -m -t 600 ann & [1] 57 92 $ Check every 10 min for ann When the line mon -m -t 600 ann & is executed, the following occurs inside the while loop in mon: getopts is executed, and it stores the character m inside the variable... terminated when you log off the system If you want a program to continue executing after you've logged off, you can run it with the nohup command, or schedule it to run with at or from the cron Consult your Unix User's Manual for more details Because mon only checks once per minute for the user's logging on, it won't hog the system's resources while it's running (an important consideration when submitting . replaced by the shell with a b c, which is three words. Thus the loop was executed three times. Whereas the shell replaces the value of $* with $1, $2, , if you instead use the special shell variable. program called twhile that simply counts to 5: $ cat twhile i=1 while [ "$i" -le 5 ] do echo $i i=$((i + 1)) done $ twhile Run it 1 2 3 4 5 $ The variable i is used as the counting. special notation is recognized by the shell when writing for commands. If you write for var do command command done (note the absence of the in), the shell automatically sequences through

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

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

  • Đang cập nhật ...

Tài liệu liên quan