1

My CSV file contains the information needed like this firstname,lastname,userid. I have 5 users which i need to parse from the CSV. In order to create the account assign to a group(group1) and assign encrypted temporary passwords. I've hit a wall, any help would be awesome. Thanks!

#!/bin/bash

OLDIFS=$IFS
IFS=","

while read firstname lastname userid 
 do 
    useradd -c "$firstname $lastname -d /home/$userid -G group1 -s /bin/bash $userid"
done
IFS=$OLDIFS

No users are being added from script execution.

9
  • 1
    Whats wrong with what you have got ? Commented Nov 4, 2014 at 13:56
  • @1up You have an un-closed quote. But there's no way of knowing whether that's the cause since you haven't properly described the problem. Commented Nov 4, 2014 at 13:57
  • Getting an Error on line 8 when I execute in BASH; ./Parse.sh: line 8: unexpected EOF while looking for matching `"' ./Parse.sh: line 12: syntax error: unexpected end of file Commented Nov 4, 2014 at 13:57
  • 2
    @1up As I said: Un-closed quote. Using an editor with syntax highlight helps. And/or shellcheck.net Commented Nov 4, 2014 at 13:58
  • 1
    @1up It looks like you send all your data as a comment to useradd. I think you meant to put quotes around each parameter. Commented Nov 4, 2014 at 14:10

1 Answer 1

1

This should help you.

#!/bin/bash

OLDIFS=$IFS
IFS=","

while read firstname lastname userid 
do 
     useradd -c "${firstname} ${lastname}" -d /home/"${userid}" -G group1 -s /bin/bash "${userid}"
done < file.csv

Your script was fine, i have made few changes as below

  1. Have quoted the variables, this will prevent unwanted expansions.
  2. Resetting IFS is not necessary as the script runs in a subshell.
  3. while needs a file input, so added it.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I had also saved my file to the wrong location, hence the no change.
Regarding #4: The double quotes are enough. This way the single quotes will be sent to useradd (which may not be a problem).

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.