LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can expect be called from within a bash script? (https://www.linuxquestions.org/questions/programming-9/can-expect-be-called-from-within-a-bash-script-563424/)

johnpaulodonnell 06-21-2007 06:28 AM

can expect be called from within a bash script?
 
Hi.

As a general question, can you run expect commands from within a bash script?

In the middle of a bash script is it possible to stick in a few expect commands to do some automated interaction and then carry on in bash?

Or is this wishful thinking?

Thanks.

chrism01 06-21-2007 06:46 AM

As it runs under bash, that makes perfect sense ...

wjevans_7d1@yahoo.co 06-21-2007 06:54 AM

My experience with expect is limited and ancient, but yes, you can do this. The easiest way, IMO, is to use expect scripts. The man page discusses how the first line of an expect script could look:

Code:

      -- may be used to delimit the end of the options.  This is
      useful if you want to pass an option-like argument to your
      script without it being interpreted by Expect.  This can
      usefully be placed in the #! line to prevent any flag-like
      interpretation by Expect.  For example, the following will
      leave the original arguments (including the script name)
      in the variable argv.

          #!/usr/local/bin/expect --

      Note that the usual getopt(3) and execve(2) conventions
      must be observed when adding arguments to the #! line.

Of course, you'd use #!/usr/bin/expect.

Hope this helps.

johnpaulodonnell 06-21-2007 06:59 AM

ok, that sounds good.

Wanted to know to see whether I should learn it.

So I can just whack in an expect block of commands like so:

Code:

#!/bin/bash

bash stuff 

expect spawn ...
send
expect
etc

back to bash stuff

One more question - can expect access variables I've defined at the beginning of the bash script?...I'm hoping to set it up such that expect is invoked within a bash script for loop, so I'm hoping expect can read $var

Thanks for your help.

nx5000 06-21-2007 09:42 AM

You can probably mix but it will get unreadable.
In bash you can call your script like this:
/path/to/blah/myexpectscript $var1 $var2

then in myexpectscript:
#!/usr/bin/expect -f

set var1 [lindex $argv 1]
puts "var1=$var1"


All times are GMT -5. The time now is 07:17 AM.