Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changes to comments in /etc/passwd file 3

Status
Not open for further replies.

grapes12

Technical User
Mar 2, 2010
124
ZA
I need to create a shell script to make changes to the comment field in the /etc/passwd file.
I got the data from the oracle database, and need to make the necessary changes to the /etc/passwd file from a spool file created in oracle
can u please assist in pointing me into the right direction
 
You wanted something like this ?
Code:
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig
while IFS=":" read user comment
do [ -z $(awk -F: "/^$user:/"'{print $2}' $FILE2) ] && usermod -c "$comment" $user
done <$FILE1

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I am receiving the following errors:
running the above code from PHV(MIS)

UX: usermod: ERROR: mrut does not exist.
UX: usermod: ERROR: sharonw does not exist.
UX: usermod: ERROR: Invalid syntax.
usage: usermod -u uid [-o] | -g group | -G group[[,group]...] |
-d dir [-m] | -s shell | -c comment |
-l new_logname | -f inactive | -e expire |
-A authorization [, authorization ...] | -K key=value ... |
-P profile [, profile ...] | -R role [, role ...] login
UX: usermod: ERROR: tanya does not exist.

I am running on Solaris 5.10 OS
Any help would be highly appreciated
 
FYI, I've just corrected the ksh's syntax errors ...
 
I redone my select statement on oracle and included the :)) seperator, when i spooled to my txt file.
I included IFS=":" for this exact reason
herewith example of my output file:
tilla :TRD/KDP Tilla de Beer

uma :TRD/DBN Usha Mansen

dbu :TRD/GER Danny Rubin

mamelan :TRD/CTN Mamela Nyembe

mcas :VRN/PLZ Maryka Castelyn

sam :S/S/KZN Sam Modibedi

celesteh:MFC/DBN Celeste Hitzeroth
 
Did you try the previous solution I posted?

I'm not sure setting IFS=":" and changing they query output will make things a lot simpler. Now the usernames will have spaces on the end of them, so they will have to be either removed or ignored somehow before searching for them in the passwd file.

Also, the logic in the script you posted (and PHV corrected syntactically) is flawed. Either you want to say [ [red]![/red] -z or change the && to ||, because you want to run usermod when the resulting string is not zero length.

Annihilannic.
 
Now the script looks like this with your changes anna
$ more test2.ksh
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

while IFS=":" read user comment
do
[ -z $(awk -F: "/^$user:/"'{print $2}' $FILE2) ] || usermod -c "$comment" $user
done <$FILE1
[MAC] /u1/home/gdg

Have not tried running it. Did you see the errors above
the UX usermod errors
 
Please post your code between [ignore]
Code:
 ...
[/ignore] tags.

That script will not work because it wil search for usernames with spaces, e.g. "tilla :" in /etc/passwd, which it will not find. You can strip off the spaces as shown below.

Also, you should quote any arguments to [ -z "..." ] if they contain variables which may be empty, otherwise you may get errors like "ggoliath[27]: test: argument expected".

Code:
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

while IFS=":" read user comment
do
  # remove spaces from end of username
  user=$(echo $user | tr -d ' ')
  [ -z [red]"[/red]$(awk -F: "/^$user:/"'{print $2}' $FILE2)[red]"[/red] ] || usermod -c "$comment" $user
done <$FILE1

Annihilannic.
 
If i trace the script, to see if any errors occurs i am not getting any.
BUT my test passwd file still does not show any changes on the comment field like i would like it to do:
anna:x:3498:500::/u1/home/anna:/bin/ksh
simphiwe:x:3499:500::/u1/home/simphiwe:/bin/ksh
maureenv:x:3500:500::/u1/home/maureenv:/bin/ksh
nicky:x:3501:500::/u1/home/nicky:/bin/ksh

Is there something wrong or amiss?
 
Found the error!
its making the changes in my existing /etc/passwd file! WHY?
the code looks like this
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

for i in `awk -F":" '{ print $1 }' $FILE1`
do
FIELD1=`cat $FILE1 | grep ${i} |awk -F":" '{ print $2 }'`
grep $i ${FILE2}
if [ $? -eq 0 ]
then
usermod -c "${FIELD1}" $i
fi
done < ${FILE1}

CAN I NOT FIRST WRITE CHANGES TO A FILE BEFORE replacing the original /etc/passwd file?
 
Of course you can... by not using the usermod utility! That utility is designed for modifying your current users... how would it know that you wanted to do it in another file?

That is why we suggested other methods earlier on in this thread that do not use usermod, but instead create a /tmp/passwd.new file. But for some reason you wanted to use usermod?

Annihilannic.
 
the script is still causing havoc in my /etc/passwd file.
CODE:
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

while IFS=":" read user comment
do
user=$(echo $user | tr -d ' ')
[ -z "$(awk -F: "/^$user:/"'{print $2}' $FILE2)" ] || usermod -c "$comment" $use
r
done <$FILE1

here is few lines from my /etc/passwd file
simphiwe:x:3499:500:TRD/GER Simphiwe Goodenough
:/u1/home/simphiwe:/bin/ksh
maureenv:x:3500:500:MTP/MTP Maureen vander Merwe
:/u1/home/maureenv:/bin/ksh
nicky:x:3501:500:VRN/STL Nicky van de Walt
:/u1/home/nicky:/bin/ksh
SEE THE SPACES BETWEEN THE LINE HOW CAN I FIX THAT?
 
I'd replace this:
while IFS=":" read user comment
with this:
while IFS=":
" read user comment

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
herewith is my script which is doing exactly what i would like it to do:
code:CODE:
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

while IFS=":" read user comment
do
user=$(echo $user | tr -d ' ')
[ -z "$(awk -F: "/^$user:/"'{print $2}' $FILE2)" ] || usermod -c "$comment" $use
r
done <$FILE1
code end:

I need to put in a condition, to test if user exists and not to change the comment field.Only change users who do not have a comment field in /etc/passwd.
How do i do that?
 
The code tags are [ignore]
Code:
[/ignore][/b] and [b][ignore]
[/ignore]
.

Try this:

Code:
#!/bin/ksh
FILE1=/u1/home/gdg/passwd_comm.txt
FILE2=/u1/home/gdg/passwd.grapes
cp $FILE2 /u1/home/gdg/passwd.$$.orig

while IFS=":" read user comment
do
 user=$(echo $user | tr -d ' ')
 [ -z "$(awk -F: "/^$user:/"'[red] && $5 == ""[/red] {print $2}' $FILE2)" ] || usermod -c "$comment" $user
done <$FILE1

Or to avoid messy quoting I would prefer:

Code:
[ -z "$(awk -F: -vuser=$user '$1 == user && $5 == "" {print $2}' $FILE2)" ] || usermod -c "$comment" $user

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top