#!/bin/bash#!/bin/bash### Description: Adds users based on provided CSV file ### CSV file must use : as separator### uid:username:comment:group:addgroups:/home/dir:/usr/shell:passwdage:password### Written by: Benjamin Cane - ben@example.com on 03-2012NEW_UID=$(echo $x |cut-d:-f1)NEW_USER=$(echo $x |cut-d:-f2)NEW_COMMENT=$(echo $x |cut-d:-f3)NEW_GROUP=$(echo $x |cut-d:-f4)NEW_ADDGROUP=$(echo $x |cut-d:-f5)NEW_HOMEDIR=$(echo $x |cut-d:-f6)NEW_SHELL=$(echo $x |cut-d:-f7)NEW_CHAGE=$(echo $x |cut-d:-f8)NEW_PASS=$(echo $x |cut-d:-f9)PASSCHK=$(grep-c ":$NEW_UID:"/etc/passwd)if[ $PASSCHK -ge 1]thenecho"UID: $NEW_UID seems to exist check /etc/passwd"elseuseradd-u $NEW_UID -c "$NEW_COMMENT"-md $NEW_HOMEDIR -s $NEW_SHELL -g $NEW_GROUP -G $NEW_ADDGROUP $NEW_USERif[!-z $NEW_PASS ]thenecho $NEW_PASS |passwd--stdin $NEW_USERchage -M $NEW_CHAGE $NEW_USERchage -d 0 $NEW_USER fifiNEW_UID=$(echo $x |cut-d:-f1)NEW_USER=$(echo $x |cut-d:-f2)NEW_COMMENT=$(echo $x |cut-d:-f3)NEW_GROUP=$(echo $x |cut-d:-f4)NEW_ADDGROUP=$(echo $x |cut-d:-f5)NEW_HOMEDIR=$(echo $x |cut-d:-f6)NEW_SHELL=$(echo $x |cut-d:-f7)NEW_CHAGE=$(echo $x |cut-d:-f8)NEW_PASS=$(echo $x |cut-d:-f9)PASSCHK=$(grep-c ":$NEW_UID:"/etc/passwd)if[ $PASSCHK -ge 1]thenecho"UID: $NEW_UID seems to exist check /etc/passwd"elseuseradd-u $NEW_UID -c "$NEW_COMMENT"-md $NEW_HOMEDIR -s $NEW_SHELL -g $NEW_GROUP -G $NEW_ADDGROUP $NEW_USERif[!-z $NEW_PASS ]thenecho $NEW_PASS |passwd--stdin $NEW_USERchage -M $NEW_CHAGE $NEW_USERchage -d 0 $NEW_USER fifiNEW_UID=$(echo $x |cut-d:-f1)NEW_USER=$(echo $x |cut-d:-f2)NEW_COMMENT=$(echo $x |cut-d:-f3)NEW_GROUP=$(echo $x |cut-d:-f4)NEW_ADDGROUP=$(echo $x |cut-d:-f5)NEW_HOMEDIR=$(echo $x |cut-d:-f6)NEW_SHELL=$(echo $x |cut-d:-f7)NEW_CHAGE=$(echo $x |cut-d:-f8)NEW_PASS=$(echo $x |cut-d:-f9)PASSCHK=$(grep-c ":$NEW_UID:"/etc/passwd)if[ $PASSCHK -ge 1]thenecho"UID: $NEW_UID seems to exist check /etc/passwd"elseuseradd-u $NEW_UID -c "$NEW_COMMENT"-md $NEW_HOMEDIR -s $NEW_SHELL -g $NEW_GROUP -G $NEW_ADDGROUP $NEW_USERif[!-z $NEW_PASS ]thenecho $NEW_PASS |passwd--stdin $NEW_USERchage -M $NEW_CHAGE $NEW_USERchage -d 0 $NEW_USER fifi### Parse $x (the csv data) and put the individual fields into variablesNEW_UID=$(echo $x |cut-d:-f1)NEW_USER=$(echo $x |cut-d:-f2)NEW_COMMENT=$(echo $x |cut-d:-f3)NEW_GROUP=$(echo $x |cut-d:-f4)NEW_ADDGROUP=$(echo $x |cut-d:-f5)NEW_HOMEDIR=$(echo $x |cut-d:-f6)NEW_SHELL=$(echo $x |cut-d:-f7)NEW_CHAGE=$(echo $x |cut-d:-f8)NEW_PASS=$(echo $x |cut-d:-f9)### Check if the new userid already exists in /etc/passwdPASSCHK=$(grep-c ":$NEW_UID:"/etc/passwd)if[ $PASSCHK -ge 1]then### If it does, skipecho"UID: $NEW_UID seems to exist check /etc/passwd"else### If not add the useruseradd-u $NEW_UID -c "$NEW_COMMENT"-md $NEW_HOMEDIR -s $NEW_SHELL -g $NEW_GROUP -G $NEW_ADDGROUP $NEW_USER### Check if new_pass is empty or notif[!-z $NEW_PASS ]then### If not empty set the password and pass expiryecho $NEW_PASS |passwd--stdin $NEW_USERchage -M $NEW_CHAGE $NEW_USERchage -d 0 $NEW_USER fififor x in`cat $1`doNEW_UID=$(echo $x |cut-d:-f1)NEW_USER=$(echo $x |cut-d:-f2)INPUT_FILE=$1for CSV_LINE in`cat $INPUT_FILE`doNEW_UID=$(echo $CSV_LINE |cut-d:-f1)NEW_USER=$(echo $CSV_LINE |cut-d:-f2)DATE=`date +%F`DATE=$(date+%F)if[-d $FILE_PATH ]thenfor FILE in $(ls $FILE_PATH/*)doecho "This is a file: $FILE"doneelseexit 1fiif[-d $FILE_PATH ]thenfor FILE in $(ls $FILE_PATH/*)doecho "This is a file: $FILE"doneelseecho "exiting... provided file path does not exist or is not a directory"exit 1fi