The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash - delay expansion of variable scandora Shell Programming and Scripting 3 02-18-2009 05:59 AM
globbing, $# is too high after wildcard expansion in bash script zoo591 Shell Programming and Scripting 3 07-25-2008 03:50 AM
Pattern expansion problem njoshi Shell Programming and Scripting 5 04-24-2007 01:28 PM
AIX Expansion CD almuwatta AIX 0 06-23-2005 06:07 PM
ksh on HP-UX -- variable expansion dangral UNIX for Dummies Questions & Answers 4 02-08-2005 12:35 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 07-02-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0