0

Looking for a way to split following argument passed to a shell script in a single line. Input

my_script.sh "memory":"8"

I am trying to find a way to allocate maximum memory to run java program. If value is 8, I will start java with 8GB of memory(-Xmx8G) and if memory is not passed, java will be run without Xmx option.

Note: Earlier I tried thisbut looks like ':' need a special treatment. Also I need to figure out within a line.

#!/bin/bash
echo "First arg: $1"

IFS=':'
read -ra arr <<< $1
for val in "${arr[@]}";
do
  printf "name = $val\n"
done

printf "name = ${arr[1]}\n"

Result

% ./myscript.sh "memory":"8"
First arg: memory:8
name = memory 8
name =
Tectrendz
  • 1,354
  • 2
  • 17
  • 36
  • Why not use standard option syntax, so you can parse it with `getopts`? – Barmar Sep 07 '21 at 22:38
  • Note that `my_script.sh "memory":"8"` is **exactly** the same as `my_script.sh memory:8` or `my_script.sh "memory:8"` -- the script can't tell which one you passed, because all three uses all create the exact same argument vector. – Charles Duffy Sep 07 '21 at 23:01

0 Answers0