Help on if-else in shell
What about
#!/bin/sh
i=1
n=2
if [ "$i" -gt "$n" ]; then
echo "i is bigger"
elif [ "$n" -gt "$i" ]; then
echo "n is bigger"
else
echo "same"
fi
second...
#!/bin/bash
i=1
n=2
if ((i > n)) ; then
echo "i is bigger"
elif ((i < n)); then
echo "n is bigger"
else
echo "same"
fi
and last one...
#!/bin/bash
i=1
n=2
if [[ $i -gt $n ]]; then
echo "i is bigger"
elif [[ $i -lt $n ]]; then
echo "n is bigger"
else
echo "same"
fi
I have the following script to make sure some services stay up. It also does a mysql backup everyday, checks for services online and if not they go up again, etc.. Not my best work but gives you an idea on how to use IF and the same goes for ELSE IF:
#!/bin/bash
function checker() {
PID1=$(pidof authserver)
PID2=$(pidof worldserver)
PID3=$(date +%k%M)
PID4=$(date +%d_%m_%y)
Destiny=/root/bck
if [ -z "$PID1" ]; then
echo 3 > /proc/sys/vm/drop_caches
screen -A -m -d -S auth ./bin/authserver
fi
if [ -z "$PID2" ]; then
echo 3 > /proc/sys/vm/drop_caches
screen -A -m -d -t titan -S world ./bin/worldserver
fi
if [ "$PID3" -eq 230 ]; then
screen -S world -p titan -X stuff $'anno SERVIDOR CERRADO DENTRO DE 10 MINUTOS POR UNA DURACION DE 30 SEGUNDOS PARA OPTIMIZACIONES\n'
sleep 300
screen -S world -p titan -X stuff $'anno SERVIDOR CERRADO DENTRO DE 5 MINUTOS POR UNA DURACION DE 30 SEGUNDOS PARA OPTIMIZACIONES\n'
sleep 180
screen -S world -p titan -X stuff $'anno SERVIDOR CERRADO DENTRO DE 2 MINUTOS POR UNA DURACION DE 30 SEGUNDOS PARA OPTIMIZACIONES\n'
screen -S world -p titan -X stuff $'ser shut 120\n'
sleep 120
/etc/init.d/apache restart
sleep 5
mysqldump -uroot -px chart > $Destiny/c$PID4.sql
mysqldump -uroot -px auth > $Destiny/a$PID4.sql
/etc/init.d/mysql restart
sleep 5
killall -9 worldserver
killall -9 authserver
echo 3 > /proc/sys/vm/drop_caches
fi
return 1
}
while :
do
checker
sleep 5
done
Like I said, not the best but gets the job done.