Shell script to check whether a number is positive or negative

Let's write a shell script to check whether a number is positive or negative.




Algorithm

1. Get a number

2. if number < 0, "echo negative"

3. elif number > 0, "echo positive"

4. else (num == 0), "echo neither Positive nor Negative"




Positive or Negative - Shell Script

#shell script to check whether a number is positive or negative

echo "Enter a Number"
read num

if [ $num -lt 0 ]
then
    echo "Negative"
elif [ $num -gt 0 ]
then
    echo "Positive"
else
    echo "Neither Positive Nor Negative"
fi

-gt stands for greater than.

-lt stands for less than.




Output

Enter a Number

10

Positive


Enter a Number

-2

Negative


Enter a Number

0

Neither Positive Nor Negative


Topics You Might Like