Hi all, How would I write a script that only affected certain groups? Trying to think of an example, how could I run: rm /home/*/file_to_delete only on the home directories of folk who belong to the 'pupil' group. One command I run is find /home -name '*' -maxdepth 1 -exec cp /home/template/file_to_copy '{}'/new_file ';' Can I build a "where user is in group 'pupils'" into that somehow? Apologies if I'm way off on all this. Ignorance isn't bliss. Any ideas or resources to read up on? Ta -- Matt __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/
On Thu, 2002-01-17 at 11:16, Matt Johnson wrote:
Hi all,
How would I write a script that only affected certain groups?
Trying to think of an example, how could I run:
rm /home/*/file_to_delete
only on the home directories of folk who belong to the 'pupil' group.
No point recursing down these trees - getting a list of home directories for that group would be something like awk -F: '($4 == 25) {print $6}' /etc/passwd change the 25 to the numeric group id of your wanted group.
One command I run is find /home -name '*' -maxdepth 1 -exec cp /home/template/file_to_copy '{}'/new_file ';'
Can I build a "where user is in group 'pupils'" into that somehow?
you could add a -group pupils to the condition (ie straight after -maxdepth 1). Nigel.
Hi all,
How would I write a script that only affected certain groups?
Trying to think of an example, how could I run:
rm /home/*/file_to_delete
only on the home directories of folk who belong to the 'pupil' group.
One command I run is find /home -name '*' -maxdepth 1 -exec cp /home/template/file_to_copy '{}'/new_file ';'
Can I build a "where user is in group 'pupils'" into that somehow?
You could try something like "find /home -group pupil"... -- Mark Evans St. Peter's CofE High School Phone: +44 1392 204764 X109 Fax: +44 1392 204763
I'm trying to install the above NIC into my samba box. My kernel is 2.4.4-GB. The module, downloaded from 3com's website, is built for a 2.2 kernel. Although inexperienced, I have tried to build the source code, but am getting build errors I cannot decipher with my limited C experience. I suspect they are library compatability errors. Has anybody ever used this card, which I now suspect is aimed at MS markets, not unix platforms, or know where I can obtain a pre-compiled module? Many thanks Darren Garside
--- Mark Evans
Hi all,
How would I write a script that only affected certain groups?
Trying to think of an example, how could I run:
rm /home/*/file_to_delete
only on the home directories of folk who belong to the 'pupil' group.
One command I run is find /home -name '*' -maxdepth 1 -exec cp /home/template/file_to_copy '{}'/new_file ';'
Can I build a "where user is in group 'pupils'" into that somehow?
You could try something like "find /home -group pupil"...
Hmm. I've got a bit stuck. I should have said the words 'RedHat'. It uses User Groups as primary groups - so all these home directories do not belong to the 'pupil' group. Any other ideas? I'm playing with cat, grep, sed and find to try to extract the names next to the pupil group in /etc/groups - but I'm messing up the pipes and the '>' etc. I am also trying to work out how to strip off useless info from a line from /etc/group, and then run all words between commas individually into find. I'm still experimenting. Please let me know any pointers, or even better - give examples. I think the scripting (potential - for me) of Linux is _such_ a big selling point. I can see what I want to do with it, and it's great when you figure out a little script to go off and do it for you. It is _so_ powerful, and makes admin tasks a breeze (once you've figured out the syntax). Thanks for responses so far. -- Matt __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/
--- Matt Johnson
--- Mark Evans
wrote: Hi all,
How would I write a script that only affected certain groups?
Trying to think of an example, how could I run:
rm /home/*/file_to_delete
only on the home directories of folk who belong to the 'pupil' group.
One command I run is find /home -name '*' -maxdepth 1 -exec cp /home/template/file_to_copy '{}'/new_file ';'
Can I build a "where user is in group 'pupils'" into that somehow?
Umm, I'm interested in solving this for you. My idea is to use a combination of "Sed", "Awk" (?) and the "Stat" command....leave it with me, I'll see what I can do. HTH, Thomas Adam ===== Thomas Adam "The Linux Weekend Mechanic" -- www.linuxgazette.com __________________________________________________ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com
Nice one! Thanks! -- Matt
Umm, I'm interested in solving this for you. My idea is to use a combination of "Sed", "Awk" (?) and the "Stat" command....leave it with me, I'll see what I can do.
HTH,
Thomas Adam
===== Thomas Adam
"The Linux Weekend Mechanic" -- www.linuxgazette.com
__________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/
On Fri, 18 Jan 2002, Matt Johnson wrote: > > You could try something like > > "find /home -group pupil"... > Hmm. I've got a bit stuck. I should have said the > words 'RedHat'. It uses User Groups as primary groups > - so all these home directories do not belong to the > 'pupil' group. Any other ideas? I'm playing with cat, > grep, sed and find to try to extract the names next to > the pupil group in /etc/groups - but I'm messing up > the pipes and the '>' etc. I am also trying to work > out how to strip off useless info from a line from > /etc/group, and then run all words between commas > individually into find. I'm still experimenting. > Please let me know any pointers, or even better - give > examples. The one-liner: perl -ne 'print join(" ", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group will give you a space-separated list of names. Change the first parameter to join (the " ") to something else if you want another separator. For example, perl -ne 'print join("\n", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group would give you one name per line. > I think the scripting (potential - for me) of Linux is > _such_ a big selling point. I can see what I want to > do with it, and it's great when you figure out a > little script to go off and do it for you. It is _so_ > powerful, and makes admin tasks a breeze (once you've > figured out the syntax). Learn about Perl and regular expressions! They're not quite as horrible and line-noisy as they appear at first. :-) Michael
--- Michael Brown
perl -ne 'print join("\n", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group
I don't know whether to laugh or cry! A big thanks. I'll try it out (and I'll check out a few Perl books from Amazon I think too!). It only makes me want more... -- Matt __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/
On Friday 18 January 2002 2:15 pm, Matt Johnson wrote:
--- Michael Brown
wrote: perl -ne 'print join("\n", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group
I don't know whether to laugh or cry!
A big thanks. I'll try it out (and I'll check out a few Perl books from Amazon I think too!). It only makes me want more...
The main one to try is Learning Perl by O'Reilly ISBN 0596001320. I've never read it personally, but it's the one that everyone keeps quoting on the beginners@perl.org list. -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
On Fri, Jan 18, 2002 at 02:51:39PM +0000, Gary Stainburn wrote:
On Friday 18 January 2002 2:15 pm, Matt Johnson wrote:
--- Michael Brown
wrote: perl -ne 'print join("\n", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group
I don't know whether to laugh or cry!
A big thanks. I'll try it out (and I'll check out a few Perl books from Amazon I think too!). It only makes me want more...
The main one to try is Learning Perl by O'Reilly ISBN 0596001320.
I've never read it personally, but it's the one that everyone keeps quoting on the beginners@perl.org list.
I've got it and would recommend it. I think O'Reilly do a boxed set with that, the Camel book & the cookbook. The above is good for learning the language, the camel book is an excellent reference and goes into a good bit of detail, and the cookbook is good if you want to get an idea of how to write a script for something. eg. I wanted to write a small ftp client to grab a file in the early hours via cron so I just grabbed and adapted the one out of the cookbook. I'd also recommend `Learning the Bash shell' pub. O'Reilly. Even though you can do most of what you can with the shell with perl, I still like writing shell scripts as they're dead easy and you'll get to understand your boot scripts etc. Well worth investing some money in good books IMO if you want to get the best out of linux. -- Frank *-*-*-*-*-*-*-*-*-*-* Boroughbridge. Tel: 01423 323019 --------- PGP keyID: 0xC0B341A3 *-*-*-*-*-*-*-*-*-*-* http://www.esperance-linux.co.uk/ When you speak to others for their own good it's advice; when they speak to you for your own good it's interference.
On Fri, 18 Jan 2002, Matt Johnson wrote:
perl -ne 'print join("\n", split(",",$_) ) if s/^pupil:.*:(.*)$/$1/' /etc/group I don't know whether to laugh or cry! A big thanks. I'll try it out (and I'll check out a few Perl books from Amazon I think too!). It only makes me want more...
FWIW, here's a breakdown of the one-liner: "perl" : pretty obvious "-ne" : the "e" means that the next argument is literal Perl code (i.e. we are putting the Perl on the command line instead of inside a script file), the "n" means "assume a basic program structure that will read in lines one by one from a specified file and execute my Perl code once for each line. "print x if y" : this gets executed for each line in the specified file (which is /etc/group). If y evaluates to true then x will be printed. "s/^pupil:.*:(.*)$/$1/" : the fun bit - a regular expression! s/a/b/ means to do a search and replace (replace a with b) ^ is "beginning of line" pupil:.*: is "pupil:(any number of any character):" (.*) is "any number of any character" $ is "end of line" $1 is a backreference. The part of the line that we want to extract is everything from the final colon up to the end of the line. This gets matched by the bit inside the brackets - ":(.*)$". The $1 refers back to the first bracketed part of the search expression, which is the bit we want to extract. Overall, the effect of this is to search for lines beginning with "pupil:", then to replace the line with everything after the final colon (i.e. the comma-separated list of users). The expression will evaluate to true if it manages to do a search and replace, which will happen only for the line that actually begins "pupil:". split(",",$_) : will take the comma-separated list of users and split it into an array using the comma as a separator. $_ is a special variable in Perl; basically it means "the thing I'm dealing with at the moment". Note that I didn't specify a variable for the search & replace operation; you can often omit a variable name and $_ will be used implicitly. join(" ", ... ) : takes the array created by split() and joins it together into a single line using " " as a separator. /etc/group : the file(s) that we want to use for input. Perl is a very nice language because it's almost never necessary to reinvent the wheel and you can fit an amazing amount of power into a single line. Consider this, for example: ( $user, $pass, $uid, $gid, $fullname, $homedir, $shell ) = split (":"); If $_ contains a line from /etc/passwd then this single line will extract all the relevant fields into variables for us. Combined with the "-ne" option, this means we could do something like: perl -ne '( $user, $pass, $uid, $gid, $fullname, $homedir, $shell ) = split (":"); print "User $user lives in $homedir\n";' /etc/passwd Just a taster of the wonderful world of Perl... Michael
participants (8)
-
'Frank Shute'
-
D Garside
-
Gary Stainburn
-
Mark Evans
-
Matt Johnson
-
Michael Brown
-
Nigel Metheringham
-
Thomas Adam