Shell Input and Output


You have already seen how to use echo to print output from your script, and how to use environment variables or command-line arguments to get information to your script. This section describes additional features for dealing with input and output.

The echo Command

Table 20–5 shows the escape sequences that may be embedded in the arguments to echo:

Table 20–5: echo Escape Sequences

Echo Escape Sequences

 

\b

Backspace

\c

Print line without newline

\f

Form feed

\n

Newline

\r

Return

\t

Tab

\v

Vertical tab

\\

Backslash

For example,

 echo "Copying files ... \c" cp -r $OLDDIR $NEWDIR echo "done.\nFile $OLDDIR copied."

will print something like

 Copying files ... done. File CurrentScripts copied.

In some versions of echo (including bash), you will need to enable escape sequences with the flag -e. You can also disable escape sequences with -E. In ksh and bash, you can use the flag -n to prevent echo from adding a newline at the end of each line. So in bash, this example could be written as

 echo -n "Copying files ... " cp -r $OLDDIR $NEWDIR echo -e "done.\nFile $OLDDIR copied."

The read Command

The read command lets you insert the user input into your script interactively read reads one line from standard input and saves the line in one or more shell variables. For example,

 echo "Enter your name." read NAME echo "Hello, $NAME"

If you do not specify a variable to save the input, REPLY is used as a default.

You can also use the read command to assign several shell variables at once. When you use read with more than one variable name, the first field typed by the user is assigned to the first variable; the second field, to the second variable; and so on. Leftover fields are assigned to the last variable.

 $ cat readDemo echo "Enter a line of text:" read $FIRST $SECOND $REST echo -e "$FIRST\n$SECOND\n$REST" $ ./readDemo Enter a line of test: the five boxing wizards jump quickly the five boxing wizards jump quickly

The field separator for shell input is defined by the IFS (Internal Field Separator) variable, which is a blank space by default. If you wish to use a different character to separate fields, you can do so by redefining the IFS shell variable. For example, IFS=: will set the field separator to the colon character (:).

Here Documents

The here document facility provides multiline input to commands within shell scripts, while preserving the newlines in the input. It is similar to file redirection. Instead of typing

 echo "Reminder: team meeting is in one hour," > message echo "in the second floor meeting room." >> message echo "Please reply if you can't make it." >> message mail dbp etch a-liu < message rm message

to create and mail a file, you can use

 mail dbp etch a-liu <<message Reminder: team meeting is in one hour, in the second floor meeting room. Please reply if you can't make it. message

to send a block of text to the command without first writing it to a file.

The operator <<word defines the beginning of multiline input. The shell reads everything up to the next line that contains only word, and treats it as input from a file. If you use <<-word (with a minus sign in front of word), then leading spaces and tabs will be stripped out of each line of input. This allows you to indent your script to make it more readable, like this:

 mail dbp etch a-liu <<-message    Reminder: team meeting is in one hour,    in the second floor meeting room.    Please reply if you can't make it. message




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net