Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  12] [ 0]  / answers: 1 / hits: 40959  / 3 Years ago, tue, november 16, 2021, 4:43:08

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

More From » 11.04

 Answers
1

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

[#43748] Tuesday, November 16, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wheance

Total Points: 314
Total Questions: 96
Total Answers: 112

Location: Benin
Member since Thu, Aug 12, 2021
3 Years ago
;