Fix the use of $@ in all shell scripts
This patch allows all our development scripts, as well as ndk-gdb
to work well when parameters containing spaces are used.
The $@ shell variable is very special because it will expand
differently if inside a quoted string. What this means is that,
while $@ normally corresponds to the list of all parameters to
a function or script, we have:
$@ -> expands to a single string
"$@" -> expands to a series of strings
This is important when using them as parameters to other commands
for example consider:
# Simple function to dump all parameters, one per line.
dump () {
for ITEM; do
echo "$ITEM"
done
}
dump1 () { # this will always print a single line
dump $@
}
dump2 () { # this will print one line per item
dump "$@"
}
dump1 aaa bbb ccc # prints "aaa bbb ccc" on a single line
dump2 aaa bbb ccc # prints three lines: "aaa", "bbb" and "ccc"
Generally speaking, one should always use "$@" except in very rare cases
(e.g. when called from an eval command).
Change-Id: I87ec2a7b078cbe463f0ec0257ecad8fd38835b2e
diff --git a/ndk-gdb b/ndk-gdb
index e6cf7af..efd9680 100755
--- a/ndk-gdb
+++ b/ndk-gdb
@@ -304,9 +304,9 @@
# Run the command, while storing the standard output to CMD_OUT
# and appending the exit code as the last line.
if [ "$REDIRECT_STDERR" != 0 ]; then
- $ADB_CMD shell $@ ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
+ $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
else
- $ADB_CMD shell $@ ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
+ $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
fi
# Get last line in log, which contains the exit code from the command
RET=`sed -e '$!d' $CMD_OUT`
@@ -330,14 +330,14 @@
# or 1 (failure) otherwise.
adb_var_shell ()
{
- _adb_var_shell 0 $@
+ _adb_var_shell 0 "$@"
}
# A variant of adb_var_shell that stores both stdout and stderr in the output
# $1: Variable name
adb_var_shell2 ()
{
- _adb_var_shell 1 $@
+ _adb_var_shell 1 "$@"
}
# Return the PID of a given package or program, or 0 if it doesn't run