LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 15
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.
 
Old 06-21-2007, 07:46 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
As it runs under bash, that makes perfect sense ...
 
Old 06-21-2007, 07:54 AM   #3
wjevans_7d1@yahoo.co
Member
 
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:

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.

Last edited by wjevans_7d1@yahoo.co; 06-21-2007 at 07:55 AM.
 
Old 06-21-2007, 07:59 AM   #4
johnpaulodonnell
Member
 
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:

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

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


Reply


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 01:00 PM
Combined bash and expect script Paul77 Linux - Software 11 03-08-2007 10:17 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM
cd keeps snapping back to current directory when called from bash script ghrellin Linux - General 5 08-28-2005 01:50 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 04:08 AM.

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