Solved

Expect question.  Should be a simple one.

Posted on 2015-02-06
8
130 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
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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 37

Expert Comment

by:Gerwin Jansen
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
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 …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now