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.

echo "However, the script may also contain shell and system commands."<br />

exit 0<br />

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

It is even possible to combine a Bash script and Perl script within the same file. Depending on how the script<br />

is invoked, either the Bash part or the Perl part will execute.<br />

Example 34−5. Bash and Perl scripts combined<br />

#!/bin/bash<br />

# bashandperl.sh<br />

echo "Greetings from the Bash part of the script."<br />

# More Bash commands may follow here.<br />

exit 0<br />

# End of Bash part of the script.<br />

# =======================================================<br />

#!/usr/bin/perl<br />

# This part of the script must be invoked with −x option.<br />

print "Greetings from the Perl part of the script.\n";<br />

# More Perl commands may follow here.<br />

# End of Perl part of the script.<br />

bash$ bash bashandperl.sh<br />

Greetings from the Bash part of the script.<br />

bash$ perl −x bashandperl.sh<br />

Greetings from the Perl part of the script.<br />

34.3. Tests and Comparisons: Alternatives<br />

For tests, the [[ ]] construct may be more appropriate than [ ]. Likewise, arithmetic comparisons might<br />

benefit from the (( )) construct.<br />

a=8<br />

# All of the comparisons below are equivalent.<br />

test "$a" −lt 16 && echo "yes, $a < 16"<br />

# "and list"<br />

/bin/test "$a" −lt 16 && echo "yes, $a < 16"<br />

[ "$a" −lt 16 ] && echo "yes, $a < 16"<br />

[[ $a −lt 16 ]] && echo "yes, $a < 16" # Quoting variables within<br />

(( a < 16 )) && echo "yes, $a < 16" # [[ ]] and (( )) not necessary.<br />

city="New York"<br />

# Again, all of the comparisons below are equivalent.<br />

test "$city" \< Paris && echo "Yes, Paris is greater than $city" # Greater ASCII order.<br />

/bin/test "$city" \< Paris && echo "Yes, Paris is greater than $city"<br />

[ "$city" \< Paris ] && echo "Yes, Paris is greater than $city"<br />

[[ $city < Paris ]] && echo "Yes, Paris is greater than $city" # Need not quote $city.<br />

Chapter 34. Miscellany 340

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

Saved successfully!

Ooh no, something went wrong!