Solved

Expect question.  Should be a simple one.

Posted on 2015-02-06
8
136 Views
Last Modified: 2015-02-07
I have the following code, which I would think would be simple, but then again, I'm just trying to figure expect out:

#!/usr/bin/expect -d
set ZERO [lindex $argv 3]
set ONE [lindex $argv 4]
set TWO [lindex $argv 5]
set THREE [lindex $argv 6]
set timeout 360

send_user "$ZERO\r"
send_user "$ONE\r"
send_user "$TWO\r"
send_user "$THREE\r"

I get the following when I run it.  Why don't I get the variables sent to standard output?

testbox-[root]/home/share/daldrid/work/aix/rootpw> exptest.exp test1 test2 test3 test4
expect version 5.39.0
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4
set argc 4
set argv0 "exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file exptest.exp
testbox-[root]/home/share/daldrid/work/aix/rootpw>

Thanks in advance!
David
0
Comment
Question by:David Aldridge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Author Comment

by:David Aldridge
ID: 40594337
How about a link to some good, understandable expect tutorials?
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 125 total points
ID: 40595209
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 125 total points
ID: 40595372
It probably is being sent, but your send_user is sending the text, then a carriage return to put the cursor back at the start of the line - eash send_user then overwrites the previous one, and the last send_user output is overwritten by the shell prompt!

To fix, change all of the "\r" at the end of send_user strings to "\n".

You *do* use "\r" when using "send" to send data to a spawned process.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 250 total points
ID: 40595475
Yes, the variables are  being output. I ran exptext.script under script which captures all terminal I/O, and I can see (some of) them.

Will continue to investigate
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 40595508
Your code does not give correct output, but it does give more than you realized
22:01:11$ script exptext.script
Script started, file is exptext.script
22:01:19$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  
set argc 4
set argv0 "./exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file ./exptest.exp
22:01:31$ exit
exit
Script done, file is exptext.script
22:01:54$ less exptext.script
Script started on Sat 07 Feb 2015 10:01:19 PM EST
22:01:19$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15^M
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  ^M
set argc 4^M
set argv0 "./exptest.exp"^M
set argv "test1 test2 test3 test4"^M
executing commands from command file ./exptest.exp^M
test4^M^M^M^M22:01:31$ exit
exit

Script done on Sat 07 Feb 2015 10:01:54 PM EST
22:02:02$ 

Open in new window


expect is smart enough to not include its option switches or its file argument in the argv list. Also, argv[0] is put in its own variable, so you want to print the argv list starting at element 0.
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 250 total points
ID: 40595511
Here's a minimally changed version of your script, which runs correctly without needing script to see the output
22:16:42$ cat exptest.exp
#!/usr/bin/expect -d
set ZERO [lindex $argv 0]
set ONE [lindex $argv 1]
set TWO [lindex $argv 2]
set THREE [lindex $argv 3]
set timeout 360

send_user "$ZERO\r\n"
send_user "$ONE\r\n"
send_user "$TWO\r\n"
send_user "$THREE\r\n"
22:18:25$ ./exptest.exp test1 test2 test3 test4
expect version 5.44.1.15
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./exptest.exp  argv[3] = test1  argv[4] = test2  argv[5] = test3  argv[6] = test4  
set argc 4
set argv0 "./exptest.exp"
set argv "test1 test2 test3 test4"
executing commands from command file ./exptest.exp
test1
test2
test3
test4
22:18:30$ 

Open in new window

0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40595845
Suggestion: looks like you're learning expect while being logged on as root? I'd try and test things as an ordinary user to prevent unexpected behavior ;)
0
 
LVL 1

Author Comment

by:David Aldridge
ID: 40596388
Some great advice.  I'll split this up for you guys.   Thanks for the help!  As for running it as root, I'm playing around on a Solaris zone that I can blow away or just restore a snapshot for it.  Not real data on it.


Thanks again!
David
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Utilizing an array to gracefully append to a list of EmailAddresses
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question