LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script error (https://www.linuxquestions.org/questions/programming-9/bash-script-error-482693/)

noir911 09-12-2006 05:02 AM

bash script error
 
I have got a script which will decrypt a GPG encrypted password file and logs me in to the server automatically using ssh key. But it's giving an error since I added a server "venus" which is an exception to the generic rules.

Code:

1 if [ -f $DECRYPT ]; then
2    if [ $1 = "venus" ]; then
3        ssh -p 23000 me@venus
4    fi
5 else
6 grep -i $1 passwd.txt
7 ssh -i ssh_key admin@$1
8 else
9  echo "no decrypted passwd file found"
10  gpg -d passwd.txt.gpg > passwd.txt
11  grep -i $1 passwd.txt
12  ssh -i ssh_key admin@$1
13 fi

here's the error I get when I run the script - ./password.sh venus

Code:

syntax error near unexpected token `else'
line 8 `else'


blackhole54 09-12-2006 05:13 AM

You have two else statements for one if. (Lines 5 & 8) Should one of them be an elif?

noir911 09-12-2006 05:41 AM

Thanks for your reply. I have just substituted the else in line 5 with elif and also the else in line 8 with elif. But still getting the same error.

druuna 09-12-2006 06:00 AM

Hi,

elif should be followed by an expression:

if [ something ]
then
....
elif [ something else ]
....
else
....
fi


Hope this helps.

blackhole54 09-12-2006 06:10 AM

Things were clearly not matched up right. Substituting elif for both elses would clearly not be right either, as well as elif requires a condition to test.

I haven't really figured out what you are trying to do, but making another wild stab at it, is the following what you were trying to do?

Code:

if [ -f $DECRYPT ]; then
  if [ $1 = "venus" ]; then
      ssh -p 23000 me@venus
  else
      grep -i $1 passwd.txt
      ssh -i ssh_key admin@$1
  fi
else
  echo "no decrypted passwd file found"
  gpg -d passwd.txt.gpg > passwd.txt
  grep -i $1 passwd.txt
  ssh -i ssh_key admin@$1
fi


noir911 09-12-2006 06:42 AM

Thanks guys. I have solved the problem. Here's what the solution looks like -

Code:

1 if [ -f $DECRYPT ]; then
2  if [ $1 = "venus" ]; then
3      ssh -p 23000 me@venus
4  fi
5 grep -i $1 passwd.txt
6 ssh -i ssh_key admin@$1
7
8 if [ ! -f $DECRYPT ]; then
9  echo "no decrypted passwd file found"
10  gpg -d passwd.txt.gpg > passwd.txt
11  if [ $1 = "venus" ]; then
12      ssh -p 23000 me@venus
13  fi
14  grep -i $1 passwd.txt
15  ssh -i ssh_key admin@$1
16 fi

I have a plan to put line 2-4 and 11-13 in a function somewhere.


All times are GMT -5. The time now is 05:14 PM.