编写一个shell脚本,添加100个新用户

如题所述

i=1
for (1..100)
do 
    groupadd users
    adduser user$i -g users
    echo user | passwd "user$1" --stdin
    i=$( $i + 1)
done

添加100用户在users组,并设置密码为user

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-24
#!/bin/bash
# Date and Time:2017-08-17-00:12

read -p "input a group name:" n

if `awk -F : '{print $1}' /etc/group | grep $n &>/dev/null` ; then
echo $?
echo "$n exist! use another groupname!"
exit 1
else
echo $?
groupadd $n&&echo "groupadd complete"
fi

i=0
for v in {01..100}
do
useradd us$v -g $n 2>/dev/null
if [[ $? -eq 0 ]] ;then
i++
fi
done

if [[ $i -eq 100 ]] ;then
echo "useradd complete"
else
echo "useradd failed"
fi
第2个回答  2017-06-22
for ((i=0;i<100;i++));do useradd test$i ; done
试试行不行
第3个回答  2017-07-18
for i in {1..100}; do aa=`printf "useradd test%03d\n" $i`;$aa; done;
会按照如下命令创建用户

useradd test001
useradd test002
useradd test003
useradd test004
useradd test005
useradd test006
useradd test007
useradd test008
useradd test009
useradd test010
第4个回答  推荐于2018-02-27
#!/bin/bash
for name in $( seq 1 100 )
    do
        useradd "user$name"
            if [ $? -eq 0 ];then
                echo -e "\n创建 "user$name" 成功!"
            fi
       done

本回答被网友采纳
相似回答