Bashwavv
Bashwavv
x
Advanced Shell Scripting
Michael Potter
Replatform Technologies, LLC
May 20th, 2007
#!/opt/local/bin/bash
set -o option
echo "My process list:" >outputfile.txt
set -o noclobber
set -o errexit
set -o pipefail
set -o nounset
trap 'echo error at about $LINENO' ERR
mv outputfile.txt outputfile.bak
echo "My process list:" >outputfile.txt
ps aux 2>&1 |grep "^$USER" >>outputfile.txt
. ./stringent.sh || exit 1
mv outputfile.txt outputfile.bak
echo "My process list:" >outputfile.txt
ps aux 2>&1 |grep "^$USER" >>outputfile.txt
mv outputfile.txt outputfile.bak
echo "My process list:" >outputfile.txt
ps aux 2>&1 |grep "^$USER" >>outputfile.txt
set -o errexit
download stringent.sh from
set -o noclobber
set -o nounset www.replatformtech.com
set -o pipefail
function traperr
{
echo "ERROR: ${BASH_SOURCE[1]} “ \
“at about line ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
WAVV Green Bay Copyright Michael Potter 16
May 20th, 2007 [email protected]
BASH_SOURCE/BASH_LINENO
echo "ERROR: ${BASH_SOURCE[1]} “ \
“at about line ${BASH_LINENO[0]}"
function traperr
{
E N
O K
echo "ERROR: $1 ${BASH_SOURCE[1]} “ \
“at about line ${BASH_LINENO[0]}"
}
B R
trap ‘traperr $BASH_COMMAND’ ERR
source ./stringent.sh
if (( $cmpRtn == 0 ))
then
echo "file same"
elif (( $cmpRtn == 1 ))
then
echo "file different"
else
echo "cmp failed"
fi
Value1 = 10
Value2 = 18
12 12
WAVV Green Bay Copyright Michael Potter 24
May 20th, 2007 [email protected]
Arithmetic Syntax
• intA=$(( ($intB + 5) * 2 ))
– Allowed anywhere a variable is allowed
• let "intA = ($intB + 5 ) * 2"
– returns 0 or 1
• (( intA = ($intB + 5) * 2 ))
– equivalent to let
• intA=\($intB+5\)*2
– no spaces allowed
– Special characters must be escaped
– intA must be declare -i
• intA=$[ ($intB + 5) * 2 ]
– deprecated
if command
if (( ))
if [ ]
if test
if [[ ]]
bash-3.1$ ./quoting.sh
\\ $MyVar \x41
\ $MyVar A
\ bob \x41
echo "$Year-$Month-$Day"
}
WAVV Green Bay Copyright Michael Potter 52
May 20th, 2007 [email protected]
sub strings
declare MyStr="The quick brown fox"
echo "${MyStr/the/a}"
# a fox jumped the dog
echo "${MyStr//the/a}”
# a fox jumped a dog
echo "${MyStr//the }”
# fox jumped dog
Michael Potter
PO Box 469
Lisle, IL 60532