Linux shell 脚本程序在第 29 行后退出?

Linux shell script program quitting after line 29?

我正在编写 shell 脚本来创建用户并将他们添加到组中。我在第 17 行遇到了一个障碍,当时我计算了一个可以在此处看到的错误 http://imgur.com/XhNGIeW 但是一半的代码似乎是完美的(在添加用户方面)但是任何人都可以帮助我谢谢。

#!/bin/bash
let repeat=1
let counter=0
while [ $repeat -eq 1 ];
        do
        echo "Please enter the username for the created user"
        read username
        sudo useradd -m $username
        echo ""
        sudo passwd $username
        let counter=$counter+1
        while [ $repeat -eq 1 ];
                do
                echo "please enter the name of the group to put the user into"
                read group
                if [ $(getent group $group) ]; then #line 17 
                        sudo usermod -G $group,sudo $username
                        let repeat=0
                else
                        echo "The group "$group$" does not exist on our system"
                        echo "Do you want to create it as a new group (y)"
                        read input
                        if [ $input == "y" ]; then
                                sudo groupadd $groups
                                sudo usermod -G $group,sudo $username
                                let repeat=0

                        fi

                fi

         done
         echo $username" has beem added to the group "$group
   done

   echo "You have now created "$counter" new user(s) and have added them to their new group(s)"

如果群组名称无效,getent group $group 将不会 return 输出所以第 17 行将等同于:

if [ != '' ]; then

!= 运算符比较运算符前后的字符串。如果其中之一 缺少所需的字符串,shell 报告错误:

[: !=: unary operator expected

如果你想检查一个有效的组,你可以简单地检查一些输出 return编辑:

if [ $(getent group $group) ]; then