Skip to content

Commit de765c9

Browse files
committed
Print spark-class command properly
1 parent a4df3c4 commit de765c9

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

bin/spark-class

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ fi
157157
export CLASSPATH
158158

159159
if [ "$SPARK_PRINT_LAUNCH_COMMAND" == "1" ]; then
160+
# Put quotes around system properties in case they contain spaces
161+
# This exports the resulting list of java opts into QUOTED_JAVA_OPTS
162+
quote_java_property "${ESCAPED_JAVA_OPTS[@]}"
160163
echo -n "Spark Command: " 1>&2
161-
echo "$RUNNER" -cp "$CLASSPATH" "${ESCAPED_JAVA_OPTS[@]}" "$@" 1>&2
164+
echo "$RUNNER" -cp "$CLASSPATH" "${QUOTED_JAVA_OPTS[@]}" "$@" 1>&2
162165
echo -e "========================================\n" 1>&2
163166
fi
164167

bin/utils.sh

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
# limitations under the License.
1717
#
1818

19+
# * ---------------------------------------------------- *
20+
# | Utility functions for launching Spark applications |
21+
# * ---------------------------------------------------- *
22+
1923
# Parse the value of a config from a java properties file according to the specifications in
2024
# http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader).
2125
# This accepts the name of the config and returns the value through JAVA_PROPERTY_VALUE.
26+
# This currently does not support multi-line configs.
2227
parse_java_property() {
2328
JAVA_PROPERTY_VALUE=$( \
2429
sed "/^[#!]/ d" "conf/spark-defaults.conf" | \
@@ -30,22 +35,24 @@ parse_java_property() {
3035
export JAVA_PROPERTY_VALUE
3136
}
3237

33-
# Properly escape java options, dealing with whitespace, double quotes and backslashes
34-
# This accepts a string, and returns the escaped list through ESCAPED_JAVA_OPTS.
38+
# Properly escape java options, dealing with whitespace, double quotes and backslashes.
39+
# This accepts a string and returns the escaped list through ESCAPED_JAVA_OPTS.
3540
escape_java_options() {
3641
ESCAPED_JAVA_OPTS=() # return value
3742
option_buffer="" # buffer for collecting parts of an option
3843
opened_quotes=0 # whether we are expecting a closing double quotes
3944
for word in $1; do
4045
contains_quote=$(echo "$word" | grep \" | grep -v \\\\\")
41-
if [ -n "$contains_quote" ]; then
46+
if [[ -n "$contains_quote" ]]; then
4247
# Flip the bit
4348
opened_quotes=$(((opened_quotes + 1) % 2))
4449
fi
4550
if [[ $opened_quotes == 0 ]]; then
51+
# Remove all non-escaped quotes around the value
4652
ESCAPED_JAVA_OPTS+=("$(echo "$option_buffer $word" | sed "s/^[[:space:]]*//" | sed "s/\([^\\]\)\"/\1/g")")
4753
option_buffer=""
4854
else
55+
# We are expecting a closing double quote, so keep buffering
4956
option_buffer="$option_buffer $word"
5057
fi
5158
done
@@ -57,3 +64,18 @@ escape_java_options() {
5764
export ESCAPED_JAVA_OPTS
5865
}
5966

67+
# Put double quotes around each of the given java options that is a system property.
68+
# This accepts a list and returns the quoted list through QUOTED_JAVA_OPTS
69+
quote_java_property() {
70+
QUOTED_JAVA_OPTS=()
71+
for opt in "$@"; do
72+
is_system_property=$(echo "$opt" | grep -e "^-D")
73+
if [[ -n "$is_system_property" ]]; then
74+
QUOTED_JAVA_OPTS+=(\"$opt\")
75+
else
76+
QUOTED_JAVA_OPTS+=("$opt")
77+
fi
78+
done
79+
export QUOTED_JAVA_OPTS
80+
}
81+

0 commit comments

Comments
 (0)