27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[[ condition1 && condition2 ]]<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

The −o and −a operators work with the test command or occur within single test brackets.<br />

if [ "$exp1" −a "$exp2" ]<br />

Refer to Example 8−3 and Example 26−10 to see compound comparison operators in action.<br />

7.4. Nested if/then Condition Tests<br />

Condition tests using the if/then construct may be nested. The net result is identical to using the &&<br />

compound comparison operator above.<br />

if [ condition1 ]<br />

then<br />

if [ condition2 ]<br />

then<br />

do−something # But only if both "condition1" and "condition2" valid.<br />

fi<br />

fi<br />

See Example 35−4 for an example of nested if/then condition tests.<br />

7.5. Testing Your Knowledge of Tests<br />

The systemwide xinitrc file can be used to launch the X server. This file contains quite a number of if/then<br />

tests, as the following excerpt shows.<br />

if [ −f $HOME/.Xclients ]; then<br />

exec $HOME/.Xclients<br />

elif [ −f /etc/X11/xinit/Xclients ]; then<br />

exec /etc/X11/xinit/Xclients<br />

else<br />

# failsafe settings. Although we should never get here<br />

# (we provide fallbacks in Xclients as well) it can't hurt.<br />

xclock −geometry 100x100−5+5 &<br />

xterm −geometry 80x50−50+150 &<br />

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

netscape /usr/share/doc/HTML/index.html &<br />

fi<br />

fi<br />

Explain the "test" constructs in the above excerpt, then examine the entire file,<br />

/etc/X11/xinit/xinitrc, and analyze the if/then test constructs there. You may need to refer ahead to<br />

the discussions of grep, sed, and regular expressions.<br />

Chapter 7. Tests 54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!