当python脚本失败时,如何停止bash脚本的执行?
因此,我的shell脚本中包含以下内容:当python脚本失败时,如何停止bash脚本的执行?,python,bash,error-handling,sh,Python,Bash,Error Handling,Sh,因此,我的shell脚本中包含以下内容: python get_link.py $password | wget --content-disposition -i- mkdir web_folder mv *.zip web_folder 因此,第一行是执行一个python脚本,我编写了这个脚本,打印出一个网站链接,wget立即检索python脚本返回的链接并下载一个zip文件。 第二行创建一个名为“web\u文件夹”的新文件夹,第三行将wget下载的zip文件移动到“web\u文件夹”中 我
python get_link.py $password | wget --content-disposition -i-
mkdir web_folder
mv *.zip web_folder
因此,第一行是执行一个python脚本,我编写了这个脚本,打印出一个网站链接,wget立即检索python脚本返回的链接并下载一个zip文件。
第二行创建一个名为“web\u文件夹”的新文件夹,第三行将wget下载的zip文件移动到“web\u文件夹”中
我面临的问题是,如果python脚本由于错误而失败,例如$password的密码错误,那么shell脚本命令的其余部分仍在执行。就我而言,打印了以下内容:
mv: cannot stat ‘*.zip’: No such file or directory
即使python脚本失败,mkdir和mv命令仍会执行。当python脚本失败时,如何确保脚本完全停止?如果您使用的是bash,请查看PIPESTATUS变量。 ${PIPESTATUS[0]}将具有来自第一个管道的返回代码
#!/bin/bash
python get_link.py $password | wget --content-disposition -i-
if [ ${PIPESTATUS[0]} -eq 0 ]
then
echo "python get_link.py successful code here"
else
echo "python get_link.py failed code here"
fi
一个紧凑的解决方案,将所有内容与和链接在一起:
(python get_link.py $password | wget --content-disposition -i-) && (mkdir web_folder) && (mv *.zip web_folder)
不太紧凑的解决方案:
python get_link.py $password | wget --content-disposition -i-
if [ $? -eq 0 ]; then
mkdir web_folder
mv *.zip web_folder
fi