Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Advanced Bash Shell Scripting Gude.pdf
Скачиваний:
57
Добавлен:
17.08.2013
Размер:
4.82 Mб
Скачать

Testing Your Knowledge of Tests

 

Advanced Bash-Scripting Guide:

 

Prev

Chapter 7. Tests

Next

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.

Prev

Home

Next

Nested if/then Condition Tests

Up

Operations and Related Topics

http://tldp.org/LDP/abs/html/testtest.html [7/15/2002 6:35:28 PM]

Preliminary Exercises

 

Advanced Bash-Scripting Guide:

 

Prev

Chapter 2. Starting Off With a Sha-Bang

Next

2.2. Preliminary Exercises

1.System administrators often write scripts to automate common tasks. Give several instances where such scripts would be useful.

2.Write a script that upon invocation shows the time and date, lists all logged-in users, and gives the system uptime. The script then saves this information to a logfile.

Prev

Home

Next

Invoking the script

Up

Basics

http://tldp.org/LDP/abs/html/prelimexer.html [7/15/2002 6:35:28 PM]

Tests and Comparisons: Alternatives

 

Advanced Bash-Scripting Guide:

 

Prev

Chapter 34. Miscellany

Next

34.3. Tests and Comparisons: Alternatives

For tests, the [[ ]] construct may be more appropriate than [ ]. Likewise, arithmetic comparisons might benefit from the (( )) construct.

a=8

 

 

 

 

# All of the

comparisons below are equivalent.

 

 

test "$a"

-lt 16 && echo "yes, $a < 16"

# "and list"

 

/bin/test

"$a" -lt 16 && echo "yes, $a < 16"

 

 

[ "$a" -lt 16 ] && echo "yes, $a < 16"

 

 

[[ $a -lt

16

]] && echo "yes, $a < 16"

# Quoting variables within

(( a < 16

))

&& echo "yes, $a < 16"

# [[ ]] and (( )) not necessary.

city="New

York"

 

 

# Again, all

of the comparisons below are equivalent.

 

test "$city"

\< Paris && echo "Yes, Paris is greater than $city"

# Greater ASCII

order.

 

 

 

 

/bin/test

"$city" \< Paris && echo "Yes, Paris is greater than $city"

[ "$city"

\<

Paris ] && echo "Yes, Paris is greater than $city"

 

[[ $city < Paris ]] && echo "Yes, Paris is greater than $city"

# Need not quote

$city.

 

 

 

 

# Thank you,

S.C.

 

 

 

 

 

 

 

Prev

Home

Next

Shell Wrappers

Up

Optimizations

http://tldp.org/LDP/abs/html/testsandcomparisons.html [7/15/2002 6:35:29 PM]