-2

I am having an issue while passing multiple string parameter which have spaces. I know there are answers but my scenario is different and not much aware of shell scripts too.

My shell script is having values as (its very simple script nothing else is there)

java -jar $PATH_TO_JAVA_MAIN $1 $2 $3 $4 $5

now $1 is only mandatory and others are optional. inputs to shell script is like Input from command line:

name:john address1:451 7th Street S.W address2:6430 Stream Side Court

now in java main method i get these in separate array index like

arg[1]=address1:451
arg[2]=7th
arg[3]=Street
arg[4]=address2:6430
arg[5]=Stream
arg[6]=Side 
arg[7]=Court

but i want that address to as one string against address1 and address2.

i tried both way as inputs

  1. name:john address1:"451 7th Street S.W" address2:"6430 Stream Side Court" result in String arg[] in my java class :

    LOGGER.info("Input values args: "+Arrays.toString(args));

    Input values args:[name:john, address1:451, 7th, Street, address2:6430, Stream, Side, Court]

Expected output :[name:john, address1:"451 7th Street", address2:"6430 Stream Side Court"] so that i can split with : and get the respective key values provided.

ami
  • 35
  • 7
  • Dukeling this is not duplicate i tried already what is suggested in various answers but i could not achieve what is required. – ami Sep 03 '17 at 17:46
  • As there is no response from anyone so i just have to go with an bad way. i just did this, name:john address1:"451~7th~Street ~S.W" address2:"6430 ~Stream~Side~Court" . nothing else i can do .. thanks or your replies and marking this question as duplicate :) – ami Sep 04 '17 at 08:38

1 Answers1

0

Use double quotes around the arguments to treat each of them as a single argument:

java -jar "$PATH_TO_JAVA_MAIN" "$1" "$2" "$3" "$4" "$5"

The same with invoking that shell script:

./script.sh "Address one two three" "other" "possible" "arguments"
Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72
  • Unix also allows escaping through `\ `: `Address\ one\ two\ three` – Izruo Sep 03 '17 at 16:13
  • providing \ is also not working. if i provide \ the out put is Input values args:[name:john, address1:\451, \7th, \Street, address2:6430, \Stream, \Side, \Court] – ami Sep 03 '17 at 17:43
  • @ami You need to put the backslash before the space. – Izruo Sep 05 '17 at 05:53