I re-did the code to something less complicated and now it says:
ubuntu@ubuntu-VirtualBox:~$ ./newuseradd.sh Newbies.csv [sudo] password for ubuntu: useradd: group 'acct' does not exist useradd: group 'hr' does not exist useradd: group 'purch' does not exist
Script:
#!/bin/bash if [ $# -ne 1 ]; then echo "USAGE: $0 FILE" exit 7 fi sed -i'.bak' 's/Accounting/acct/;s/Purchasing/purch/;s/Human Resources/hr/;s/Information Technology/it/' $1 # Part 3 - transforming the group names while read line; do userinfo=($(echo $line | tr ',' ' ')) firstinitial=$(echo "${userinfo[1]}" | cut -c1) username=$(echo ${firstinitial}${userinfo[0]} | tr A-Z a-z) echo useradd $username -g ${userinfo[2]} -G employee -c ${userinfo[3]} -m #Part 2 echo "$(date): $username, ${userinfo[1]}, ${userinfo[0]}, $?" >> it-log.txt #Part 4 assigment- it log echo -n "${userinfo[0]}, ${userinfo[1]}" >> hr-log.txt grep "$username" /etc/passwd | cut -d':' -f1,7,3 | tr ':' ',' >> hr-log.txt
done < $1 #part 1 assigment 6 1 Answer
In this case, I think it's best to serialize your data in a stride list (of 4).
#!/bin/bash
declare -A passwd group
declare -la a=()
while IFS="," read -r b c d e; do a+=("$b" "$c" "$d" "$e"); a=("${a[@]// }")
done < newbies.csv
for i in passwd group; do while IFS=: read -r j _ ; do eval ${i}\[\$j\]=1; done < /etc/$i
done
lookup(){ local a a=${1}[$2]; [[ ${!a} = 1 ]]
}
for ((y = 0, x = 0; y < ${#a[@]}; y += 4)); do read -r \ lastname firstname department uid \ <<< "${a[@]:$y:4}" printf Processing:\ %s\\n \ "$lastname $firstname $department $uid" lookup group \ "$department" || echo groupadd "$department" lookup passwd \ "$firstname" || { \ echo useradd -n -c "Assigment4" -g "$department" "$firstname" && ((x++)); }
done
echo "Complete. $x accounts have been created." 0