Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-21-2007, 06:28 AM   #1
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
can expect be called from within a bash script?


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?

Old 06-21-2007, 06:46 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
As it runs under bash, that makes perfect sense ...
Old 06-21-2007, 06:54 AM   #3
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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:

       -- 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.

Last edited by; 06-21-2007 at 06:55 AM.
Old 06-21-2007, 06:59 AM   #4
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
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:


bash stuff  

expect spawn ...

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.
Old 06-21-2007, 09:42 AM   #5
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
expect/bash sk8guitar Programming 5 10-06-2009 12:00 PM
Combined bash and expect script Paul77 Linux - Software 11 03-08-2007 09:17 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 12:50 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration