Expect command not working in bash script

I have a wait problem.

when i run abc.sh which includes the following lines

#!/usr/bin/expect spawn scp /etc/httpd/conf/httpd.conf 192.168.0.12:/tmp ###################### expect { -re "password:" { exp_send "PASSWORD\r" } } interact 

his works are beautiful

but when i use the similar code in my startup script it doesn't work


 #!/bin/bash clear while read -u3 LINE do code ......... code ....... code ........ REMOTE_COMMANDS1="scp -r -v $BASE_DIRECTORY/$USERNAME $D_IPADDRESS:/home/" spawn $REMOTE_COMMANDS1 ###################### expect { -re "password:" { exp_send "redhat\r" } } interact done 3< /opt/sitelist.txt 

he gives an error

./script.sh: line 62: syntax error near unexpected token }' ./script.sh: line 62: }'

I think this is because I do not include #! / Usr / bin / expect at the top of the script, but if I use this and execute my script, it does nothing and does not display all the code in the terminal after extraction. so can we include #! / usr / bin / expect and #! / bin / bash at the same time?

Regards, Aditya

+4
source share
1 answer

Of course, bash cannot interpret wait commands, just as bash cannot interpret Java / Perl / Python syntax. There are several approaches.

Write the expected script as a separate program and call it from the bash script:

 #!/bin/bash clear while read -u3 LINE do #... ./expect_scp.exp "$BASE_DIR" "$D_IPADDRESS" "$USERNAME" "$PASSWORD" done 3< /opt/sitelist.txt 

and expect_scp.exp is

 #!/usr/bin/expect set base_dir [lindex $argv 0] set remote_ip [lindex $argv 1] set username [lindex $argv 2] set password [lindex $argv 3] # or foreach {base_dir remote_ip username password} $argv {break} spawn scp -rv $base_dir/$username $remote_ip:/home/ expect -re "password:" exp_send -- "$password\r" interact 

You can put the expected script inside a bash script, with particular emphasis on quoting. Fortunately, single quotes are not particularly expected.

 #!/bin/bash clear export BASE_DIR D_IPADDRESS USERNAME PASSWORD while read -u3 LINE do #... expect -c ' spawn scp -rv $env(BASE_DIR)/$env(USERNAME) $env(D_IPADDRESS):/home/ expect -re "password:" exp_send -- "$env(PASSWORD)\r" interact ' done 3< /opt/sitelist.txt 
+9
source

All Articles