ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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;
#!/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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.