本文共 1059 字,大约阅读时间需要 3 分钟。
批量添加20个用户到class01组,用户名以std开头,以数字结尾,格式:std01---std20
方法1 1 2 3 4 5 6 7 8 9 10 11 12 | #!/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:
1 2 3 4 5 6 7 8 9 10 | #!/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 输出是同宽的本文转自 boy461205160 51CTO博客,原文链接:http://blog.51cto.com/461205160/1920294
转载地址:http://zvujl.baihongyu.com/