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/build/core/ndk-common.sh b/build/core/ndk-common.sh
index 2253bf5..cff2fa3 100644
--- a/build/core/ndk-common.sh
+++ b/build/core/ndk-common.sh
@@ -138,13 +138,13 @@
{
if [ "$VERBOSE" = "yes" ] ; then
echo "## COMMAND: $@"
- $@ 2>&1
+ "$@" 2>&1
else
if [ -n "$TMPLOG" ] ; then
echo "## COMMAND: $@" >> $TMPLOG
- $@ >>$TMPLOG 2>&1
+ "$@" >>$TMPLOG 2>&1
else
- $@ > /dev/null 2>&1
+ "$@" > /dev/null 2>&1
fi
fi
}
@@ -723,7 +723,7 @@
log "Copying file: $@"
log " from $SRCDIR"
log " to $DSTDIR"
- mkdir -p "$DSTDIR" && (cd "$SRCDIR" && tar cf - $@) | (tar xf - -C "$DSTDIR")
+ mkdir -p "$DSTDIR" && (cd "$SRCDIR" && tar cf - "$@") | (tar xf - -C "$DSTDIR")
fail_panic "Cannot copy files to directory: $DSTDIR"
}
diff --git a/build/tools/builder-funcs.sh b/build/tools/builder-funcs.sh
index 173cd3d..e3b1dd2 100644
--- a/build/tools/builder-funcs.sh
+++ b/build/tools/builder-funcs.sh
@@ -26,7 +26,7 @@
if [ "$VERBOSE2" = "yes" ]; then
echo "$@"
fi
- $@
+ "$@"
else
echo "${_BUILD_TAB}${_BUILD_HIDE}$@" >> $_BUILD_MK
fi
@@ -148,27 +148,27 @@
builder_ldflags ()
{
- _builder_varadd _BUILD_LDFLAGS $@
+ _builder_varadd _BUILD_LDFLAGS "$@"
}
builder_ldflags_exe ()
{
- _builder_varadd _BUILD_LDFLAGS_EXE $@
+ _builder_varadd _BUILD_LDFLAGS_EXE "$@"
}
builder_cflags ()
{
- _builder_varadd _BUILD_CFLAGS $@
+ _builder_varadd _BUILD_CFLAGS "$@"
}
builder_cxxflags ()
{
- _builder_varadd _BUILD_CXXFLAGS $@
+ _builder_varadd _BUILD_CXXFLAGS "$@"
}
builder_c_includes ()
{
- _builder_varadd _BUILD_C_INCLUDES $@
+ _builder_varadd _BUILD_C_INCLUDES "$@"
}
builder_reset_cflags ()
@@ -216,7 +216,7 @@
if [ -z "$_BUILD_CXX" ]; then
_BUILD_CXX=${CXX:-g++}
fi
- for src in $@; do
+ for src in "$@"; do
srcfull=$_BUILD_SRCDIR/$src
if [ ! -f "$srcfull" ]; then
echo "ERROR: Missing source file: $srcfull"
diff --git a/build/tools/prebuilt-common.sh b/build/tools/prebuilt-common.sh
index 2be6d23..e7e99dd 100644
--- a/build/tools/prebuilt-common.sh
+++ b/build/tools/prebuilt-common.sh
@@ -41,28 +41,28 @@
# Usage: str=`dashes_to_underscores <values>`
dashes_to_underscores ()
{
- echo $@ | tr '-' '_'
+ echo "$@" | tr '-' '_'
}
# Translate underscores to dashes
# Usage: str=`underscores_to_dashes <values>`
underscores_to_dashes ()
{
- echo $@ | tr '_' '-'
+ echo "$@" | tr '_' '-'
}
# Translate commas to spaces
# Usage: str=`commas_to_spaces <list>`
commas_to_spaces ()
{
- echo $@ | tr ',' ' '
+ echo "$@" | tr ',' ' '
}
# Translate spaces to commas
# Usage: list=`spaces_to_commas <string>`
spaces_to_commas ()
{
- echo $@ | tr ' ' ','
+ echo "$@" | tr ' ' ','
}
# Remove trailing path of a path
@@ -111,7 +111,7 @@
sort_uniq ()
{
local RET
- RET=$(echo $@ | tr ' ' '\n' | sort -u)
+ RET=$(echo "$@" | tr ' ' '\n' | sort -u)
echo $RET
}
@@ -908,7 +908,7 @@
{
local NDK="$1"
shift
- echo "$NDK/$(get_toolchain_install_subdir $@)"
+ echo "$NDK/$(get_toolchain_install_subdir "$@")"
}
# $1: toolchain name
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