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

echo "var1 = $var1"<br />

echo<br />

# A single 'read' statement can set multiple variables.<br />

echo −n "Enter the values of variables 'var2' and 'var3' (separated by a space or tab): "<br />

read var2 var3<br />

echo "var2 = $var2 var3 = $var3"<br />

# If you input only one value, the other variable(s) will remain unset (null).<br />

exit 0<br />

A read without an associated variable assigns its input to the dedicated variable $REPLY.<br />

Example 11−3. What happens when read has no variable<br />

#!/bin/bash<br />

echo<br />

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

# First code block.<br />

echo −n "Enter a value: "<br />

read var<br />

echo "\"var\" = "$var""<br />

# Everything as expected here.<br />

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

echo<br />

echo −n "Enter another value: "<br />

read<br />

# No variable supplied for 'read', therefore...<br />

#+ Input to 'read' assigned to default variable, $REPLY.<br />

var="$REPLY"<br />

echo "\"var\" = "$var""<br />

# This is equivalent to the first code block.<br />

echo<br />

exit 0<br />

Normally, inputting a \ suppresses a newline during input to a read. The −r option causes an<br />

inputted \ to be interpreted literally.<br />

Example 11−4. Multi−line input to read<br />

#!/bin/bash<br />

echo<br />

echo "Enter a string terminated by a \\, then press ."<br />

echo "Then, enter a second string, and again press ."<br />

read var1 # The "\" suppresses the newline, when reading "var1".<br />

# first line \<br />

# second line<br />

Chapter 11. Internal Commands and Builtins 127

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

Saved successfully!

Ooh no, something went wrong!