|
Brace expansion problem in Bash
I have a script that takes an option for server pools to run the script against. The option is given as a comma separated list (ie, -p 201,204,301).
I'm using eval and brace expansion to get those pool numbers into an array. It works fine unless only 1 pool number is given. Here's the code:
Code:
while [ "$1" != "" ]; do
case $1 in
-p ) shift
pools=($(for i in $(eval echo {$1}); do echo $i; done | sort))
;;
If multiple pools given:
Code:
[me@server bin]$ ./project_nunenhoffen.sh -p 201,202
+ '[' -p '!=' '' ']'
+ case $1 in
+ shift
+ pools=($(for i in $(eval echo {$1}); do echo $i; done | sort))
++ sort
+++ eval echo '{201,202}'
++++ echo 201 202
++ for i in '$(eval echo {$1})'
++ echo 201
++ for i in '$(eval echo {$1})'
++ echo 202
+ shift
+ '[' '' '!=' '' ']'
If single pool given:
Code:
[me@server bin]$ ./project_nunenhoffen.sh -p 201
+ '[' -p '!=' '' ']'
+ case $1 in
+ shift
+ pools=($(for i in $(eval echo {$1}); do echo $i; done | sort))
++ sort
+++ eval echo '{201}'
++++ echo '{201}'
++ for i in '$(eval echo {$1})'
++ echo '{201}'
+ shift
+ '[' '' '!=' '' ']'
So if a single pool is given it leaves the braces around it which is bad. I could do a test of $1 to see if there are any commas and process it different if there aren't but I was wondering if there's a better solution than that. Also wondering if how I handled getting the pool numbers into an array was the best way to do it (eval & brace expansion).
Thanks,
Mike G.
|