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

let "a −= 5" # Equivalent to let "a = a − 5"<br />

echo "32 − 5 = $a"<br />

let "a = a * 10" # Equivalent to let "a = a * 10"<br />

echo "27 * 10 = $a"<br />

let "a %= 8" # Equivalent to let "a = a % 8"<br />

echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"<br />

echo<br />

eval<br />

exit 0<br />

eval arg1 [arg2] ... [argN]<br />

Translates into commands the arguments in a list (useful for code generation within a script).<br />

Example 11−9. Showing the effect of eval<br />

#!/bin/bash<br />

y=`eval ls −l` # Similar to y=`ls −l`<br />

echo $y<br />

# but linefeeds removed because "echoed" variable is unquoted.<br />

echo<br />

echo "$y" # Linefeeds preserved when variable is quoted.<br />

echo; echo<br />

y=`eval df`<br />

echo $y<br />

# Similar to y=`df`<br />

# but linefeeds removed.<br />

# When LF's not preserved, it may make it easier to parse output,<br />

#+ using utilities such as "awk".<br />

exit 0<br />

Example 11−10. Forcing a log−off<br />

#!/bin/bash<br />

y=`eval ps ax | sed −n '/ppp/p' | awk '{ print $1 }'`<br />

# Finding the process number of 'ppp'.<br />

kill −9 $y<br />

# Killing it<br />

# Above lines may be replaced by<br />

# kill −9 `ps ax | awk '/ppp/ { print $1 }'<br />

chmod 666 /dev/ttyS3<br />

# Doing a SIGKILL on ppp changes the permissions<br />

# on the serial port. Restore them to previous state.<br />

rm /var/lock/LCK..ttyS3<br />

# Remove the serial port lock file.<br />

exit 0<br />

Chapter 11. Internal Commands and Builtins 133

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

Saved successfully!

Ooh no, something went wrong!