How to use wait and shell script together

I am using wait script command and some shell script command as shown below.

#!/usr/bin/expect
#!/bin/bash
spawn sudo mkdir -p /usr/lib/jvm1
expect "\[sudo\] password for hduser:"
send "cisco\r"

spawn sudo apt-get install openssh-client

      

But the above command didn't install openssh-client. He just shows the command. and nothing is installed.

Where am I doing wrong here?

+3


source to share


2 answers


Once done spawn

in the installation openssh-client

, you have expect

to expect

wait for something to happen. Else, expect

don't worry about anything and just quit. In this case, we can wait eof

to confirm that the installation is complete.

#!/usr/bin/expect
spawn sudo mkdir /usr/lib/jvm1
# This will match the ": " at the end. That is why $ symbol used here.
expect ": $"; # It will be more robust than giving as below 
  #expect "\[sudo\] password for hduser:" 
send "password\r"
#This will match the literal '$' symbol, to match the '$' in terminal prompt
expect "\\$"
spawn sudo apt-get install openssh-client
expect ": $"
send "password\r"
expect eof { puts "OpenSSH Installation completed successfully. :)" }

      

In the above code, the reason for using the literal '$' is the reason for the terminal prompt. Manual interaction of actions as shown below.

dinesh@VirtualBox:~$ sudo mkdir /usr/lib/dinesh7
[sudo] password for dinesh: 
dinesh@VirtualBox:~$ 

      



The last line that has a character $

in the terminal. You can customize the prompt based on your terminal. You might be wondering why we have to send the password a second time. Remember that this is not a normal terminal, where providing a password for the administrator is sufficient for other administrator operations until the terminal is closed. With expect

we create sudo

every time it is different, and this is the reason for repeating it again.

As Andrey pointed out, you don't need to add #!

using path bash

.

Update: At the expect

time of default expectations 10 seconds

. You can change it using set

as shown below.

set timeout 60; # Timeout will happen after 60 seconds.

      

+3


source


You need to pass -y to apt-get, for example: spawn sudo apt-get install -y openssh-client

-or- wait for the installation prompt and reply "Y"



Also, you don't need to run two shells, it just #!/usr/bin/expect

should be enough.

+2


source







All Articles