Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

Passing flags as stdin into a bash script

I am familiar with passing stdin into a shell script the traditional way:

shell>./dothis.bash filename
Variable $1 will be passed to the script.

What I would like to do is pass multiple values per variable I pass in, using flags or another method that someone might recommend:

shell>./dothis.bash -d database1 database2 database3 -p 3306

Where -d will pass database1,database2, and database3 to the script as an array or something. The number of arguments might be different each time.

Anyone know how to accomplish something like this?
0
jmicorp
Asked:
jmicorp
1 Solution
 
ozoCommented:
#!/bin/bash
for value  in $* ; do
if [ "${value:0:1}" = "-" ] ; then
  arrayname=${value:1}
else
 eval $arrayname='(${'$arrayname'[*]} $value)'
 eval echo '$'{$arrayname[*]}
fi
done
0
 
woolmilkporcCommented:
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now