Assigning last line to variable in expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning last line to variable in expect
# 1  
Old 02-04-2010
Assigning last line to variable in expect

As part of an expect script, I have to convert a strange user ID to a conventional UNIX ID. To do this, I read the contents of a file and do a little awk magic. Here's that bit of the expect script:

Code:
send "awk 'NF == 10  && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info\r"
expect "\$ $"

Let's say that the output of the awk command is "bob". Once executed, afaik, the above causes the contents of expect_out(buffer) to be exactly:

Code:
awk 'NF == 10  && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info
bob
$

I can assign *all* of the buffer to a variable:

Code:
send "awk 'NF == 10  && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info\r"
 expect "\$ $"
set username $expect_out(buffer)

But all I want to assign to the variable is the last line before the prompt -- "bob". How do I do that? If it matters, I'm a little unclear about whether the prompt is actually in expect_out(buffer) or not. I might be asking how to save only the last line, or perhaps only the second-to-last. Please pardon my ignorance-- this really is my first attempt at an expect script, and the assignment of portions of output to a variable seems potentially useful.

---------- Post updated 02-04-10 at 11:24 AM ---------- Previous update was 02-03-10 at 07:11 PM ----------

(Moved thread to "Shell Programming..." from "...for Dummies")

---------- Post updated at 01:23 PM ---------- Previous update was at 11:24 AM ----------

Ah, ok... I think we have a winner. I learned about lindex and split, and so I added:

Code:
set resp1 $expect_out(buffer)
set resp2 [split $resp1 "\r"]
set servername [lindex $resp2 1]

That does the trick... now $servername can be called as needed later in the script. It's a little offensive to do it in two steps like that... I'll look into combining when I have time to brush up on TCL/Expect syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it. Background context: Post processing LAMMPS simulation data. tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles,... (10 Replies)
Discussion started by: ThomasP
10 Replies

2. Shell Programming and Scripting

Expect - assigning UNIX command output to a variable

Hi, I'm writing a script that connects through ssh (using "expect") and then is supposed to find whether a process on that remote machine is running or not. Here's my code (user, host and password are obviously replaced with real values in actual script): #!/usr/bin/expect set timeout 1... (3 Replies)
Discussion started by: oseri
3 Replies

3. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

4. Shell Programming and Scripting

Assigning Variable to Line Numer in nl

hi, i'm creating a little menu for some users. i'm running the command: du -a /apps | sort -n -r | head -n 10 | nl i then get the top 10 files by size in the /apps directory the output is like this: 1 101415752 /apps 2 89188064 /apps/userA 3 74521335 ... (1 Reply)
Discussion started by: horhif
1 Replies

5. Shell Programming and Scripting

EXPECT: Assign variable by reading a line of text from a file

Hi All, I have been using a program on windows called AutoKey. My environment at work is Linux and I have been experimenting with expect. Very powerful. I can move my AutoKey scripts to Linux using Expect once I am educated on how to read from a file using Expect. My application would be... (1 Reply)
Discussion started by: quemalr
1 Replies

6. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

7. Shell Programming and Scripting

how to Read a file and assigning each line to a variable?

Friends, I have a file output.txt with values as below: 092307135717 061910135717 I want to know how to read this file and then assign each value to a variable. say like var1=092307135717 var2=061910135717 So that I can use this VAR1 and Var2 in the shell script for further processing.... (3 Replies)
Discussion started by: shyamaladevi
3 Replies

8. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

9. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

10. Shell Programming and Scripting

Assigning value to a variable

Is there any difference between: set variable=39 and variable=39 (1 Reply)
Discussion started by: proactiveaditya
1 Replies
Login or Register to Ask a Question