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

. data−file # Load a data file.<br />

# Same effect as "source data−file", but more portable.<br />

# The file "data−file" must be present in current working directory,<br />

#+ since it is referred to by its 'basename'.<br />

# Now, reference some data from that file.<br />

echo "variable1 (from data−file) = $variable1"<br />

echo "variable3 (from data−file) = $variable3"<br />

let "sum = $variable2 + $variable4"<br />

echo "Sum of variable2 + variable4 (from data−file) = $sum"<br />

echo "message1 (from data−file) is \"$message1\""<br />

# Note: escaped quotes<br />

print_message This is the message−print function in the data−file.<br />

exit 0<br />

File data−file for Example 11−18, above. Must be present in same directory.<br />

# This is a data file loaded by a script.<br />

# Files of this type may contain variables, functions, etc.<br />

# It may be loaded with a 'source' or '.' command by a shell script.<br />

# Let's initialize some variables.<br />

variable1=22<br />

variable2=474<br />

variable3=5<br />

variable4=97<br />

message1="Hello, how are you?"<br />

message2="Enough for now. Goodbye."<br />

print_message ()<br />

{<br />

# Echoes any message passed to it.<br />

if [ −z "$1" ]<br />

then<br />

return 1<br />

# Error, if argument missing.<br />

fi<br />

echo<br />

until [ −z "$1" ]<br />

do<br />

# Step through arguments passed to function.<br />

echo −n "$1"<br />

# Echo args one at a time, suppressing line feeds.<br />

echo −n " "<br />

# Insert spaces between words.<br />

shift<br />

# Next one.<br />

done<br />

echo<br />

Chapter 11. Internal Commands and Builtins 140

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

Saved successfully!

Ooh no, something went wrong!