Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Please help me writing a shell programming using if else, all possibile code using [[ ]], [ ], (( )).
I tried but it does not work (you can check my previous question Arithmetic binary operators -gt, -lt give error but work in a shell script).

Here is the C version:

int i = 10, n = 20;

if (i < n)
    printf("i smaller");
else if (i > n)
    printf("n smaller");
else
    printf("same");
share|improve this question
1  
You can read more about shell scripting in the Advanced Bash-Scripting Guide at: tldp.org/LDP/abs/html You can also download the whole site tutorials as a complete .pdf here –  Pavlos G. Aug 23 '11 at 17:46

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
You could remove many semicolons, use a better indentation and use elif, but substantially seems correct. The first one should work also with sh, the POSIX shell. –  enzotib Aug 23 '11 at 18:50
    
ty, feel free to edit it :) –  Denwerko Aug 23 '11 at 22:44

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.