如何获得衍生进程的退出code在期望的shell脚本? [英] How to get the exit code of spawned process in expect shell script?
问题描述
我试图执行一个执行的expect脚本以及衍生的进程有退出code在它的脚本。但我无法得到衍生进程主脚本的退出code。我总是得到零成功。
I am trying to execute a script that executes an EXPECT script and a spawned process which has exit code in it. But I'm unable to get the exit code of the spawned process to main script. I'm always getting zero as success.
期望脚本是:
[Linux Dev:anr ]$ cat testexit.sh
#!/bin/bash
export tmp_script_file="/home/anr/tmp_script_temp.sh"
cp /home/anr/tmp_script $tmp_script_file
chmod a+x $tmp_script_file
cat $tmp_script_file
expect << 'EOF'
set timeout -1
spawn $env(tmp_script_file)
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
}
EOF
echo "spawned process status" $?
rm -f $tmp_script_file
echo "done"
衍生的脚本:
[Linux Dev:anr ]$ cat tmp_script
exit 3
Expect脚本的执行:
Execution of Expect script:
[Linux Dev:anr ]$ ./testexit.sh
exit 3
spawn /home/anr/tmp_script_temp.sh
spawned process status 0
done
但问题是我无法获得出口产生了回报code期望脚本。我想催生脚本的退出code 3到主脚本和主脚本应该与退出code 3退出。
Problem is I am unable to get the spawned exit return code to expect script. I want the exit code 3 of spawned script to main script and main script should be exit with exit code 3.
请帮我拿到催生出code到主脚本。
Please help me to get the spawned exit code to main script.
推荐答案
随着格伦的帮助下,我得到的解决方案..和我最后的脚本是:
With the help of glenn, I got solution.. and my final script is::
预计脚本
[Linux Dev:anr ]$ cat testexit.sh
#!/bin/bash
export tmp_script_file="/home/anr/tmp_script_temp.sh"
cp /home/anr/tmp_script $tmp_script_file
chmod a+x $tmp_script_file
cat $tmp_script_file
expect << 'EOF'
set timeout -1
spawn $env(tmp_script_file)
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
eof
}
foreach {pid spawnid os_error_flag value} [wait] break
if {$os_error_flag == 0} {
puts "exit status: $value"
exit $value
} else {
puts "errno: $value"
exit $value
}
EOF
echo "spawned process status" $?
rm -f $tmp_script_file
echo "done"
衍生的脚本:
[Linux Dev:anr ]$ cat tmp_script
exit 3
Expect脚本的执行:
Execution of Expect script:
[Linux Dev:anr ]$ ./testexit.sh
exit 3
spawn /home/anr/tmp_script_temp.sh
exit status: 3
spawned process status 3
done
感谢格伦再次..
Thanks Glenn once again..
这篇关于如何获得衍生进程的退出code在期望的shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!