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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

username2=<br />

# username2 has been declared, but is set to null.<br />

echo "username2 = ${username2:−`whoami`}"<br />

# Will echo because of :− rather than just − in condition test.<br />

exit 0<br />

The default parameter construct finds use in providing "missing" command−line arguments in scripts.<br />

DEFAULT_FILENAME=generic.data<br />

filename=${1:−$DEFAULT_FILENAME}<br />

# If not otherwise specified, the following command block operates<br />

#+ on the file "generic.data".<br />

#<br />

# Commands follow.<br />

See also Example 3−4, Example 29−2, and Example A−7.<br />

Compare this method with using an and list to supply a default command−line argument.<br />

${parameter=default}, ${parameter:=default}<br />

If parameter not set, set it to default.<br />

Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and<br />

is null, [22] as above.<br />

echo ${username=`whoami`}<br />

# Variable "username" is now set to `whoami`.<br />

${parameter+alt_value}, ${parameter:+alt_value}<br />

If parameter set, use alt_value, else use null string.<br />

Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is<br />

null, see below.<br />

echo "###### \${parameter+alt_value} ########"<br />

echo<br />

a=${param1+xyz}<br />

echo "a = $a" # a =<br />

param2=<br />

a=${param2+xyz}<br />

echo "a = $a"<br />

param3=123<br />

a=${param3+xyz}<br />

echo "a = $a"<br />

# a = xyz<br />

# a = xyz<br />

echo<br />

echo "###### \${parameter:+alt_value} ########"<br />

echo<br />

a=${param4:+xyz}<br />

echo "a = $a" # a =<br />

param5=<br />

Chapter 9. Variables Revisited 85

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

Saved successfully!

Ooh no, something went wrong!