Shibli
Shibli

Reputation: 6149

How pass grouped parameters in command line

I would like to pass three groups of parameters such as:

sh run.sh [1,4,6,8] [3,6,10,14] [8,16,32]

If possible then how can I access to parameters in script?

Upvotes: 0

Views: 214

Answers (1)

anubhava
anubhava

Reputation: 786081

You can pass your comma separated numbers as arguments to your script:

bash run.sh "1,4,6,8" "3,6,10,14" "8,16,32"

Please note I am using bash instead of sh to be able to use bash shell instead of posix sh.

Then inside the run.sh script, you can use IFS=, read -ra to split each argument into shell array:

IFS=, read -ra argarr1 <<< "$1"
IFS=, read -ra argarr2 <<< "$2"
IFS=, read -ra argarr3 <<< "$3"

And use each array as:

for i in "${argarr1[@]}"; do
    echo "$i"
done

Upvotes: 4

Related Questions