LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2009, 01:59 PM   #16
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47

Moin,

Quote:
Originally Posted by NsearchOf View Post
How would you get the same result using an array?
an array makes sense for a variable list of parameters. The code below assigns the user list values to the array variable "usr_arr" using the already shown mechanism and then iterates over the array's members:

Code:
while IFS=: read group password gid user_list; do
  usr_arr=(${user_list//,/ })
  for ((i=0; i<${#usr_arr[*]}; i++)); do
    echo group $group ${usr_arr[$i]}
  done
done < /etc/group
The important part is the second line: The array is defined this way:
Code:
usr_arr=(user1 user2 user3)
because the parameter expansion within the braces ${user_list//,/ } replaces all "," by spaces - so every user name becomes a separate item of the array. If you would assign $user_list - means "user1,user2,user3", you would get only one item.

Jan
 
Old 06-01-2009, 10:00 AM   #17
NsearchOf
LQ Newbie
 
Registered: May 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Smile Thanks everyone!

Thanks everyone for your help! I have learned a lot in just a few days!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with shell script - pulling two fields from a file Dcrusoe Programming 6 05-15-2009 02:58 PM
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 02:13 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
help with shell script - fill an array with values from a file delmoras Linux - General 1 07-17-2006 11:19 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration