Home > Software engineering >  if statement in script file does not return output
if statement in script file does not return output

Time:03-08

read code
read answer

if [[ $answer" = B && "$code" = grep $code filename ]]

then echo blue

else :

fi
done

Hello, if someone can help me understand why I am not receiving an output I would appreciate it. I am supposed to have two conditions in my if statement, so if the user gives a B and the file contains the "code" then it will echo blue

if [ $answer" = B && "$code" = '6314' ]
then echo blue

else :

fi
done

...

I do get an answer

CodePudding user response:

Suggesting to try this:

if [[ "$answer" == B && "$code" == "$(grep $code filename)" ]]; then 
  echo blue
else
  echo red
fi

Or:

if [[ "$answer" == B && "$code" == '6314' ]]; then 
  echo blue
else 
  echo red
fi

CodePudding user response:

if [ "$answer" = "B" ] && [ "`grep \"$code\" filename`" = "$code" ]; then
  echo code blue
else
  echo code red
fi
  • Related