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

时间:2015-09-02 15:15:43

标签: linux shell raspberry-pi sh raspbian

我正在编写一个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)"

1 个答案:

答案 0 :(得分:0)

如果该组的名称无效,getent group $group将不会返回任何名称 输出所以第17行将相当于:

if [ != '' ]; then

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

[: !=: unary operator expected

如果要检查有效组,可以只检查一些输出 退回:

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