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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:20:41  来源:igfitidea点击:

cp exits with a 64 error status

bashpackagemaker

提问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 cpreturns 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/dbfor the command to copy files /folder/to/my/dband /Library/Applicationto.

问题是,你没有一个文件夹,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 echowhere 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 >&2to 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 echois 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 codereally 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 脚本中迭代参数