0

Is there a way to use expect command directly in a bash script for which the automation is to be done, instead of writing a expect script and use spawn command to trigger the bash script from expect script?

Like altogether in one script?

  • Unclear what you're asking. How do I ask a good question? – glenn jackman Aug 29 '18 at 17:14
  • Is it okay now ? – Arun Prakash Aug 29 '18 at 17:19
  • For me the bash via expect is what is working (running the bash script with spawn). When I use expect in bash it does not work, Do we need to include something(like spawn for bash) to make it work? – Arun Prakash Aug 29 '18 at 17:31
  • 1
    @ArunPrakash, so, you'd like to include an expect script in a shell script to automate that same shell script? Why not just change the script to behave automatically without external help? – ilkkachu Aug 29 '18 at 17:51
  • 1
    @ArunPrakash, yes, and especially if you wrote the yourself, why not modify it to accept the inputs in some other form, in some way that would lend itself more to being automated, without requiring the use of expect? – ilkkachu Aug 29 '18 at 18:07
0

You can start expect from a shell script, but expect can't control the input of a shell that isn't a child process of expect.

On the other hand, there is rarely a reason to use expect on the shell itself, whatever you ant to do with expect you can do directly from the shell. Most likely you don't want to use expect on the shell itself, but on commands started from the shell. So you can call expect from the shell and have expect call the programs that you want to control with expect.

  • I get it - but expect can't control the input of a shell that isn't a child process of expect – Arun Prakash Aug 29 '18 at 18:26
  • Why would you need expect on the shell itself? – RalfFriedl Aug 29 '18 at 18:30
  • The Shell script required some input to be given. So I thought I would automate with expect – Arun Prakash Aug 29 '18 at 18:43
  • There is rarely a reason why the shell script would want some import and not a program called by the shell script. – RalfFriedl Aug 29 '18 at 18:49
0

If you don't need your script to be interactive, then change read var to var="fixed value".

Or, you can automate the input by passing values to your scripts standard input:

./myscript.sh <<END_OF_INPUTS
input string 1
input string 2
... and so on
END_OF_INPUTS

Or, you can put the values into the script itself:

$ cat test.sh
#!/bin/sh
read -p "this one is interactive: " first

exec 0<<END_INPUT
foo
bar
END_INPUT
read -p "2nd input is automated: " second
read -p "3nd input is automated: " third

echo "1st input: $first"
echo "2nd input: $second"
echo "3rd input: $third"
echo done


$ sh test.sh
this one is interactive: I type this
1st input: I type this
2nd input: foo
3rd input: bar
done

Expect is a very useful tool, but there are not very many places where it's really essential.

  • First one ends up in a infinite loop. Second one throws a error warning: here-document at line 61 delimited by end-of-file (wanted 'END_INPUT') & syntax error: unexpected end of file even after ending END_INPUT – Arun Prakash Aug 29 '18 at 18:25
  • I suspect you made a typo: my code as posted is correct. Did you put whitespace before or after the end_input ending word? – glenn jackman Aug 29 '18 at 18:28
  • No I didn't give spaces, I just copied your code – Arun Prakash Aug 29 '18 at 18:42
  • so where does "line 61" come from? – glenn jackman Aug 29 '18 at 18:48
  • The mistake is form my end. I gave wrong input. – Arun Prakash Aug 29 '18 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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