user6790656
user6790656

Reputation: 1

The output remains the same

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

Answers (2)

sjsam
sjsam

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

  • Used read prompt with the -p option
  • Added a null-value check for die and set defaults to 'GREEN' using die="${die:-GREEN}"
  • Added elif-else part to deal with with input which is other than expected value.
  • Since the input is preprocessed, replaced [[..]] with built-in [..].

Upvotes: 0

shas
shas

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

Related Questions