in reply to in place editing by variables
I agree with hdb that it's safer (and easier) to use modules. Also consider using usermod --shell <new_shell> system command, on Linux, at least.
One way to replace the shell manually:
use strict; use warnings; my $new_shell = "/bin/nologin"; # in-place editing with backup $^I = ".bak"; # diamond operator "<>" reads from command line arguments or STDIN while ( my $line = <> ) { # skip empty lines next if $line =~ /^\s*$/; # split on ":" and return the 7th field (list slice) my $old_shell = ( split ":", $line )[6]; $line =~ s/$old_shell/$new_shell/; print "$line\n"; }
Update: using explicit variable $line instead of default variable $_ for better readability.
Well done is better than well said. -- Benjamin Franklin
|
|---|