LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-30-2014, 01:15 PM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Lightbulb For Loop Containing Bash within an Expect Script


My Expect script looks as shown below:

my.exp:

Code:
#!/usr/bin/expect -f
set M [lindex $argv 0]

spawn echo "stats <$M>" \| debugfs.ocfs2 -n /dev/HPEVA/OCFS2

expect "END"
send "q\r"

So, I am passing a parameter to the Expect script. Since debugfs.ocfs2 will sit at the "END" prompt line I am sending it "q\r" so that it can be closed / quitted automatically.


Here is what happens when I run the above script:

Code:
./my.exp M000000000000000065702b00000000
spawn echo stats <M000000000000000065702b00000000> | debugfs.ocfs2 -n /dev/HPEVA/OCFS2
stats <M000000000000000065702b00000000> | debugfs.ocfs2 -n /dev/HPEVA/OCFS2
send: spawn id exp4 not open
while executing
"send "q\r""
(file "./my.exp" line 8)
I have been looking up the error on Google and did not find any one particular cause of "spawn id exp4 not open". I do now understand that the previous command may have crashed or too early closed for the send command to send anything to it.

Please Note: The main problem / question / requirement is we have a large number of those values M00*(M000000000000000065702b00000000) and we need to run the debugfs command for each of them. So I wrote this small for loop script and called it a wrapper script that will call the expect script passing it a new value each time:

wrapper.sh:
Code:
for M in `grep Lockres /root/fslock |grep M0 |awk '{print $2}'`; do 

./my.exp $M ; 
done
The debugfs command shows the output and then sits at "END" that is why I am using Expect to send it "q\r".


Can I run that for loop in Expect itself and walk through each value?


Do I need to put an extra "echo" in this line?

Code:
spawn echo echo "stats <$M>" \| debugfs.ocfs2 -n /dev/HPEVA/OCFS2
so that it becomes:

Code:
echo stats <M000000000000000065702b00000000> | debugfs.ocfs2 -n /dev/HPEVA/OCFS2

I can't test that thing right now. Actually that is what my friend wants to do and he has the system so I will have to wait until morning when he goes to office.

Last edited by devUnix; 09-30-2014 at 01:20 PM.
 
Old 09-30-2014, 10:05 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
I don't know a lot about expect, but assuming
Code:
spawn echo "stats <$M>" \| debugfs.ocfs2 -n /dev/HPEVA/OCFS2
is equivalent to running
Code:
echo "stats <$M>" | debugfs.ocfs2 -n /dev/HPEVA/OCFS2
in the shell, your problem is that it won't accept any more input: echo will close the debugfs.ocfs2 input pipe after it finishes. You probably want something like this:

Code:
#!/usr/bin/expect -f
set M [lindex $argv 0]

spawn debugfs.ocfs2 -n /dev/HPEVA/OCFS2

send "stats <$M>\r"
expect "END"
send "q\r"
Or, if debugfs.ocfs2 isn't timing sensitive, perhaps you don't need expect at all, just a simple shell script:

Code:
for M in `grep Lockres /root/fslock |grep M0 |awk '{print $2}'`; do 
    { echo "stats <$M>"; echo "q"; } | debugfs.ocfs2 -n /dev/HPEVA/OCFS2; 
done
 
Old 10-01-2014, 11:37 PM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by ntubski View Post

for M in `grep Lockres /root/fslock |grep M0 |awk '{print $2}'`; do
{ echo "stats <$M>"; echo "q"; } | debugfs.ocfs2 -n /dev/HPEVA/OCFS2;
done


That does not work. "END" still keeps sitting there for a manual "q" and "return key stroke".

I liked your expect script and think it does the work. Thanks a lot! I only needed to change the logic.
 
  


Reply



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
Working Expect script failing when placed inside bash Loop. Sigmaflux Linux - Newbie 1 01-27-2014 10:51 PM
Expect script with loop does not work zongbot Programming 5 08-31-2011 01:19 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
expect script - for argument loop ? nilleso Linux - Software 3 09-16-2009 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 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
Open Source Consulting | Domain Registration