PDA

View Full Version : beginner shell script



cap10Ibraim
May 9th, 2010, 10:49 AM
please help with this example


#!/bin/sh
VALID_PASSWORD="secret" #this is our password.
echo "Please enter the password:"
read PASSWORD
if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi
im getting this error when i type secret


[: 9: secret: unexpected operator
ACCESS DENIED!

Jose Catre-Vandis
May 9th, 2010, 11:14 AM
You only need one equal sign in the test:

#!/bin/sh
VALID_PASSWORD="secret" #this is our password.
echo "Please enter the password:"
read PASSWORD
if [ "$PASSWORD" = "$VALID_PASSWORD" ]; then
echo "You have access!"
else
echo "ACCESS DENIED!"
fi

cap10Ibraim
May 9th, 2010, 11:40 AM
Thanks yes it works now , but is that syntax(first post) completely wrong or old or.. because I copied it from a tutorial

Portmanteaufu
May 9th, 2010, 03:31 PM
Actually, the problem is that you're writing Bash code and running it with the Dash shell.

Ubuntu is a bit unusual in that '/bin/sh' (a system shortcut to the default shell) is pointing to Dash. In most other systems, it points to Bash. Unfortunately, most tutorials go ahead and assume it's pointing to Bash without mentioning it.

If you change your first line to

#!/bin/bash
then '==' will work as expected.

(Btw, this is a very, very common stumbling block. I've seen it in two or three threads in the last week, so don't feel bad! :D )

cap10Ibraim
May 9th, 2010, 04:01 PM
I was waiting for a similar reply thanks a lot

Jose Catre-Vandis
May 9th, 2010, 09:59 PM
Actually, the problem is that you're writing Bash code and running it with the Dash shell.

Ubuntu is a bit unusual in that '/bin/sh' (a system shortcut to the default shell) is pointing to Dash. In most other systems, it points to Bash. Unfortunately, most tutorials go ahead and assume it's pointing to Bash without mentioning it.

If you change your first line to

#!/bin/bash
then '==' will work as expected.

(Btw, this is a very, very common stumbling block. I've seen it in two or three threads in the last week, so don't feel bad! :D )

I went at it so quickly I didn't spot that!