Bash 脚本错误捕获
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8886065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Script Error Catching
提问by skippr
I am very new to bash scripts, and for my first script attempt I am submitting files to my professor's dropbox within the same server.
我对 bash 脚本非常陌生,对于我的第一次脚本尝试,我将文件提交到同一服务器内教授的保管箱。
The code is this:
代码是这样的:
#! /bin/bash
echo "Submit Lab? \c"
read choice
if [ $choice = "y" ]; then
echo "Sending to Prof's dropbox..."
cp -r /path/to/lab /path/to/dropbox
else
echo "Submission terminated."
fi
Usage for the command is simply "$ submit 1
" (1 is a single integer, corresponding to which lab I want to submit)
命令的用法只是“ $ submit 1
”(1 是一个整数,对应于我要提交的实验室)
The bash script appends the entered argument (a single integer) to the necessary filename (a directory), then submits that directory to my prof's dropbox using cp
.
bash 脚本将输入的参数(单个整数)附加到必要的文件名(目录),然后使用cp
.
More for the sake of learning than of absolute necessity, I wanted to create a clean prompt that will catch any cp
errors (such as file not existing), and be able to output my own error message.
更多的是为了学习而不是绝对必要,我想创建一个干净的提示来捕获任何cp
错误(例如文件不存在),并能够输出我自己的错误消息。
So basically, something like:
所以基本上,类似于:
echo "Submit lab?"
read choice
echo "Send to Prof's dropbox"
cp -rv /path/to/lab /path/to/dropbox
<catch any errors from cp>
if [ cp has errors ]
echo "Submission failed."
else if [ cp has no errors ]
echo "Submission successful."
And so on...
I understand that I could use verbose mode, and that cp
itself prints out errors, but as stated before, I'm doing this for the purpose of learning error catching in bash and being able to make a clean prompt.
我知道我可以使用详细模式,并且它cp
本身会打印出错误,但如前所述,我这样做是为了学习在 bash 中捕获错误并能够做出干净的提示。
Any help would be appreciated.
任何帮助,将不胜感激。
Thanks!
谢谢!
Also:I have a similar script which submits labs from my laptop, using scp
. Assuming I have this question answered, can I apply the solutions to scp exactly the same way?
另外:我有一个类似的脚本,它从我的笔记本电脑提交实验室,使用scp
. 假设我已经回答了这个问题,我可以以完全相同的方式将解决方案应用于 scp 吗?
EDIT:I don't want cp
errors to output to the shell! The most common error would probably be "cannot stat blah: no such file or directory." I would like to catch that error, and simply say "Submission failed."
编辑:我不希望cp
错误输出到 shell!最常见的错误可能是“cannot stat blah: no such file or directory”。我想抓住那个错误,然后简单地说“提交失败”。
EDIT #2:jcollado's answer is correct, the problem is on my part. Did not end the nested if-else
with the proper "fi
."
编辑#2:jcollado 的回答是正确的,问题出在我的身上。没有if-else
以正确的“ fi
.”结束嵌套。
After fixing that up, it worked as intended. Still need to catch the error output but I believe I know where I need to go to figure that out on my own, so this question is answered as far as I'm concerned.
修复后,它按预期工作。仍然需要捕获错误输出,但我相信我知道我需要自己去哪里解决这个问题,所以就我而言,这个问题得到了回答。
FINAL EDIT:Ah, what the heck - the redirect was so easy I figured I'd put the solution on here. If using Linux, simply add "2>/dev/null
" (if you don't want error messages being saved in any file) after the cp
to make "cp -r /first/path /second/path 2>/dev/null
"
最终编辑:啊,这到底是怎么回事——重定向是如此简单,我想我会把解决方案放在这里。如果使用Linux,只需添加“ 2>/dev/null
”(如果你不想错误信息被保存在任何文件中那样)后,cp
使“ cp -r /first/path /second/path 2>/dev/null
”
回答by Jonathan Leffler
Although you can test $?, you can also test the exit status of a command directly:
虽然可以测试 $?,但也可以直接测试一个命令的退出状态:
if cp -rv /path/to/lab /path/to/dropbox
then echo Submission successful
fi
exit $?
The errors were already reported to standard error by cp
. The exit
shown will exit with status 0 (success) if cp
was successful; otherwise, it exits with the status that cp
exited with.
错误已由 报告给标准错误cp
。该exit
显示将退出状态为0(成功),如果cp
成功; 否则,它以退出时的状态cp
退出。
Clearly, you can wrap that in a loop if you want to, or you can make it non-interactive (but any exit terminates the script).
显然,如果需要,您可以将其包装在一个循环中,或者您可以使其非交互式(但任何退出都会终止脚本)。
回答by jcollado
To check the return code from the previous command, you can use the $?
special variable as follows:
要检查上一个命令的返回码,您可以使用$?
特殊变量,如下所示:
if [ "$?" -ne "0" ]; then
echo "Submission failed"
exit 1
fi
echo "Submission successful."
Additionally, if you want a clean prompt, you can redired stderr
as explained here.
此外,如果您想要一个干净的提示,您可以stderr
按照此处的说明重新发送。
Finally, you can also use this in the script that uses scp
, since it works regardless of the command you're using.
最后,您还可以在使用 的脚本中使用scp
它,因为无论您使用什么命令,它都可以工作。
回答by chemila
echo "Submit lab?"
read choice
echo "Send to Prof's dropbox"
cp -rv /path/to/lab /path/to/dropbox &>/dev/null
[ $? -eq 0 ] && { #success, do something } || { #fail, do something }
And so on...
等等...