Bash脚本错误捕获 [英] Bash Script Error Catching

查看:203
本文介绍了Bash脚本错误捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对bash脚本非常陌生,对于第一次脚本尝试,我正在将文件提交到同一服务器内教授的保管箱中。

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.

代码是这样的:

#! /bin/bash
echo "Submit Lab$1? \c"

read choice

if [ $choice = "y" ]; then
   echo "Sending to Prof's dropbox..."
   cp -r /path/to/lab$1 /path/to/dropbox
else
   echo "Submission terminated."
fi

该命令的用法只是 $ Submit 1 (1是一个整数,与我要提交的实验室相对应)

Usage for the command is simply "$ submit 1" (1 is a single integer, corresponding to which lab I want to submit)

bash脚本会附加输入的参数(一个整数)到必要的文件名(目录),然后使用 cp 将该目录提交到我的教授的投递箱中。

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.

为了学习而不是绝对必要,我想创建一个干净的提示来捕获任何 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.

所以基本上,类似:

echo "Submit lab$1?"

read choice

echo "Send to Prof's dropbox"
cp -rv /path/to/lab$1 /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...

我知道我可以使用冗长的词模式,并且 cp 本身会打印出错误,但是如前所述,我这样做是为了学习bash中的错误捕获并能够发出干净提示。

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.

任何帮助将不胜感激。

谢谢!

也:我有一个类似的脚本,它使用 scp 。假设我已经回答了这个问题,我可以完全一样地将解决方案应用于scp吗?

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?

编辑:我不希望 cp 错误输出到外壳!最常见的错误可能是无法统计:没有这样的文件或目录。我想捕获该错误,只是说提交失败。

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."

编辑#2: jcollado的回答是正确的,问题是对我而言。没有以正确的 fi 结束嵌套的 if-else

EDIT #2: jcollado's answer is correct, the problem is on my part. Did not end the nested if-else with the proper "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.

最终编辑:嗯,到底是什么-重定向是如此简单,我想我将解决方案放在这里。如果使用Linux,只需在之后添加 2> / dev / null (如果您不希望将错误消息保存在任何文件中)。 cp 制作 cp -r / first / path / second / path 2> / dev / null

FINAL 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"

推荐答案

尽管可以测试$ ?,但是也可以直接测试命令的退出状态:

Although you can test $?, you can also test the exit status of a command directly:

if cp -rv /path/to/lab$1 /path/to/dropbox
then echo Submission successful
fi
exit $?

错误已由 cp 。如果 cp 成功,显示的 exit 将以状态0(成功)退出;否则,它会以 cp 退出的状态退出。

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.

很显然,如果您可以,也可以使其不交互式(但任何退出都会终止脚本)。

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).

这篇关于Bash脚本错误捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆