如果wget
命令在没有更改的情况下以有效的方式失败,我很乐意返回确切的值。
exit #?
可以wget
输出返回的值吗?
实施例
# If it succeeds, then wget returns zero instead of non zero
## 0 No problems occurred.
## 1 Generic error code.
## 2 Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’...
## 3 File I/O error.
## 4 Network failure.
## 5 SSL verification failure.
## 6 Username/password authentication failure.
## 7 Protocol errors.
## 8 Server issued an error response.
wget https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png -o test.img
if [ $? -ne 0 ]
then
# exit 16 # failed ends1 <== This doesn't tell anything
exit #?
fi
答案 0 :(得分:1)
wget https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png -o test.img
# grab wget's exit code
exit_code=$?
# if exit code is not 0 (failed), then return it
test $exit_code -eq 0 || exit $exit_code
答案 1 :(得分:0)
Bash的-e
选项可以做你想做的事:
如果管道(可能包含单个简单命令),列表或复合命令,则立即退出(参见 SHELL GRAMMAR ),以非零状态退出。
了解
也很重要Bash的退出状态是脚本中执行的最后一个命令的退出状态。
我对Bash 4.4的实验表明,即使调用陷阱处理程序,也会返回失败命令的退出状态:
$ ( trap 'echo $?' ERR; set -e; ( exit 3 ) ; echo true ) ; echo $?
3
3
所以你可以写:
#!/bin/bash
url=https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png
set -e
wget -o test.img "$url"
set +e # if you no longer want exit on fail
对于脚本中的一个命令,您可能更喜欢显式测试并退出,如下所示:
wget -o test.img "$url" || exit $?