2

Following is the code I'm trying to execute with expect in shell script.

send "date +%d%m%y \n"  
set date1 "$expect_out(buffer)"  
puts "$date1"  
expect "#"  

The output I'm getting is

date +%d%m%y  
111217

Is there any way to suppress the command(date +%d%m%y) and only store the output in variale date1?

Kavya Jain
  • 87
  • 1
  • 10

1 Answers1

0

Try this: expect to see 6 digits as a whole word

send "date +%d%m%y\r"         ;# typically we use \r to "hit enter"
expect -re {\m(\d{6})\M}
set date1 "$expect_out(1,string)"  
puts "$date1"  
expect "#"  

More generally, to select the text between the first line (the command) and the last line (the prompt), you can do:

lassign [regexp -inline {.*?\n(.*)\r\n#} $expect_out(buffer)] _ date1

where _ is a throw-away variable that will hold the entire matching text of the regex, and date1 will hold only the captured text.

See https://tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm for Tcl regex syntax.


Without lassign, you could write

regexp {.*?\n(.*)\r\n#} $expect_out(buffer) _ date1
glenn jackman
  • 238,783
  • 38
  • 220
  • 352