0

I would like to have optional variables in my script, because I don't always need to use them.

For example in my script I have:

hiveParams=""
if [ "$1" == hiveParams=* ]; ### if the first param starts with hiveParams=
then
echo "$hiveParams"
spark submit --conf spark.sql.hive.conf.list=\$hiveParams
fi

And when I run the script I would like that the script takes "TestHiveParams" and replace it in $hiveParams

sh test.sh hiveParams="TestHiveParams"

How can I make this run?

Update: I have added:

 set -o --

and run the job:

sh test.sh --hiveParams="someParams"

and I get that the numbers of parameters is 0.

How should I define it , so that it recognizes it?

gigives
  • 65
  • 1
  • 1
  • 6
  • *optimal -> optional. I fixed it for you :) – wjandrea Jun 22 '20 at 13:56
  • The standard format that's most similar to what you want is `--option=value`, so `--hiveParams="TestHiveParams"`. There are also commands that take arguments in the same format you described, `option=value`, like `dd`, but I'd recommend going with the standard. – wjandrea Jun 22 '20 at 13:58
  • `env hiveParams="TestHiveParams" sh test.sh` then `... ${hiveParams:-"DefaultHiveParams"} ...` or `if [ -n "$hiveParams" ]; ...` – jhnc Jun 22 '20 at 14:04
  • Can anyone help me with the syntax?. Sorry for the simple questions.. but somehow can not get it correct. (I have updated the question) – gigives Jun 22 '20 at 14:59
  • at the top of your script add `set -xv` to see each command expanded; alternatively, add `echo "1.${1}."` (the `1.` / `.` are added as visual delimiters) to see what `${1}` actually looks like; I'm guessing once you see what `${1}` looks like you'll be able to edit your test accordingly – markp-fuso Jun 22 '20 at 15:42

0 Answers0