groupadd 命令不起作用错误:“不是有效的组名”

groupadd 命令不起作用错误:“不是有效的组名”

我重新编写了代码,使其不那么复杂,现在它显示:

ubuntu@ubuntu-VirtualBox:~$ ./newuseradd.sh Newbies.csv [sudo] ubuntu 的密码: useradd:组“acct”不存在 useradd:组“hr”不存在 useradd:组“purch”不存在

脚本:

 #!/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

任务详述

答案1

在这种情况下,我认为最好将数据序列化为步幅列表(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."

相关内容