Ubuntu – Help on if-else in shell

11.04command linescripts

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");

Best Answer

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
Related Question