0

I'm fairly new to Linux/CentOS and shell scripting. Let's say I have a file named useraccounts.list. This file contains CSV data in this specific order:

first name, middle initial, last name, username, password

for example:

John,N,Snow,seords,cuai2Ohzdh

What I'm trying to do is traverse through this list creating user accounts from the provided data, and with the password at the end of each line. I would like to store it using the passwd command for each individual user.

Here is what I have so far:

#!/bin/sh
for i in 'more useraccounts.list'
do
        echo "$1 $2 $3 $4 $5 $6"
done <file.txt

Can someone please help me out?

1 Answer 1

2

Before you get to how you will store the password, you need to sort out how you will read the values from the file correctly. In bash, the primary tool for determining how the shell will break a line into individual tokens is the Internal Field Separator (default: space tab newline). The process is called word-splitting The IFS variable allows you to set the characters that will control word-splitting.

You can use that to your advantage in reading lines such as yours by including a comma in IFS. This will allow you to specify individual variable to read each name, initial, last, user name and password into. A while loop is the normal way to accomplish this -- and it allows you to set the IFS for that block of code without effecting the remainder of the script.

An Example, in your case, would be:

#!/bin/bash

[ -z "$1" ] && {    ## validate one argument given on command line
    printf "error: insufficient input. usage: %s filename.\n" "${0##*/}"
    exit 1
}

[ -r "$1" ] || {    ## validate it is a readable filename
    printf "error: file not found/readable '%s'.\n" "$1"
    exit 1
}

## read each line in file separated by ','
#  set Internal Field Separator to break on ',' and '\n'
#  protect against lack of '\n' on last line with $pw test
while IFS=$',\n' read -r first mi last uname pw || [ -n "$pw" ]; do

    printf "name: %-5s %s. %-6s user: %s  pass: %s\n" \
        "$first" "$mi" "$last" "$uname" "$pw"

    ## Create User Accounts/Store Password Here...

done <"$1"

exit 0

Input

$ cat dat/useracct.txt
John,N,Snow,seords,cuai2Ohzdh
Jill,O,Rain,reords,cuai3Ohzdh
Jane,P,Sleet,peords,cuai4Ohzdh

Output

$ bash readuserfile.sh dat/useracct.txt
name: John  N. Snow   user: seords  pass: cuai2Ohzdh
name: Jill  O. Rain   user: reords  pass: cuai3Ohzdh
name: Jane  P. Sleet  user: peords  pass: cuai4Ohzdh

You can then create the user accounts with the desired options and store the password any way you like. Let me know if you have any questions.

Sign up to request clarification or add additional context in comments.

5 Comments

Should the || [ -n "$pw" ] be using &&?
No, when handling files with non-POSIX line ends, read will read the line into the individual variables, but will not register the read being complete due to the lack of terminating newline. In this case, you check that the last variable has been filled in lieu of the newline for confirmation of a good read. It works because the list variables are unset in between each read so if the last variables isn't filled, there was no complete read irrespective of the terminating newline. So it is an either/or test after read here.
Thank you David for the input! I have a question now regarding adding the password, i implemented the command to add the users "useradd -c "$first $mi $last" $uname" but now i want to implement the passwd, i know when using the command i have to enter a password, then verify it. Is there an alternative to this? Could i just use the command passwd ans supply the password from the useracct.txt? thanks, hope this makes sense.
im just trying to use the password for each individual user, and assign it using the passwd command. Thanks
Sorry, I was in a chat on another question. The general rule is you never store or pass a password in a variable, etc.. for security reasons. However, useradd provides the -p option that will take a password as returned by crypt(3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.