Tuesday, January 29, 2013

Simple way To Run A Command on Linux Using Shell Script


Let us take the example of the command "route add -net default netmask 0.0.0.0 gw 172.26.64.1" which adds the default route to the PC.  I had a problem of my default route being deleted when ever i run my usual automation script as a result of which the PC was not reachable from my automation server. To resolve this issue we wrote a simple shell script that will indefinitely add route until the process is stopped. I would trigger this script when ever i had to run my automation script

Note: Name this file as routeAddScript.sh and run it with the command ./routeAddScript.sh

#!/bin/sh

while [ 1 ]; do
        route add -net default netmask 0.0.0.0 gw 172.26.64.1
#       echo "Ran route command"
        sleep 1
done
exit 0