0

I have a script which is on remote server which returns exit status of the script. Now I want to execute that remote script using expect and get the exit status of the script.

My remote server script

 #!/usr/bin/ksh
 echo "Test script"
 exit 1

My expect script on local server

#!/usr/bin/ksh
expect -c "
log_user 0
spawn ssh abc@abc /var/tmp/test.ksh
expect { password:test }
log_user 1
expect 100%
"

Output of the expect script

Test script

But I also want the exit status of my remote script i.e 1. How can i achieve this using expect.

1 Answer 1

1

Some wait(2) system call (maybe instead waitpid or wait4; these may or may not be the same manual page depending on the flavor of unix) obtains the exit status word. In expect this is exposed via the wait procedure documented in the expect(1) man page. For example a process can be written that exits with a psuedo-random exit code, and that value obtained from the return value of wait.

#!/usr/bin/env expect

spawn -noecho \
  expect -c {set code [expr { int(100*rand()) }]; puts "hi $code"; exit $code}
expect -ex "hi"
expect eof

set exunt [wait -i -1]
puts $exunt

set child_exit_code [lindex $exunt end]
puts $child_exit_code

This when run should produce something like

$ expect ./expectexitcode 
hi 78
700 ðӶ 0 78
78
$ 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.