if statement - Integer comparison in Bash using if-else -
i have variable called choice
. now, try use if compare entered value:
read $choice if [ "$choice" == 2 ];then #do elif [ "$choice" == 1 ];then #do else else echo "invalid choice!!" fi
the output goes directly invalid choice if enter either 1 or 2. tried put quotes around 1 , 2 inside if statement. still didn't work. using -eq
gives me error "unary operator expected".what doing wrong here?
your read
line incorrect. change to:
read choice
i.e. use name of variable want set, not value.
-eq
correct test compare integers. see man test
descriptions (or man bash
).
an alternative using arithmetic evaluation instead (but still need correct read
statement):
read choice if (( $choice == 2 )) ; echo 2 elif (( $choice == 1 )) ; echo 1 else echo "invalid choice!!" fi
Comments
Post a Comment