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 12-02-2004, 04:34 PM   #1
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Rep: Reputation: 0
simple perl and regex


Hi I am trying to code a simple script in perl
I want to call it changeshell.
What it will do, is scan through the /etc/passwd file, and search entry which contains a shell, and change it to an inputted shell.
How can I scan w/ regex and replace a certain field?
 
Old 12-02-2004, 05:25 PM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
Code:
while(<FILEHANDLE>) {
        $line =~ s!/bin/bash\n$!/bin/false\n!;
        push(@output, $line);
}

foreach (@output) {
        print(OUT);
}
That will require fleshing out obviously.
 
Old 12-02-2004, 10:51 PM   #3
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
You can use the command line script,

perl -pi -e s?/bin/bash\$?WATEVERINPUT? /etc/passwd

The \$ will ensure only the entries at the end of the line will be replaced
This will directly overwrite the /etc/passwd.
 
Old 12-02-2004, 11:06 PM   #4
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Original Poster
Rep: Reputation: 0
well i dont want to overwrite it completely just swap the entry of a said user
 
Old 12-02-2004, 11:26 PM   #5
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
Quote:
Originally posted by hk_linux
This will directly overwrite the /etc/passwd.
Yes ... yes it will ... make sure to get it right the first time

You'll also want the correct shell syntax, which is: perl -pi -e 's#/bin/sh$#/new/path#' /etc/passwd <-- Single quotes around the -e block avoid having to escape for the shell. If you're uncomfortable editing the passwd file in place, you can use perl -pi.old -e 's#/bin/sh$#/new/path#' /etc/passwd, which will put the original in /etc/passwd.old.

I posted the long way in case you wanted to do more with the data than sed can do ...

(Note that the '?'s from the last poster and the '#'s in this are completely interchangeable, but '?' is a regex reserved character and makes the search string hideous to read).

If it's jjust one user, why not just vipw and be done with it?
 
Old 12-03-2004, 01:59 PM   #6
phlx
LQ Newbie
 
Registered: Nov 2003
Posts: 10

Original Poster
Rep: Reputation: 0
well here is what I whipped up, but its not working..

#!/usr/local/bin/perl -w
use strict;
open FILEHANDLE, 'passwd' or die $!;

while (<FILEHANDLE>) {
my $line =~ s{/bin/bash\n}{/bin/csh\n};
push(@output, $line);
}

close FILEHANDLE;



also, I'm not sure, but is there another way I could do this directily to STDout instead of loading it all into @output to print? (that is what print does correct ?)



as of right now my one liner perl proggy is

#!/usr/local/bin/perl -w

system perl => -pi => -e => 's#/bin/bash$#/bin/csh#g' => passwd;


Last edited by phlx; 12-03-2004 at 02:23 PM.
 
Old 12-03-2004, 03:01 PM   #7
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
Code:
#!/usr/local/bin/perl -w

use strict;
open FILEHANDLE, 'passwd' or die $!;

while (<FILEHANDLE>) {
        my $line =~ s#/bin/bash\n#/bin/csh\n#;
        print $line;
}

close FILEHANDLE;
That should fix that, and print straight to STDOUT.

If you're looking for a one liner, it would look like this:

Code:
(to STDOUT)
perl -pe 's#/bin/bash\n#/bin/sh\n#' /etc/passwd

(to /etc/passwd)
perl -i -pe 's#/bin/bash\n#/bin/sh\n#' /etc/passwd
 
  


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
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 08:25 PM
simple regex not so simple (perl) ludeKing Programming 5 03-02-2005 02:29 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
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