Results 1 to 5 of 5

Thread: /etc/passwd script question

  1. #1
    Join Date
    Sep 2006
    Location
    Florida
    Beans
    187
    Distro
    Ubuntu

    Question /etc/passwd script question

    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

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

    Code:
    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?
    Never let a computer know you’re in a hurry.

  2. #2
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: /etc/passwd script question

    Maybe something like this:

    Code:
    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

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

  4. #4
    Join Date
    Nov 2006
    Location
    Belgium
    Beans
    3,010
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: /etc/passwd script question

    you can 'escape' the * wildcard with \, like so

    Code:
    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 !

  5. #5
    Join Date
    Sep 2006
    Location
    Florida
    Beans
    187
    Distro
    Ubuntu

    Re: /etc/passwd script question

    Thank you everyone, for all the help.

    Using your advice I was able to get my script working.
    Never let a computer know you’re in a hurry.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •