Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing an if statement after a specific output in expect script

Tags:

linux

expect

Is there a way to execute an if statement if I see a specific output? For example, when the console says "bad interpreter permission denied" I want to execute a command like "dos2unix file_name"?

So the logic will be like the following,

if (output is "bad interpreter permission denied")
{
  send dos2unix file_name
}
fi

This is an expect script.

Edit:

Could I do something like this in an expect script?

if (grep -cim1 '^M$' lruload.sh) -eq 1; then
send dos2unix filename
fi
like image 812
tiger_groove Avatar asked May 29 '26 03:05

tiger_groove


1 Answers

When you say execute the commands, I hope you meant to execute the command in the shell. You can use exec command for this purpose.

I'm not sure where you are interacting. I mean like telnet or ftp or bash. Anyway, under any case, you will be sending a command and expecting a prompt.

send "command 1"
expect "prompt"
send "command 2"
expect {
    timeout { puts "timeout happened"}

    "bad interpreter per mission denied" {
         set result [exec dos2unix <filename>]
     }
}
# if need to intact with three application, further use 'send' and 'expect'

You have the result variable to store the dos2unix output.

like image 70
Dinesh Avatar answered May 31 '26 20:05

Dinesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!