Issue
I have a script I use for building my kernel, and in it I have a function I want to use to catch errors in my build commands, but I can't figure out how to pass multiple arguments as one to get it working properly. The syntax is as follows:
run_build() {
cmd=$($1)
status=${PIPESTATUS[0]}
$*
if ($status != 0); then
echo "$cmd failed!"
exit 1
else
echo "$cmd succeeded."
fi
}
The problem is, when I try to pass a make
command with its arguments, the make
command is being executed with no arguments. I tried to use the "$*" syntax to call all arguments but it doesn't seem to work. An example command would be
make O=out -j8 zImage
I need the next 3 arguments to be included when calling the line from the script. Anyone have ideas?
Solution
First point, you don't want to pass the entire command as a single argument: I'm trying to put a command in a variable, but the complex cases always fail!
This is the main error: cmd=$($1)
-- the $()
syntax invokes Command Substitution that executes the first argument. Just use cmd=$1
to store the first parameter into the "cmd" variable.
Additional errors:
if ($status != 0); then
-- that syntax is incorrect: useif ((status != 0)); then
for proper arithmetic evaluation.$*
-- to execute the positional parameters correctly, use"$@"
(with the quotes) -- that form will maintain any arguments that contain whitespace as intended.
Answered By - glenn jackman Answer Checked By - Marie Seifert (WPSolving Admin)