Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux shell脚本程序在第29行之后退出?_Linux_Shell_Raspberry Pi_Sh_Raspbian - Fatal编程技术网

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

Linux shell脚本程序在第29行之后退出?,linux,shell,raspberry-pi,sh,raspbian,Linux,Shell,Raspberry Pi,Sh,Raspbian,我正在编写一个shell脚本来创建用户并将其添加到组中。我在第17行遇到了一个障碍,在这里我可以看到一个错误 但一半的代码似乎是完美的(在添加用户方面),但谁能帮我这个谢谢 #!/bin/bash let repeat=1 let counter=0 while [ $repeat -eq 1 ]; do echo "Please enter the username for the created user" read username

我正在编写一个shell脚本来创建用户并将其添加到组中。我在第17行遇到了一个障碍,在这里我可以看到一个错误 但一半的代码似乎是完美的(在添加用户方面),但谁能帮我这个谢谢

#!/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
将不会返回任何值 因此,第17行的输出相当于:

if [ != '' ]; then
=运算符比较运算符前后的字符串。如果其中一个
缺少所需字符串,外壳程序报告错误:

[: !=: unary operator expected
如果要检查有效组,只需检查某些输出 返回:

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

如果组名无效,
getent group$group
将不会返回任何值 因此,第17行的输出相当于:

if [ != '' ]; then
=运算符比较运算符前后的字符串。如果其中一个
缺少所需字符串,外壳程序报告错误:

[: !=: unary operator expected
如果要检查有效组,只需检查某些输出 返回:

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

哪一个是第17行?为什么有
''
?只有
'
应该足以识别空字符串?哪一个是第17行?为什么有
'
?只有
应该足以识别空字符串?当它到达“是否要将其创建为新组”时,它会无缘无故地关闭终端???@blawrence第22行中的
[
命令的参数太多:
如果[[$input==“y”]
。如果[$input==y],您只需要
如果[$input==y]
。一旦进入“是否要将其创建为一个新组”,它就会无缘无故地关闭终端???@blawrence第22行中的
[
命令的参数太多:
如果[[$input==“y”]
。如果[$input==y],您只需要
就可以了。