#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Question bash script to add new users taking info from a .txt file


    Hello, everybody
    First of all, I'd like to say thank you to all of you for the forum (admins, users...), it's a really valuable resource for a lot a people, including me.
    I don't know if this is the right place where to ask this, my question is about my homework. If it's not, please, could an admin move it to its right place?... I'm a bit confused with an exercise, so if anybody can point me in the right direction (Or give me the answer) I'll be really gratefull:

    Given a text file called users.txt with this info inside:

    Peter:7777:/home/store ;

    Quinn:7777:/home/accounts ;

    David:7777:/home/secondstore ;

    Daniel:7777:/home/address ;


    The script should create an user using the first word of each line, the second word of each line will be used as password, and the third one as the home directory. The internal field separator is :

    I can only use awk or shellscripting for the exercise. The teacher pointed that one of the posible solutions includes the SYSTEM command used inside AWK...but there are other solutions using only shellscripting

    I've tried to do it using USERADD command, creating a loop with a "for" etc...but nothing at the moment

    Thanks for your help
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    867
    Rep Power
    391

    Talking


    Show us what you got.
    And copy/paste the result when you execute your script.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Posts
    2
    Rep Power
    0
    I'm trying this in a shell: sudo awk -F ':| ' '{ print("adduser --home", $3, $1"; echo", $2 " | passwd", $1) | "/bin/bash" }' users.txt

    the error I get is:
    adduser: Please enter a username matching the regular expression configured
    via the NAME_REGEX[_SYSTEM] configuration variable. Use the `--force-badname'
    option to relax this check or reconfigure NAME_REGEX.
    passwd: user «Jperez» does not exist
    adduser:....
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    I thought it was useradd ... but anyway ... -F':|' is setting a field delimiter of :| in awk, which is not what you want.

    Depending on how you want the commands to run, you may wish to look at something 'simpler', such as:

    Code:
    for user_dets in $(cat users.txt)
    do
      name=echo "$user_dets" | awk -F\: '{print $1}'
      pwd=echo "$user_dets" | awk -F\: '{print $2}'
      home=echo "$user_dets" | awk -F\: '{print $3}'
      sudo useradd -d "$home" -m -p "$pwd" $name
    done
    done
    I have never used the -p option before, so it may be used invalidly!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo