[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Expect question. Should be a simple one.

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
David Aldridge
Asked:
David Aldridge
4 Solutions
 
David AldridgeAuthor Commented:
How about a link to some good, understandable expect tutorials?
0
 
RobSampsonCommented:
0
 
simon3270Commented:
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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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
 
David AldridgeAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now