EP89
EP89

Reputation: 5

How can I pass a string with spaces as a function argument in bash

I am trying to pass a string with spaces as ./triggerSparkApp 'Premier League' via a bash script.

I implemented it in this way via bash script;

if [ $@ -eq "${0}" ]
  then
    echo "No arguments" 
  else
       spark-submit --master yarn-client --jars hive-hcatalog-core-0.13.0.jar spark-queries-1.0-SNAPSHOT-jar-with-dependencies.jar "${0}" --driver-memory 4g --executor-memory 2g --executor-cores     
 fi

The argument should be passed through in spark-submit before --driver-memory. As it is it is returning:

[: too many arguments

I would appreciate any help. thanks

Upvotes: 0

Views: 460

Answers (2)

P.P
P.P

Reputation: 121427

You need to use quote around the $@ in order avoid word splitting. And use == for string comparison; -eq for integers.

if [ "$@" == "${1}" ]
  then
    echo "No arguments"
  else
       spark-submit ...
 fi

Alternatively you can use [[ .. ]] in which word splitting is not performed.

Upvotes: -1

Fred
Fred

Reputation: 7015

I think this is what you want :

if [ "$#" -eq 0 ]
then
  echo "No arguments"
else
  spark-submit --master yarn-client --jars \
    hive-hcatalog-core-0.13.0.jar \
    spark-queries-1.0-SNAPSHOT-jar-with-dependencies.jar \
    "$1" --driver-memory 4g --executor-memory 2g --executor-cores     
fi

The $# expands to the number of arguments. The first argument is $1, not $0, so I replaced that in the spark-submit command. The \ continuations are for readability only.

Another way I like to perform the argument presence test is this :

if (($#))
then
  spark-submit --master yarn-client --jars \
    hive-hcatalog-core-0.13.0.jar \
    spark-queries-1.0-SNAPSHOT-jar-with-dependencies.jar \
    "$1" --driver-memory 4g --executor-memory 2g --executor-cores     
else
  echo "No arguments"
fi

The (( )) test, when it contains an expression that evaluates to an integer, returns 0 (true) if the integer is non-zero, and a non-zero value (false) otherwise.

Upvotes: 3

Related Questions