Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Example: In a shell script I open a SSH tunnel whit this:

/usr/bin/expect <<EOD
set timeout -1
spawn ssh -fNL localhost:1873:localhost:873 HOST
expect "*?assword:" { send "$SSHPASS\r"}
sleep 2
exit [exp_pid]
EOD

export SSH_PID=$?
echo $SSH_PID

now $SSH_PID has some number, but it is not the PID of the spawned ssh. I tried different ways I found but non worked. Any Idea?

share|improve this question
add comment

1 Answer

Exit status are 8 bit intergers (though most shells (ksh93 being an exception) will truncate it to 7 bits as they use bit 8 for signaled processes), so it will only work if the pid is between 0 and 127. Get the pid from a pipe instead of the exit status:

SSH_PID=$(
expect <<EOD
...
spawn -noecho ssh ...
puts [exp_pid]
...
EOF
)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.