Skip to content

Instantly share code, notes, and snippets.

@pbrandiezs
Last active February 22, 2018 21:29
Show Gist options
  • Save pbrandiezs/0f8fc51f322df7a3fdf19d0b4f28d40f to your computer and use it in GitHub Desktop.
Save pbrandiezs/0f8fc51f322df7a3fdf19d0b4f28d40f to your computer and use it in GitHub Desktop.
BASH - parse a config file and create users - sample code.
#!/bin/bash
SFTP_USERS="./sftp_users.txt"
cat ${SFTP_USERS} | while IFS='' read -r LINE || [[ -n "$LINE" ]]
do
ID=`echo $LINE|cut -d: -f1`
PASSWORD=`echo $LINE|cut -d: -f2`
NEWUID=`echo $LINE|cut -d: -f3`
NEWGID=`echo $LINE|cut -d: -f4`
if ! id -u ${ID} > /dev/null 2>&1; then
echo Adding ${ID}
echo UID is ${NEWUID}
echo GID is ${NEWGID}
groupadd --gid " "${NEWGID} ${ID}
useradd --inactive -1 --comment "service account" --create-home --gid ${NEWGID} --uid ${NEWUID} ${ID}
echo Adding /incoming/${ID} directories and setting ${ID} password
echo ${ID}:${PASSWORD} | chpasswd
mkdir -p /incoming/${ID}/input
mkdir -p /incoming/${ID}/archived
mkdir -p /incoming/${ID}/output
mkdir -p /incoming/${ID}/error
mkdir -p /incoming/${ID}/processed
chown -R ${ID}:${ID} /incoming/${ID}/*
else
echo ${ID} exists, continuing
### Remove id's, uncomment for testing
# echo Removing ${ID}
# rm -fr /incoming/${ID}
# userdel --remove ${ID}
###
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment