HomePage » ShellScript

Bash stuff


Regular Expressions
Options processing with getopt


Compiling shell scripts to binary executables

http://www.datsi.fi.upm.es/~frosal/
Use the command shc -f script_file_name

Bash

CHeck out bash4 http://www.bash-hackers.org/wiki/doku.php/bash4


Bash array and for loop
#!/bin/bash

DIRS[0]=one
DIRS[1]=two
DIRS[2]=three

for d in ${DIRS[@]}
do
		echo $d
done



enable history timestamp
Save this in /etc/bashrc, this will enable timestamp display with the history command

export HISTTIMEFORMAT="%Y-%h-%d - %H:%M:%S "


bash shortcuts
^aGo to beginning of line
^a, alt-lGo to beginning of line, and change all letters to lower case
alt-cChange line to lower case
^e Go to end of line
^r Go to end of line
^k Delete line
^w Delete word
^yUndo deletion
^l Clear screen


More at http://linuxhelp.blogspot.com/2005/08/bash-shell-shortcuts.html

bash prompt
Reference: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
PS1="\u@\h \W> "


Case switch
case "$1" in
start)
 do start;
;;

stop)
 do stop;
;;

*)
 echo usage;
;; 
esac


Function and variables
#!/bin/bash

function run() {
		eval "$1=\"xxx\""
}

VAR=1
run VAR
echo $VAR


function myfunc()
{
	local  myresult='some value'
	echo "$myresult"
}

result=$(myfunc)
echo $result


Compare floating point numbers
#!/bin/bash
LOAD=`uptime | cut -d: -f5 | cut -d, -f1`
LIMIT=5.0
echo $LOAD
if [ `expr $LOAD \> $LIMIT` -eq 1 ]; then
# do your thing
fi


Read input, if then else
echo "Please enter an address to forward to."
echo "To disable forward, type DISABLE. Default is null in 10seconds"
read -t10 fw
if [ $fw == "DISABLE" ];
then
 echo "Disable mail forward done."
 rm -Rf ~/.forward
elif [ $fw == "disable" ];
 then
 echo "Disable mail forward done."
 rm -Rf ~/.forward
else
 echo "Activating mail forward to $fw"
 echo $fw > ~/.forward
fi
echo "Done."


for loop
TASKS=`ls -1 /scp/backup.jobs/*.sh`
for tt in $TASKS
do
       eval $tt & LASTJOB=$!
       wait $LASTJOB
done


if then else
if [ ! -f blah.txt ]; then
 echo "File does not exist."
else
 echo "File already exist."
fi


Text parsing
sed -n "s/.*ok.*:.*:25 .*:\(.*\)::.*/\1/gp" /var/log/smtpd/current
cut -f5 -d: /var/log/smtpd/current | grep .
egrep -R ok /var/log/smtpd/current |awk '{print $6}'|sed -e 's/:/ /g'| cut -f2 -d' '
awk 'BEGIN {FS=":" } /ok/ {print $5}' /var/log/smtpd/current
fgrep ok /var/log/smtpd/current | cut -f5 -d: fgrep ok /var/log/smtpd/current | cut -f5 -d:


bash array and loops
#!/usr/local/bin/bash
PATH[0]=/home/sites/1/
PATH[1]=/home/sites/2/
PATH[2]=/home/sites/3/
PATH[3]=/home/sites/4/
PATH[4]=/home/sites/5/
arrsize=${#PATH[@]}
DATESTR=`/bin/date +%Y-%m`

for ((i=0;i<$arrsize;i++)); do
        echo ${PATH[$i]}
        find ${PATH[$i]} ! -name "*.zip" -mtime +31 | xargs zip -m ${PATH[$i]}/logs-$DATESTR.zip
done
exit 0


real example 1
#!/bin/bash
# script to get IP and MX record for a list of domains
# column definition
# COL1: domain name
# COL2: apache virtual host domain
# COL3: IP address of A record
# COL4: MX record(s)

function getip() {
        IP=`host "$1" |  awk '{print $4}' | sed s/found\:/NOT_FOUND/g`
        if [ ${#IP} -lt 2 ]; then
                # no record found
                echo -n "NOT_FOUND"
        else
                echo -n $IP
        fi
}

function getmx() {
         MX=`host -tmx  "$1" | grep handled | grep -v datapipe | awk '{print $7}' | sed '$!N;s/\n/\//g'`
         if [ ${#MX} -lt 2 ]; then
                # no MX record found
                echo NONE
         else
                # get IP address for each MX record
                MXIP=`host "$MX" |  awk '{print $4}' | sed s/found\:/NOT_FOUND/g`
                echo "$MX ($MXIP)"
         fi
}

for f in `cat domains_step2.csv`
do
        d=`echo $f  | awk 'BEGIN {FS=","} {print $1}'`
        echo -n $f
        getip "$d"
        echo -n ","
        getmx "$d"
done


Parameter Expansion

JAVA_HOME=${JAVA_HOME:-"/usr/java/latest"}


Remove carriage returns

echo $text | tr -d '\n'



awk

 awk -F: '($3 == "0") {print}' /etc/passwd


sed

Inline editing
sed -i.bak -e 's/Today/Yesterday/g' some_file.txt
Comments [Hide comments/form]
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki