Score:0

groupadd command not working error: 'is not a valid group name'

il flag

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

Assigment with the details

Soren A avatar
mx flag
I think that `groupadd` only takes one groupname argument at the time.
AnneBright avatar
il flag
Thank you, is there an easier way for me to do this then? I have been watching a lot of videos and doing some reading online, and this is how my script might seem a little frankstein-ish...
bac0n avatar
cn flag
@Steeldriver unquoted `${a[*]}` will not expand to comma-separated (neighter will `${a[@]}` in any form), but `"${a[*]}"` will, so it will loop: `IFS=,; a=(a b c); echo ${a[*]} "${a[*]}"`
hr flag
@AnneBright OK apparently I'm wrong - please ignore my previous comment
bac0n avatar
cn flag
@AnneBrigh: @Steeldriver is still right though because you changed IFS to `,`, `${groups[*]}` won't split as expected (`" \n\t"`), think you can comment `IFS` as it's not used I believe. (secgroup= is assigned as a variable but addressed as an array)
bac0n avatar
cn flag
there are a lot of other problems, you should probably echo the different "$variables" to be sure the parsing works.
Score:1
cn flag

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."
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.