KfWiki : ShellScript

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
HomePage » ShellScript


Regular Expressions

Compiling shell scripts to binary executables

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

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


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"
read 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

There are no comments on this page. [Add comment]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.3889 seconds