I needed to run a script based on the environment..I'm just having some issue with the script..can someone help me ?
#!/bin/ksh
pat01=`uname -a | awk '{printf "%s", $2}'`
#if it is server 1 run this file
if [$pat01 == 'cpcmlt01'] ; then
echo "run this file"
#if it is server 2 run this file
if [$pat01 == 'cpcmlt02'] ; then
echo "run the second file"
fi
fi
#!/bin/ksh -x
pat01=`uname -a | awk '{printf "%s", $2}'`
if [ $pat01 == 'cpcmlt01' ] #if it is server 1 run this file
then
echo "run this file"
else
if [ $pat01 == "cpcmlt02" ] #if it is server 2 run this file
then
echo "run the second file"
fi
fi
Take off the -x (first line...)
if it works, else, send us the output
Ok I had some time to test so (changed the names of the servers so I can test...)
#!/bin/ksh
pat01=$(uname -a | awk '{printf "%s", $2}')
if [ $pat01 == 'oslo4' ] #if it is server 1 run this file
then
echo "run this file"
else
if [ $pat01 == "oslo6" ] #if it is server 2 run this file
then
echo "run the second file"
exit 2
fi
echo "Not concerned..."
exit
fi
echo "Finish"
1) You have no spaces between the brackets and the contents.
2) == only works in certain shells, and even then, only inside [[ ]]. Use =.
3) You should quote all your variables in case any of them are blank.
4) awk's printf usually needs () brackets.
5) You have your if's nested when you didn't want or need them to be.
So:
#!/bin/ksh
pat01=`uname -a | awk '{printf("%s", $2)}'`
#if it is server 1 run this file
if [ "$pat01" = 'cpcmlt01' ] ; then
echo "run this file"
fi
#if it is server 2 run this file
if [ "$pat01" = 'cpcmlt02' ] ; then
echo "run the second file"
fi
But I think you could do it more elegantly with case:
case `uname -a | awk '{printf("%s", $2)}'` in
cpcmlt02)
;;
cpcmlt02)
;;
*) echo "Unknown server"
;;
esac
In fact, you might not need uname at all:
case $HOSTNAME in
cpcmlt02)
;;
cpcmlt02)
;;
*) echo "Unknown server $HOSTNAME"
;;
esac
oslo4:/export/home/vbe $ ./debugscript
run this file
Finish
oslo4:/export/home/vbe $
#now set $pat01 == 'oslo5'
oslo4:/export/home/vbe $ ./debugscript
Not concerned...
# else if [ $pat01 == "oslo4" ]
oslo4:/export/home/vbe $ ./debugscript
run the second file
pat01=`uname -a | awk '{printf "%s", $2}'`
#if it is server 1 run this file
if [ $pat01 == "cpcmlt01" ] ; then
echo "run this file"
#if it is server 2 run this file
if [ $pat01 == "cpcmlt02" ] ; then
echo "run the second one"
fi
fi