Solved

Expect question.  Should be a simple one.

Posted on 2015-02-06
8
138 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 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