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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-09-2005, 06:13 PM
|
#1
|
|
Member
Registered: Sep 2004
Location: http://longbeach.ca.u$
Distribution: Ubuntu,Gentoo, OSX
Posts: 136
Rep:
|
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
|
|
|
|
02-09-2005, 06:31 PM
|
#2
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,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
|
|
|
|
02-09-2005, 06:41 PM
|
#3
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,718
|
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.
|
|
|
|
02-09-2005, 06:41 PM
|
#4
|
|
Member
Registered: Sep 2004
Location: http://longbeach.ca.u$
Distribution: Ubuntu,Gentoo, OSX
Posts: 136
Original Poster
Rep:
|
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 08:10 PM.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:55 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|