Table of Contents
Bash
Generic script frameworks
Developed over time, these are frameworks which allows me to concentrate on the purpose of the script, the “boilerplate” code is already written and handles the boring stuff such as parsing input, reporting errors and writing log files.
Misc
Print tab character.
tab=$(echo -en “\t”)
bash-3.00$ tab=$(echo -en "\t") bash-3.00$ echo "test $tab test" test test bash-3.00$ echo "test${tab}test" test test bash-3.00$ echo "test${tab}${tab}test" test test bash-3.00$
specifying the base of a number
value too great for base
probably refers to an arithmetic operation on a single 8 or 9 with a leading 0, ie. 08 or 09. Bash assumes this is an octal number from the leading 0 but 8 & 9 are not legal octal characters.
You can specify the base of a number using base#number
, usage is:-
RTTMP7=$((10#$RTTMP6 + 10#$RTTMP4))
Comparisons
Arithmetic Comparisons | |
---|---|
-lt | < |
-gt | > |
-le | ⇐ |
-ge | >= |
-eq | == |
-ne | != |
String Comparisons | |
---|---|
= | equal |
!= | not equal |
< | less then |
> | greater then |
-n s1 | string s1 is not empty |
-z s1 | string s1 is empty |
if elif else
ACTION=${1} if [ "${ACTION}" = "start" ]; then echo "Starting Apache instances" elif [ "${ACTION}" = "stop" ]; then echo "Stopping Apache instances" else echo "No parameter passed, Useage:- startstopapache.sh start|stop" exit 2 fi
if with multiple conditions
#!/bin/bash FABRIC=$1 if [ "${FABRIC}" = "" ] then echo "You need to provide a Fabric number, either 1 or 2" exit 1 fi
AND if [ "${FABRIC}" != "1" ] && [ "${FABRIC}" != "2" ] then echo "Fabric values can only be 1 or 2" fi
OR if [ ${CPRESULT} != 0 ] || [ ${CPUSERRESULT} != 0 ] then THRESHOLD=1 fi
for loop
Specify a range of variables to work with
for i in {1..5}
for VARIABLE in bar boyl gm ppy or for VARIABLE in `cat FILENAME` do echo "WebSite is ${VARIABLE}, acton is ${ACTION}" done
Exit from loop early
# use for loop read all GC servers for (( gc=0; gc<${GCLENGTH}; gc++ )); do echo "Using >${GLOBALCATALOG[$gc]}<" echo "===============================" echo "" ldapsearch -x -L -h ${GLOBALCATALOG[$gc]} -D "${CN}" -W -b "${SEARCHBASE=}" -s sub proxyAddresses LDAPSEARCHRESULT=$? if [ ${LDAPSEARCHRESULT} = 0 ] then break fi done
while loop
while ($line = <SERIAL>) { print "$line\n"; }
Time duration of operation and record exit status
DURATION=$(time (tar -xvzf ${TARLOCATION}/ww-wiki-backup-${DAY}.tar.gz > /dev/null 2>&1) 2>&1) TARRESULT=$? echo "untar result is >${TARRESULT}<, " >> ${TMPLOG} echo -n "Explode return code is >${TARRESULT}<, it took" >> ${TMPLOG} echo "${DURATION}" >> ${TMPLOG} echo "" >> ${TMPLOG}
Remove first or last character from variable
Use #
for start of variable or %
for the end of the variable.
[ scripts]$ FILE=admin.log.bz2
[ scripts]$ echo $FILE admin.log.bz2 [ scripts]$ FILE=${FILE%.bz2} [ scripts]$ echo $FILE admin.log
Getting web page with wget
Basic use:-
$ wget --user='user' --password='pw' 'http://www.yoursite.com/index.html'
wget with login page and cookie is a two stage process. First, login and retreive cookie, secondly use this cookie to download resource:-
$ wget --save-cookies tmpcookies.txt --post-data 'user=user&password=pw' http://www.yoursite.com/login.html $ wget --load-cookies tmpcookies.txt -p http://www.yoursite.com/protectedcontent.html
curl is somewhat similar:-
$ curl -u 'user:pw' 'http://www.yoursite.com'
Spinner.sh
#/bin/bash # written Andrew Stringer 26/07/2013 #Aimlessly spins round waiting for something function spinner() { local delay=0.75 local spinstr='/-\|' printf "...\n" while [ true ]; do local temp=${spinstr#?} printf "[%c]" "$spinstr" local spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b" done } echo "Waiting for 10s." echo "================" spinner & LAST_SPINNER_PID=$! sleep 10 kill ${LAST_SPINNER_PID} echo "" echo "Finished!" echo ""
$ ./spinner.sh Waiting for 10s. ================ ... [-] Finished!
Does directory exist
#!/bin/bash DIR=H if [ -d $DIR ]; then echo "Dir $DIR exists" else mkdir H fi