批量添加20个用户到class01组,用户名以std开头,以数字结尾,格式:std01---std20
方法1
#!/bin/sh
groupadd class01
a=std
for ((i=1;i<=20;i++))
do
if [ $i -lt 10 ];then
username="$a"0"$i"
else
username=$a$i
fi
useradd -G class01 -M $username
done
方法2:
#!/bin/bash
groupadd class01
for i in {1..20}
do
if [ $i -lt 10 ];then
useradd "std0$i" -g class01
else
useradd "std$i" -g class01
fi
done
方法3: 此方法最简单高效,善用seq,会有意想不到效果
for i in `seq -w 20`;do useradd -G class01 sdt$i;done
seq的参数:
-f, --format=FORMAT use printf style floating-point FORMAT (default: %g)
-s, --separator=STRING use STRING to separate numbers (default: /n)
-w, --equal-width equalize width by padding with leading zeroes
-f 选项 指定格式
seq -f"%3g" 1 10
% 后面指定数字的位数 默认是"%g",
"%3g"那么数字位数不足部分是空格
# seq -f"%03g" 1 11
001
002
003
004
005
006
007
008
009
010
011
% 前面指定字符串,sed -f"%03g" 1 11 这样的话数字位数不足部分是0
# seq -f "test%03g" 8 12
test008
test009
test010
test011
test012
-w 指定输出数字同宽 不能和-f一起用
# seq -w 1 10
输出是同宽的
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。