2

I'm having trouble parsing through a CSV file to add users.

I wrote a small script to add a list of 1000 users:

export IFS=","

cat users.csv | while read a b c d e f g; do useradd -d -u $a -U $d ; done

The CSV file is formatted as:

ps2946,male,Phil Harris,Recording,6508 Hollywood Blvd,Nov 16 1950,760 901 3537
mc3225,male,Mel Blanc,Radio,6385 Hollywood Blvd.,Oct 1 1983,626 647 5560
fn1029,male,Franklin Pangborn,Motion pictures,1500 Vine Street,Mar 10 1911,858 497 6108
we9602,undefined,Webb Pierce,Recording,1600 Vine Street,Sep 3 1988,559 595 6063

However upon running the script it iterates through the entire file repeating:

useradd: invalid home directory '-u'

I used export IFS to separate the words by comma and use them as the parameters for the useradd command.

I read the manual and used the value for the home directory, but I feel like my entire script is off.

Can someone please help me in properly formatting a script to add users from a CSV file?

For reference I used this thread as a base: https://stackoverflow.com/questions/4439536/shell-script-to-parse-through-a-file-csv-and-process-line-by-line

1

The error is because you're calling useradd with the -d option , which expects the next word to be the home directory path or directory name in the default home directory (not -u).

As a first suggestion, try:

cat users.csv | while read a b c d e f g; do useradd -d "$a" -U "$d" ; done
  • Thank you. Is there a way to add in the other parameters to the stored user? Or would it be better to store the info (phone number, address, birthday) into a comment? – SaintLaurent May 26 '17 at 19:56
  • A comment sounds a good idea, either that or finger information using chfn – einonm May 26 '17 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.