In: Computer Science
* Show the output of the following BASH code:
i=7
i=$((i-5))
if [ $i -eq 4 ] # -eq is ==
then
echo "Four"
elif [ $i -eq 2 ] then
echo "Two"
else
echo $i
fi
Output of the above BASH code is "Two"
Explanation:
In the program "i" value is 7 then $i = $((i-5)) that is 7-5=2
Then the value of $i=2
In the code the if condition is that "if $i = 4 , Then display "Four" "
"if $i=2, Then display "Two" otherwise display value of "$i"
Since the $i value is 2 , the program displays "Two" as output
This is the reason , the program displays "Two" as output.
Error in the code:
In the above code , at elif statement "then" must be in next line
otherwise program will display an error.
Correct code:
i=7
i=$((i-5))
if [ $i -eq 4 ] # -eq is ==
then
echo "Four"
elif [ $i -eq 2 ]
then
echo "Two"
else
echo $i
fi
Screenshot of the running code and Output: