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

Example 11−13. Using set with positional parameters<br />

#!/bin/bash<br />

# script "set−test"<br />

# Invoke this script with three command line parameters,<br />

# for example, "./set−test one two three".<br />

echo<br />

echo "Positional parameters before set \`uname −a\` :"<br />

echo "Command−line argument #1 = $1"<br />

echo "Command−line argument #2 = $2"<br />

echo "Command−line argument #3 = $3"<br />

set `uname −a` # Sets the positional parameters to the output<br />

# of the command `uname −a`<br />

echo $_ # unknown<br />

# Flags set in script.<br />

echo "Positional parameters after set \`uname −a\` :"<br />

# $1, $2, $3, etc. reinitialized to result of `uname −a`<br />

echo "Field #1 of 'uname −a' = $1"<br />

echo "Field #2 of 'uname −a' = $2"<br />

echo "Field #3 of 'uname −a' = $3"<br />

echo −−−<br />

echo $_ # −−−<br />

echo<br />

exit 0<br />

Invoking set without any options or arguments simply lists all the environmental and other variables<br />

that have been initialized.<br />

bash$ set<br />

AUTHORCOPY=/home/bozo/posts<br />

BASH=/bin/bash<br />

BASH_VERSION=$'2.05.8(1)−release'<br />

...<br />

XAUTHORITY=/home/bozo/.Xauthority<br />

_=/etc/bashrc<br />

variable22=abc<br />

variable23=xzy<br />

Using set with the −− option explicitly assigns the contents of a variable to the positional parameters.<br />

When no variable follows the −−, it unsets the positional parameters.<br />

Example 11−14. Reassigning the positional parameters<br />

#!/bin/bash<br />

variable="one two three four five"<br />

set −− $variable<br />

# Sets positional parameters to the contents of "$variable".<br />

Chapter 11. Internal Commands and Builtins 135

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

Saved successfully!

Ooh no, something went wrong!