PDA

View Full Version : [SOLVED] /etc/passwd script question



Loki57701
November 7th, 2012, 10:29 PM
Hello all, I need some scripting help. I need to modify the /etc/passwd file using a script.

I want to change the 2nd column of a user field from x to *.

As an example:

From


test:x:123:123:some comment:/bin/bash:/home/test

to


test:*:123:123:some comment:/bin/bash:/home/test

I've tried various combinations of sed and awk, but the /'s in the path names, and the wildcard '*' seem to mess things up.

Does anyone have an idea of how I can go about this?

drmrgd
November 7th, 2012, 10:41 PM
Maybe something like this:


awk -F: '{ sub(/x/,"*",$2); print }' /etc/passwd

Not sure if that's what you were looking to do. You can redirect the output to a file too, if you like. Just add '> somefile.txt' to the end

sisco311
November 7th, 2012, 10:44 PM
Check out: BashFAQ 001 and http://mywiki.wooledge.org/Quotes

koenn
November 8th, 2012, 11:01 AM
you can 'escape' the * wildcard with \, like so


sed s/:x:/:\*:/ /path/file
(you need to add the -i option or redirect the output to make changes permanent)

make sure you know what you're doing wrt /etc/shadow vs. /etc/passwd, and the effects of '*' in the password field !

Loki57701
November 8th, 2012, 05:20 PM
Thank you everyone, for all the help.

Using your advice I was able to get my script working.