cp890 has asked for the wisdom of the Perl Monks concerning the following question:
My intent is to change the shell with inplace editing on a user or group of users by using variables thru substitution on different platforms- Linux, HPUX, Solaris,but I cannot get pass the script failure
I am focus on user1 in the script below but would also like to see a script example if I wanted to in place edit user1 and user3.Input --temp_pass
user1:x:9760822:3241:USER 1:/home/user1:/bin/bash
user2:x:9760822:3242:USER 2:/home/user2:/bin/ksh
user3:x:9760822:3243:USER 3:/home/user3:/bin/sh
Output to achieve is:
user1:x:9760822:3241:USER 1:/home/user1:/bin/nologin
user2:x:9760822:3242:USER 2:/home/user2:/bin/ksh
user3:x:9760822:3243:USER 3:/home/user3:/bin/nologin
Script failure
perl -w test_replace.pl
Use of uninitialized value in substitution (s///) at test_replace.pl line 26.
Use of uninitialized value in print at test_replace.pl line 27.
#!/usr/bin/perl open (LOGFILE, '>> test_replace.txt'); $IN_PASS = 'temp_pass'; # File to be edited print LOGFILE "File being edited---$IN_PASS\n"; # local $^I = '.bak'; # Call for in-place editing; make backups with a +.bak suffix $ID = "user1"; # (@PW_LIST) = ('user1' , 'user3'); #print LOGFILE "@PW_LIST\n"; # #foreach ($ID) @PW_LIST { #Loop to change shell if file +s exists $HOME_DIR = `cat ${IN_PASS} | grep $ID| cut -d: -f6`; $L_ID = `cat ${IN_PASS} | grep $ID`; $L_USER = `cat ${IN_PASS} | grep $ID | cut -d: -f1-6`; print LOGFILE " $L_ID\n"; print LOGFILE " $L_USER\n"; print LOGFILE "Current home directoy for $ID:$HOME_DIR\n"; $GO_TEST1 = "test_file1"; $GO_TEST2 = "test_file2"; # if (( "($HOME_DIR)/($GO_TEST1)") && ("($HOME_DIR)/($GO_TEST2)")) { +# verifying both files exists $CURR_SHELL = `cat ${IN_PASS} | grep $ID| cut -d: -f7`; print LOGFILE "Current Shell for $ID: $CURR_SHELL\n"; $NEW_SHELL = "/bin/nologin"; print LOGFILE "Replacing shell for \"$ID\" with $NEW_SHELL.\n"; s?$L_ID?$L_USER:$NEW_SHELL?g; print; } else { print LOGFILE " FAILED: ($HOME_DIR)/($GO_TEST1) and/or ($HOME_DIR)/( +$GO_TEST2) is missing for ($ID).\n"; } print LOGFILE "Finished\n"; close LOGFILE;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: in place editing by variables
by hdb (Monsignor) on Apr 11, 2013 at 18:19 UTC | |
|
Re: in place editing by variables
by roboticus (Chancellor) on Apr 11, 2013 at 17:53 UTC | |
|
Re: in place editing by variables
by reisinge (Hermit) on Apr 11, 2013 at 18:54 UTC | |
|
Re: in place editing by variables
by 2teez (Vicar) on Apr 11, 2013 at 19:13 UTC | |
by cp890 (Initiate) on Apr 11, 2013 at 22:06 UTC |