The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Reply
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
  #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.
  #2 (permalink)  
Old 07-02-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
#!/bin/ksh

list='201,204,301'

IFS=,; set -A array ${list}

typeset -i i=0
while (( $i < ${#array[*]} ))
do
  echo "${array[$i]}"
  ((i=i+1))
done;
  #3 (permalink)  
Old 07-02-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
Quote:
Originally Posted by vgersh99 View Post
Code:
#!/bin/ksh

list='201,204,301'

IFS=,; set -A array ${list}

typeset -i i=0
while (( $i < ${#array[*]} ))
do
  echo "${array[$i]}"
  ((i=i+1))
done;
I'm using Bash which doesn't have -A option for set and, from what I can tell, doesn't have an equivalent. I wish it did because you basically do what I need in a single line with your example.

For now I've solved my problem this way:

Code:
while [ "$1" != "" ]; do
  case $1 in
    -p  )               shift
                        case $1 in
                          *,* ) pools=( $(for i in $(eval echo {$1}); do echo $i; done | sort) ) ;;
                          *   ) pools=$1 ;;
                        esac
                        ;;
  #4 (permalink)  
Old 07-02-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Code:
#!/bin/bash

list='201,204,301'

IFS=,; array=(${list})

typeset -i i=0
while (( $i < ${#array[*]} ))
do
  echo "${array[$i]}"
  ((i=i+1))
done;
  #5 (permalink)  
Old 07-02-2009
mglenney mglenney is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 85
Perfect. I was so hung up on solving it using brace expansion.

Thank you!!
  #6 (permalink)  
Old 07-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by vgersh99 View Post
Code:
#!/bin/bash

list='201,204,301'

IFS=,; array=(${list})

typeset -i i=0
while (( $i < ${#array[*]} ))
do
  echo "${array[$i]}"
  ((i=i+1))
done;

All of the above is equivalent to:

Code:
list='201,204,301'
array=( ${list//,/ } )
printf "%s\n" "${array[@]}"
Reply

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 06:10 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