Save an specific part of a expect_out in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save an specific part of a expect_out in a variable
# 1  
Old 02-28-2017
Save an specific part of a expect_out in a variable

I have a expect file like this
Code:
#!/opt/tools/unsupported/expect-5.39/bin/expect

spawn ssh -l user ip
expect_after eof {exit 0}
set timeout 10
log_file /report.txt

expect "Password:" { send "pasword\r" }
expect "$ " { send "date\r" }

expect "$ " { send "readlink /somelink\r" }

set CCM_BUILD $expect_out(buffer)

send_log "CCM: $CCM_BUILD"

expect "$ " { send "date\r" }
expect "$ " { send "exit\r" }
~

The result of the readlink will be a number like 447. that's what I want in my log file. but what I'm getting is:
Code:
Password:
Last login: Tue Feb 28 09:49:42 2017 from gbplr6gn01.genband.com^M
^[[?1034h-bash-4.1$ date
Tue Feb 28 09:50:42 CST 2017
-bash-4.1$ CCM: readlink /localdisk2/jenkins/jobs/CCM/jobs/Deploy_CCM_build/builds/lastSuccessfulBuild
447
-bash-4.1$ date
Tue Feb 28 09:50:42 CST 2017
-bash-4.1$

how can I get just CCM: 447?
# 2  
Old 02-28-2017
Code:
echo "CCM: $(readlink /localdisk2/jenkins/jobs/CCM/jobs/Deploy_CCM_build/builds/lastSuccessfulBuild)"

# 3  
Old 02-28-2017
echo doesn't work with expect
# 4  
Old 02-28-2017
Quote:
Originally Posted by bebehnaz
echo doesn't work with expect
I guess I don't understand something very basic what you're after..
# 5  
Old 02-28-2017
From the EXPECT manual: "any matching and previously unmatched output is saved in the variable expect_out(buffer)"

You could use regexp to do a substring match of that, or just use expect again
My expect is rusty, try something like this:

Code:
expect "$ " { send "readlink /somelink\r" }
expect -re {\n([0-9]*)}

set CCM_BUILD $expect_out(1,string)

# 6  
Old 02-28-2017
Quote:
Originally Posted by neutronscott
From the EXPECT manual: "any matching and previously unmatched output is saved in the variable expect_out(buffer)"

You could use regexp to do a substring match of that, or just use expect again
My expect is rusty, try something like this:

Code:
expect "$ " { send "readlink /somelink\r" }
expect -re {\n([0-9]*)}

set CCM_BUILD $expect_out(1,string)

nope didn't work Smilie gave me the same output
# 7  
Old 03-01-2017
Try this:

Code:
#!/opt/tools/unsupported/expect-5.39/bin/expect --

set timeout 10
match_max 256
expect_after eof {exit 0}
log_file -noappend report.txt

puts -nonewline "Fetching link from ip..."
spawn -noecho ssh -l user ip
log_user 0

expect "Password:" { send "pasword\r" }
expect "$ " { send "date\r" }

expect "$ " { send "readlink /somelink\r" }
expect -r "readlink .*\r\n(.*)\r\n" {
    set CCM_BUILD $expect_out(1,string)
}

send_log "CCM: $CCM_BUILD"
puts ""
expect "$ " { send "date\r" }
expect "$ " { send "exit\r" }

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to download specific files and save in two folders

I am trying to download all files from a user authentication, password protected https site, with a particular extension (.bam). The files are ~20GB each and I am not sure if the below is the best way to do it. I am also not sure how to direct the downloaded files to a folder as well as external... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

3. Shell Programming and Scripting

A simpler way to do this (save a list of files based on part of their name)

Hello, I have a script that checks every file with a specific extension in a specific directory. The file names contain some numerical output and I am recording the file names with the best n outcomes. The script finds all files in the directory with the extension .out.txt and uses awk to... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

4. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

5. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

6. Shell Programming and Scripting

expect_out buffer no such variable running script background

I am trying to use send and receive using expect. the expect_out(buffer) is working fine while it is running it as foreground. But the same script when it is ran as background, the expect_out(buffer) errored out. Is there any factor influence when we run script in foreground and in background? ... (0 Replies)
Discussion started by: shellscripter
0 Replies

7. Shell Programming and Scripting

Save output in a variable

Hi, I have a 3rd party tool on Solaris 8. I am running it thorugh my script but I am not able to capture its output into a variable. Like, I tried, but did not work. output=`/usr/bin/myTool` echo $output Also, I tried saving in a file but when I run, output is always shown on the... (19 Replies)
Discussion started by: angshuman_ag
19 Replies

8. Shell Programming and Scripting

Set specific part in command output into variable

I am trying unsuccessfully to set into a variable a specific part of command output: The command output will be as: line 1: <varied> line 2: 2 options: option 1: Set view: ** NONE ** or option 2: Set view: <different_name_of_views_always_without_spaces> and I would like to get into... (7 Replies)
Discussion started by: orit
7 Replies

9. UNIX for Dummies Questions & Answers

Save contents of a Variable

I want to save the contents of a variable to a file. How can that be achieved? I have tried with: echo $varname > textfile.txt but for some reason it does not print anything. (1 Reply)
Discussion started by: carl_vieyra
1 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question