General: December 2006 Archives
A friend of mine wanted a script (in bash), which would try to mount/cd to an NFS (mounted share). But he had problems when the the NFS share was not mounted or the cd command timed out. He wanted certain actions to be done when the command succeeded or when it timed out. Here is the script which I cooked up for him (could make it better, but this gives an overall picture of using expect and bash together in a single script)
#!/bin/bash
# change this to the actual command that needs to run.
# Two Sample command have been given here
cmd="mount -t nfs hotmail.com:/mailbox/easwar/ /easwar/"
#cmd="cd /usr/sap/trans/"
# how long should I wait ?
cmd_timeout=5
# dont edit this
cmd_status=`/usr/bin/expect <
spawn /bin/bash
sleep 0.5
send -- "${cmd}\n"
sleep 0.5
expect {
"*trans]#" {exit 10}
"*No such*" {exit 20}
timeout {exit 30}
}
EOF`
# you can edit from here
# get the return code from the previous command
ERRVAL=$?
# perform actions based on it ...
if [ $ERRVAL -eq 20 ]
then
echo "Sorry. Directory Not Found"
elif [ $ERRVAL -eq 30 ]
then
echo "Command \"$cmd\" timed out ... "
elif [ $ERRVAL -eq 10 ]
then
echo "Success... run more commands"
fi
If you see this error in your mail log, then, you have to do the following:
touch /var/qmail/queue/lock/sendmutex chown qmails:qmail /var/qmail/queue/lock/sendmutex
