将用户和组添加到系统 - Shell Scripting

时间:2017-05-22 17:56:57

标签: shell sh

我正在尝试编写一个简短的脚本,将用户添加到具有组的系统,并为用户提供选项,通过在逗号后分割字符串来删除用户名列表。

 #!/bin/bash

    while [ x"$username" = x ]; do

    read -p "Please enter the username you wish to create : " username

    if id -u "$username" >/dev/null 2>&1; then

    echo "User already exists"

    username=""

    fi

    done

    while [ x"$group" = x ]; do

    read -p "Please enter the primary group. If group not exist, it will be created : " group

    if id -g "$group" >/dev/null 2>&1; then

    echo "Group exist"

    else

    groupadd "$group"

    fi

    done

    read -p "Please enter bash [/bin/bash] : " bash

    if [ x"$bash" = x ]; then

    bash="/bin/bash"

    fi

    read -p "Please enter homedir [/home/$username] : " homedir

    if [ x"$homedir" = x ]; then

    homedir="/home/$username"

    fi

    read -p "Please confirm [y/n]" confirm

    if [ "$confirm" = y ]; then

    useradd -g "$group" -s $bash -d $homedir -m $username

    fi

    while [ "$retry" = y ]; do

    retry="n"

    read -p "Do you want to delete users. Enter their username separated by a comma:" users

    if [ x"$users" = x ]; then

    read -p "No user name inserted. Try again? [y/n]" retry

    fi

    done

    IFS=', ' read -r -a array <<< "$users"

    for user in "${array[@]}"
    do
    userdel -r "$user"
    done 

问题是,当我尝试删除用户时,我收到了一个意外的脚本结束错误(没有结束脚本运行就好了)。 当我运行时,插入的新用户也不会出现在系统用户列表中:

for i in $(cat /etc/passwd  | cut -d: -f1); do
   echo -n $i ": "
   grep $i /etc/group | cut -d: -f1 | tr "\n" " "
   echo
done

在shell脚本方面,我是一个完整的问候者,请你解释一下我错在哪里? 谢谢大家的时间! 使用http://www.shellcheck.net/

检查代码

0 个答案:

没有答案
相关问题