I am using a preflight bash script in packagemaker :
run cp -pf "/folder/to/my/db" "/Library/Application Support/app/db
The run function (that I found on StackOverflow by the way) :
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?
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.Replace the misguided (almost always wrong)
$*
with the correct"$@"
:Plain
$*
breaks words at spaces;"$@"
preserves spaces in arguments. Most often,$*
is not the right notation (though it would be fine in theecho
where you used$@
). It isn't clear to me why the command's arguments are being listed twice in the error message.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 toecho
is entirely appropriate.In fact, the
run()
function can be simplified still more; the variablecode
really isn't needed:If you want the script to exit too, then you can use:
The
{ ...; }
notation treats the commands within as a unit for I/O redirection without starting a sub-shell.See also How to iterate over arguments in a Bash script.