I'm having this poblem with a expect script. I want to capture when the output of a command is "0" and distinguish from other numbers or chains like "000" or "100"
my code is:
#!/usr/bin/expect -f
set timeout 3
spawn bash send "echo 0\n"
expect {
-regexp {^0$} { send_user "\nzero\n" }
-re {\d+} { send_user "\number\n"}
}
send "exit\n"
I have the follwing response:
spawn bash
echo 0
number
But the following regex don't work:
-regexp {^0$} { send_user "\nzero\n" }
if I change it to:
-regexp {0} { send_user "\nzero\n" }
works, but it also capture "00" "10" "1000" etc.
send "echo 100\n"
expect {
-regexp {0} { send_user "\nzero\n" }
-re {\d+} { send_user "\nnumber\n"}
}
Result:
spawn bash
echo 10
zero
I don't know what I'm doing wrong and I didn't found any from help on google. I also searched for similar problems resolved here I couldn't make any of it work on my code.
Update:
I tried the proposed fix code:
#!/usr/bin/expect -f
set timeout 3
spawn bash
send "echo 0\n"
expect {
-regexp {\b0\b} { send_user "\nzero\n" }
-re {\d+} { send_user "\nnumber\n"}
}
send "exit\n"
But I still having this response:
spawn bash
echo 0
number
Update 2
I also tried this line:
-regexp {"^0\n$"} { send_user "\nzero\n" }
But I still having the same results.
Thanks in advance
I could resolve the problem:
#!/usr/bin/expect -f
set timeout 3
spawn bash
send "echo 0\n"
expect -re "(\\d+)" {
set result $expect_out(1,string)
}
if { $result == 0 } {
send_user "\nzero\n";
} else {
send_user "\nnumber\n";
}
send "exit\n"