如何让这个脚本循环X次?

时间:2017-03-02 17:50:46

标签: loops scripting

我试图将groupadd coffee下的所有内容循环5次,以便一次创建5个新用户。取这些变量并填写其他命令。完成所有操作后,再次为新用户启动。我上下搜索,似乎无法搞清楚。这是脚本:

@media only screen and (max-width: 400px) {
    #additional {
        margin-top: 200px;
    }
}

我知道我可能需要对此进行重组,但我希望我可以添加一些简单的东西来修复它。

1 个答案:

答案 0 :(得分:0)

这是我得到帮助的解决方案。

<pre>
#!/bin/bash

sudo groupadd Coffee
# adds group Coffee to system

COUNTER=0
         while [  $COUNTER -lt 5 ]; do
             echo The counter is $COUNTER
echo
    read -p "What is the new user's login ID? " username
    read -s -p "What is the user's password? " password 
echo
    read -p "What is the new user's full name? " fullname
    read -p "What is the new user's initial group? " intgroup
    read -p "What is the new user's additional group? " addgroup
#prompts for variables

if [ $(getent group $intgroup) ]; then
    echo
else
    sudo groupadd $intgroup
fi
#checks to see if specified group is on the system


if [ $(getent group $addgroup) ]; then
    echo
else
    sudo groupadd $addgroup
fi
#checks to see if specified group is on the system

    sudo useradd -m -G Coffee,$intgroup,$addgroup $username
#creates home directory and adds user into groups


if [ -z $password ] 
then
    echo -e "newpass\nnewpass" | sudo passwd $username
else 
    echo -e "$password\n$password" | sudo passwd $username
fi
#changes password or gives default one
 let COUNTER=COUNTER+1 
         done
echo
<code>