Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new to shell scripting. When I want to run a bunch of Linux commands I do fine, but how do I give user input when another script is called and asks for input while it runs. Here is the situation where I am stuck. I install the same server setup in classrooms frequently so I want to completely automate it.

#!/bin/bash
sudo apt-get install python -y
sudo apt-get install python-m2crypto -y
sudo apt-get install git-core -y
git clone https://github.com/learningequality/ka-lite.git
cd ka-lite/
./setup_linux.sh

#the script works until here
#now, while the setup script is running, the following needs to happen
#I need to press enter twice
#enter the password twice
#press enter twice
#press y and then enter

#here are some things I tried, none of them worked
send "yes\n"
send "yes\n"
#echo | <Press [enter] to continue...> #enter
#echo | <return> #enter
Password8 #password
Password8 #password
echo | <yourfinecommandhere> #enter
echo | <return> #enter
y
share|improve this question
3  
If password entry is involved, you probably need a tool like expect. –  celtschk Jun 24 at 20:08
add comment

1 Answer

You can use TCL Expect or Perl::Expect. After trying them both I prefer the later because I am more familiar with Perl.

This is a snipped of a script that I use to ssh into several test servers (not recommended for sensitive production servers):

if( defined $password ) {
  $exp->expect(
    $timeout,
    [   qr/no\)\?\s+$/i => sub {
        my $self = shift;
        $self->send("yes\n");
        exp_continue;
      }
    ],
    [   qr/password:\s+$/i => sub {
        my $self = shift;
        $self->send("$password\n");
        exp_continue;
      }
    ],
    '-re',
    qr/~/,    #' wait for shell prompt, then exit expect
  );
}

You can look at the full source here: https://github.com/DavidGamba/bin/blob/master/cssh

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.