Reputation: 1
The output remains "Dead,ciao in hell" irrespective of the input.
<---Begin Code--->
echo "Which wire to cut? Red or Green? "
read die
if [[ die = "red" ]]; then
echo "You are saved!"
else echo "Dead,ciao in hell"
fi
<---End COde--->
Can somebody pleas help me?
Thanks!
Upvotes: 0
Views: 37
Reputation: 21965
You should sanitize user input. Say, what if he typed reD
in panic? He is still saved?
#!/bin/bash
read -p "Which wire to cut? Red or Green?" die
die="${die:-GREEN}" # if the die's value is null, set it to GREEN
die="${die^^}" # Convert the input to uppercase
if [ "$die" = "RED" ]
then
echo "You are saved!"
elif [ "$die" = "GREEN" ]
then
echo "Dead,ciao in hell"
else
echo "Confused, you deserve to die"
fi
Improvements
read prompt
with the -p
optiondie
and set defaults to 'GREEN' using die="${die:-GREEN}"
elif-else
part to deal with with input which is other than expected value.[[..]]
with built-in [..]
.Upvotes: 0
Reputation: 703
You missed the $ symbol at die
echo "Which wire to cut? Red or Green? "
read die
if [[ $die = "red" ]]; then
echo "You are saved!"
else echo "Dead,ciao in hell"
fi
OutPut
Which wire to cut? Red or Green?
asd
Dead,ciao in hell
Which wire to cut? Red or Green?
red
You are saved!
Upvotes: 2