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

# then "$*" and "$@" do not echo positional params as expected.<br />

mecho () # Echo positional parameters.<br />

{<br />

echo "$1,$2,$3";<br />

}<br />

IFS=""<br />

set a b c<br />

mecho "$*"<br />

mecho $*<br />

mecho $@<br />

mecho "$@"<br />

# Set, but empty.<br />

# Positional parameters.<br />

# abc,,<br />

# a,b,c<br />

# a,b,c<br />

# a,b,c<br />

# The behavior of $* and $@ when $IFS is empty depends<br />

# on whatever Bash or sh version being run.<br />

# It is therefore inadvisable to depend on this "feature" in a script.<br />

# Thanks, S.C.<br />

exit 0<br />

Other Special Parameters<br />

$−<br />

$!<br />

Flags passed to script (using set). See Example 11−13.<br />

This was originally a ksh construct adopted into Bash, and unfortunately it does not<br />

seem to work reliably in Bash scripts. One possible use for it is to have a script<br />

self−test whether it is interactive.<br />

PID (process id) of last job run in background<br />

LOG=$0.log<br />

COMMAND1="sleep 100"<br />

echo "Logging PIDs background commands for script: $0" >> "$LOG"<br />

# So they can be monitored, and killed as necessary.<br />

echo >> "$LOG"<br />

# Logging commands.<br />

echo −n "PID of \"$COMMAND1\": " >> "$LOG"<br />

${COMMAND1} &<br />

echo $! >> "$LOG"<br />

# PID of "sleep 100": 1506<br />

$_<br />

# Thank you, Jacques Lederer, for suggesting this.<br />

Special variable set to last argument of previous command executed.<br />

Example 9−9. underscore variable<br />

Chapter 9. Variables Revisited 78

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

Saved successfully!

Ooh no, something went wrong!