Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell scripting : Giving error invalid command name "server1"

Tags:

shell

unix

expect

I have written a shell script for double authentication. This is giving an error while execution in if statement.

#!/usr/bin/Expect
#!/bin/bash
set cpPass "App12345"
set server [lindex $argv 0]
set pass "App12345@123"
set app "app1"
set appPass "app@1013"
spawn ssh user1@$server.corp.clll.com
expect {
        "(yes/no)?" {

                      send "yes\r"

                      expect "password:"

                      send "$cpPass\r"

                    }

        "password:" {
                       send "$pass\r"
                    }
     }
expect "bash"
send "su - $app\r"
expect "Password:"
send "$appPass\r"

if [ "$server" == "server1" ]
then
   send "cd /ngs/app/genevad/QA/site/distribution/servers1\r";
else
   send "sqlplus gqa_owner/App#[email protected]:1700/app1D\r";
fi

interact

The error is like this on execution of command ./script.sh server1

invalid command name "server1"
    while executing
""$server" == "novello" "
    invoked from within
"if [ "$server" == "novello" ]"
    (file "./script.sh" line 28)

I have tried various combinations in if like : if [ $server == "novello" ] if [ $server -eq "novello" ] if [ "$server" -eq "novello" ] etc but still not working. Any suggestions/Solutions to it?

like image 825
Nishant Lakhara Avatar asked Jun 13 '26 11:06

Nishant Lakhara


1 Answers

Problem is here:

#!/usr/bin/Expect
#!/bin/bash

You cannot have 2 shebangs in same Unix script. Only first will be in effect and second will be ignored. Hence your script must be fully expect compliant script (since that is first one here - should be lowercase expect though).

Moment you try to use a bash syntax in it, you will get errors since that bash specific script will be interpreted by expect.

like image 160
anubhava Avatar answered Jun 17 '26 22:06

anubhava