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 />

#!/bin/bash<br />

PS3='Choose your favorite vegetable: ' # Sets the prompt string.<br />

echo<br />

select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"<br />

do<br />

echo<br />

echo "Your favorite veggie is $vegetable."<br />

echo "Yuck!"<br />

echo<br />

break # if no 'break' here, keeps looping forever.<br />

done<br />

exit 0<br />

If in list is omitted, then select uses the list of command line arguments ($@) passed to the script<br />

or to the function in which the select construct is embedded.<br />

Compare this to the behavior of a<br />

for variable [in list]<br />

construct with the in list omitted.<br />

Example 10−30. Creating menus using select in a function<br />

#!/bin/bash<br />

PS3='Choose your favorite vegetable: '<br />

echo<br />

choice_of()<br />

{<br />

select vegetable<br />

# [in list] omitted, so 'select' uses arguments passed to function.<br />

do<br />

echo<br />

echo "Your favorite veggie is $vegetable."<br />

echo "Yuck!"<br />

echo<br />

break<br />

done<br />

}<br />

choice_of beans rice carrots radishes tomatoes spinach<br />

# $1 $2 $3 $4 $5 $6<br />

# passed to choice_of() function<br />

exit 0<br />

See also Example 35−3.<br />

Chapter 10. Loops and Branches 123

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

Saved successfully!

Ooh no, something went wrong!