LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-09-2005, 07:13 PM   #1
Ateo
Member
 
Registered: Sep 2004
Location: http://longbeach.ca.u$
Distribution: Ubuntu,Gentoo, OSX
Posts: 136

Rep: Reputation: 15
bash assistance requested..


Hi. I'm trying to write my own script that will back up user directories. I wanted this script to backup all user directories based on the user group. So far, I have a simple script that'll backup whatever directory/directories I want. What I can't seem to get right is extracting the line containing users from /etc/group, ie:
Code:
root@epitaph portage # cat /etc/group | grep users
users:x:100:user1,user2,user3
I know what I need, I just don't how to parse it. I need the users (user1,user2,user3) to be extracted and commas replaced with spaces so that I can use that output as a space delimited variable for a bash for loop, ie:
Code:
userlist="user1 user2 user3"
for user in $userlist
Thanks
 
Old 02-09-2005, 07:31 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Re: bash assistance requested..

Quote:
Originally posted by Ateo
Code:
root@epitaph portage # cat /etc/group | grep users
users:x:100:user1,user2,user3
Code:
userlist="user1 user2 user3"
for user in $userlist
Try
Code:
root@epitaph portage # userlist=`grep users /etc/group  |cut -d":" -f 4|tr "," " "`


Cheers,
Tink
 
Old 02-09-2005, 07:41 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

There are many ways of doing this, here are 2.

A mix of awk and sed. Probably one of the simpler ones:

grep audio /etc/group | awk -F: '{ print $NF }' | sed 's/,/ /'

In the above example awk sets its field seperator to a : and prints the last field, which are the users with the comma still present. The sed part takes care of the comma.

And one which uses only sed, should be faster:

grep users /etc/group | sed -e 's/.*:\(.*\)/\1/' -e 's/,/ /'

2 sed parts. The first part (between the 2 -e's ) seperates the users from the rest by using the greediness of a regular expression ( .*: is everything up to and including the last : ) The part between \( and \) is what is needed and can be printed with the \1
The second part replaces the comma with a space.

Up to you to fill a variable with it.

Hope this helps.
 
Old 02-09-2005, 07:41 PM   #4
Ateo
Member
 
Registered: Sep 2004
Location: http://longbeach.ca.u$
Distribution: Ubuntu,Gentoo, OSX
Posts: 136

Original Poster
Rep: Reputation: 15
Excellent. It worked, with modifications....

Code:
usergrp="100"
userlist=`grep $usergrp /etc/group | cut -d":" -f 4|tr "," " "`
echo $userlist
Thanks a bunch!

I ended up needing to use the user group number as using "users" would also grep smbusers...

Last edited by Ateo; 02-09-2005 at 09:10 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
another cd didn't requested tripledotz Linux - Newbie 2 10-07-2005 11:24 AM
Perl assistance requested... Ateo Programming 2 05-26-2005 04:01 PM
Bash assistance Ateo Programming 5 03-09-2005 12:58 AM
Assistance Requested: Hardware selection for linux servers the_halfling Linux - Hardware 2 05-05-2004 08:05 AM
NIC help requested PureFire Linux - Networking 6 08-03-2003 06:02 PM


All times are GMT -5. The time now is 06:38 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration