5

This is my script:

echo "Name"

read name

if [ "$name" == "abcd" ]; then

 echo "Password"

 read password

 if [ "$password == "pwd" ]; then

  echo "Hello"

 else

  echo "Wrong password"

 fi

else

 echo "wrong username"

fi

And this is the output I get when I run it:

sh hello.sh

Name

abcd

hello.sh: line 14: unexpected EOF while looking for matching `"'

hello.sh: line 16: syntax error: unexpected end of file

Any idea whats wrong here? This could be a very silly one, but i wasted almost an hour on it.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
hari
  • 1,297
  • 5
  • 17
  • 36

4 Answers4

9
if [ "$password == "pwd" ]; then

You have an unmatched " before $password

cnicutar
  • 178,505
  • 25
  • 365
  • 392
3

you can use case. Here's an example. You won't want to tell anybody which component is wrong, for security reason.

echo "Name"
read name
echo "Password"
read password
case "${name}${password}" in
 "abcdpwd" ) 
     echo "hello";;
  *) echo "User or password is wrong";;
esac
bash-o-logist
  • 6,665
  • 1
  • 17
  • 14
0

In Shell script.

if condition
    then
        if condition
        then
            .....
            ..
            do this
        else
            ....
            ..
            do this
        fi
    else
        ...
        .....
        do this
    fi
vivek
  • 1,034
  • 11
  • 14
-1
if [ "$name" == "abcd" ];
then
  echo "Password"
  read password
  if [ "$password" == "pwd" ];
  then
    echo "Hello"
  else
    echo "Wrong password"
  fi
else
  echo "wrong username"
fi
h3nr1ke
  • 363
  • 6
  • 19
ashok
  • 1