why in Cygwin Terminal - the if statement work
and ubuntu - unix - not working for
this code :
#!/bin/sh
valid_password="pass"
echo "Please enter the password:"
read password
if [ "$password" == "$valid_password" ]
then
echo "You have access!"
else
echo "Access denied!"
fi
because the correct syntax to
[is:From your error message it sounds like you wrote:
change this to:
notice the space after
[.ifjust takes a shell command, try to run it and depending if the exit code from the program is0it will run the commands inside theifstatement.In your terminal, write i.e.:
to test your if statement:
As @nullrevolution said, the
!is evaluated if you use double quotes, it will try to run last command in your shell history, in this case that is matchingu.This is because the
!is evaluated before the double quotes are matched, and echo is run. If you still want to use double quotes, you will have to escape the!outside the quotes:@nullrevolution also said you could try with
bash, which has a builtin syntax for the expression inside if statements.Also in your program I guess you do not want to echo the password in the terminal, to turn off echo temporary change:
to
if you forgot to write
stty echoto turn on echo again, just writeresetin your terminal, and it will reset the terminal to default settings.A useful tutorial for bourn shell script can be found here:
http://www.grymoire.com/Unix/Sh.html