bash cp 以 64 错误状态退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25790736/
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
cp exits with a 64 error status
提问by arnaud
I am using a preflight bash script in packagemaker:
我在packagemaker 中使用了预检 bash 脚本:
run cp -pf "/folder/to/my/db" "/Library/Application Support/app/db
run cp -pf "/folder/to/my/db" "/Library/Application Support/app/db
The run function (that I found on StackOverflow by the way) :
运行函数(我在 StackOverflow 上找到的):
run() { $*; code=$?; [ $code -ne 0 ] && echo "command [$*] failed with error code $code\nERROR: $@\n"; }
run() { $*; code=$?; [ $code -ne 0 ] && echo "command [$*] failed with error code $code\nERROR: $@\n"; }
The command cp
returns a 64 code. What is this 64 status please?
How can I resolve that?
该命令cp
返回 64 代码。请问这个64状态是什么?我该如何解决?
回答by Jonathan Leffler
The problem is that you don't have a folder Support/app/db
for the command to copy files /folder/to/my/db
and /Library/Application
to.
问题是,你没有一个文件夹,Support/app/db
该命令复制文件/folder/to/my/db
和/Library/Application
到。
Replace the misguided (almost always wrong) $*
with the correct "$@"
:
用$*
正确的替换被误导的(几乎总是错误的)"$@"
:
run()
{
"$@"
code=$?
[ $code -ne 0 ] && echo "command [$*] failed with error code $code\nERROR: $@\n"
}
Plain $*
breaks words at spaces; "$@"
preserves spaces in arguments. Most often, $*
is not the right notation (though it would be fine in the echo
where you used $@
). It isn't clear to me why the command's arguments are being listed twice in the error message.
平原$*
在空格处断词;"$@"
保留参数中的空格。大多数情况下,$*
不是正确的表示法(尽管echo
在您使用的地方会很好$@
)。我不清楚为什么命令的参数在错误消息中列出了两次。
The error reporting would be improved by adding >&2
to the end to redirect the output to standard error, which is where error messages belong. (I'd remove the repetition while I'm at it.) Note that using $*
inside the argument to echo
is entirely appropriate.
通过添加>&2
到末尾以将输出重定向到标准错误,错误报告将得到改进,这是错误消息所属的地方。(当我在做的时候,我会删除重复。)请注意,$*
在参数中使用toecho
是完全合适的。
[ $code -ne 0 ] && echo "command [$*] failed with error code $code" >&2
In fact, the run()
function can be simplified still more; the variable code
really isn't needed:
事实上,run()
功能还可以进一步简化;code
确实不需要变量:
run()
{
"$@" || echo "command [$*] failed with error code $?" >&2
}
If you want the script to exit too, then you can use:
如果您也希望脚本退出,则可以使用:
run()
{
"$@" || { echo "command [$*] failed with error code $?" >&2; exit 1; }
}
The { ...; }
notation treats the commands within as a unit for I/O redirection without starting a sub-shell.
该{ ...; }
符号将其中的命令视为 I/O 重定向的一个单元,而无需启动子 shell。
See also How to iterate over arguments in a Bash script.
另请参阅如何在 Bash 脚本中迭代参数。