Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cooper M.Advanced bash-scripting guide.2002.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
916.67 Кб
Скачать

Advanced Bash−Scripting Guide

exit $NOTFOUND

fi

if [ ${filename##*.} != "gz" ]

# Using bracket in variable substitution. then

echo "File $1 is not a gzipped file!" exit $NOTGZIP

fi

zcat $1 | most

#Uses the file viewer 'most' (similar to 'less').

#Later versions of 'most' have file decompression capabilities.

#May substitute 'more' or 'less', if desired.

exit $? # Script returns exit status of pipe.

#Actually "exit $?" unnecessary, as the script will, in any case,

#return the exit status of the last command executed.

compound comparison

−a

logical and

exp1 −a exp2 returns true if both exp1 and exp2 are true.

−o

logical or

exp1 −o exp2 returns true if either exp1 or exp2 are true.

These are similar to the Bash comparison operators && and ||, used within double brackets.

[[ condition1 && condition2 ]]

The −o and −a operators work with the test command or occur within single test brackets. if [ "$exp1" −a "$exp2" ]

Refer to Example 8−2 and Example 26−7 to see compound comparison operators in action.

7.4. Nested if/then Condition Tests

Condition tests using the if/then construct may be nested. The net result is identical to using the && compound comparison operator above.

if [ condition1 ] then

if [ condition2 ] then

do−something # But only if both "condition1" and "condition2" valid.

fi

7.4. Nested if/then Condition Tests

50

Advanced Bash−Scripting Guide

fi

See Example 35−3 for an example of nested if/then condition tests.

7.5. Testing Your Knowledge of Tests

The systemwide xinitrc file can be used to launch the X server. This file contains quite a number of if/then tests, as the following excerpt shows.

if [ −f $HOME/.Xclients ]; then exec $HOME/.Xclients

elif [ −f /etc/X11/xinit/Xclients ]; then exec /etc/X11/xinit/Xclients

else

#failsafe settings. Although we should never get here

#(we provide fallbacks in Xclients as well) it can't hurt. xclock −geometry 100x100−5+5 &

xterm −geometry 80x50−50+150 &

if [ −f /usr/bin/netscape −a −f /usr/share/doc/HTML/index.html ]; then netscape /usr/share/doc/HTML/index.html &

fi

fi

Explain the "test" constructs in the above excerpt, then examine the entire file, /etc/X11/xinit/xinitrc, and analyze the if/then test constructs there. You may need to refer ahead to the discussions of grep, sed, and regular expressions.

7.5. Testing Your Knowledge of Tests

51