Skip to content

Instantly share code, notes, and snippets.

@JamesTheBard
Last active August 29, 2015 13:57
Show Gist options
  • Save JamesTheBard/9377618 to your computer and use it in GitHub Desktop.
Save JamesTheBard/9377618 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Variable assignment for legibility. The file content should be
# in the form of:
# username, password
filename=$1
# If the file exists, loop through it and start making accounts
# else just quit and display the help.
if [ -e $filename ]
then
while read line; do
# Parse the string and store in the userinfo array.
IFS="," userinfo=($line)
# Strip leading and trailing whitespace from each string
# Also, too lazy to shorten this up.
username=$(echo ${userinfo[0]} | sed -e 's/^ *//g;s/ *$//g')
password=$(echo ${userinfo[1]} | sed -e 's/^ *//g;s/ *$//g')
# Check to see if the user already exists, if it doesn't then
# create it.
if id -u $username >/dev/null 2>&1
then
echo "User '$username' already, exists!"
else
echo -n "Creating user '$username'..."
useradd -m $username
echo "done."
fi
# Change the password for the user
echo "$username:$password" | chpasswd
done < $filename
else
echo "Usage: $0 <filename>"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment