2

I'm trying to add users to the system from a file. The file looks exactly like this:

[email protected] password 1
[email protected] baddpassword 2

The numbers are their IDs. Its coming from a mysql db and I need to do something with those variables too so I need 4 variables. The USER, PASS, FIRSTID (being 1) and LASTID (being the last ID of the mysql dump in this case, 2. This is considering more than 2 entries at a time)

So far what I have is this:

for USER in $(awk -F@ 'NR>1 {print $1}' /root/users_w_pass)
do
    for PASS in $(awk 'NR>1 {print $2}' /root/users_w_pass)
    do
        useradd $USER -d /home/$USER -m
        echo "$USER:$PASS" | chpasswd
        ...


    done
done

When running this, the password does not get added to the corresponding user and I have no idea on how I am supposed to do that since I thought it would work but I can see how it doesn't. It seems to loop the password. Also I am creating files and assigning ACLs for each, so running newusers on a file won't work here, especially that there is a third column as well.

1 Answer 1

4

You don't want to read lines with for. The proper way to do it is to use while with read. The read builtin can automatically parse fields per line if they're separated by whitespace (spaces and tabs). The -r option makes sure that backslashes aren't interpreted while reading:

while read -r user password id; do
    # strip the @domain part from username
    user="${user%%@*}"
    useradd "$user" -d "/home/$user" -m
    echo "$user:$password" | chpasswd
    # and so on
done < your_input_file

Also note that you shouldn't use all-uppercase variables. $USER is already reserved for your username.

4
  • I appreciate the update answer. I should have been more clear on the ID part also. What I need is that the first line, in this case being id number 1 should be set to $firstid and the last entry (in this case there are only two entries here) so we use 2 for this example, should be set to $lastid. The reason is that at the end of the script, I need to run a mysql command to "UPDATE table set status='1' WHERE id BETWEEN $firstid AND $lastid". This script runs every hour. So every hour, only the users with status=0 are dumped to a file similar to the output we started with.
    – unixpipe
    Apr 26, 2014 at 7:32
  • Wouldn't it be easier to just run set status='1' where id=$id for every added user? Of course it has a little bit of an impact performance-wise. Otherwise I'd use head -n 1 and tail -n 1 to find the first and last ID after the loop has run.
    – slhck
    Apr 26, 2014 at 7:35
  • I'm an idiot. Yes, that would be easier. For some reason I dont know why I thought I'd be setting potentially new users to status=1. Will test this momentarily and return with a nice big green checkmark for your awesome help.
    – unixpipe
    Apr 26, 2014 at 7:39
  • Worked like an absolute charm. Thank you very much for the help!
    – unixpipe
    Apr 26, 2014 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .