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.

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

}<br />

digit_check () # Front−end to isdigit ().<br />

{<br />

if isdigit "$@"<br />

then<br />

echo "\"$*\" contains only digits [0 − 9]."<br />

else<br />

echo "\"$*\" has at least one non−digit character."<br />

fi<br />

echo<br />

}<br />

a=23skidoo<br />

b=H3llo<br />

c=−What?<br />

d=What?<br />

e=`echo $b`<br />

f=AbcDef<br />

g=27234<br />

h=27a34<br />

i=27.34<br />

# Command substitution.<br />

check_var $a<br />

check_var $b<br />

check_var $c<br />

check_var $d<br />

check_var $e<br />

check_var $f<br />

check_var # No argument passed, so what happens?<br />

#<br />

digit_check $g<br />

digit_check $h<br />

digit_check $i<br />

exit 0<br />

# Script improved by S.C.<br />

select<br />

# Exercise:<br />

# −−−−−−−−<br />

# Write an 'isfloat ()' function that tests for floating point numbers.<br />

# Hint: The function duplicates 'isdigit ()',<br />

#+ but adds a test for a mandatory decimal point.<br />

The select construct, adopted from the Korn Shell, is yet another tool for building menus.<br />

select variable [in list]<br />

do<br />

command...<br />

break<br />

done<br />

This prompts the user to enter one of the choices presented in the variable list. Note that select uses<br />

the PS3 prompt (#? ) by default, but that this may be changed.<br />

Example 10−29. Creating menus using select<br />

Chapter 10. Loops and Branches 122

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

Saved successfully!

Ooh no, something went wrong!