1.批量创建10个用户stu01-stu10,并且设置随机8位密码,要求不能用shell循环(例如:for,while等),只能用命令及管道实现。
##方法1:
[root@server tmp]# echo stu{01..10}|tr " " "\n"|sed -r 's#(.*)#useradd \1 ; pass=$((RANDOM+10000000)); echo "$pass"|passwd --stdin \1; echo -e "\1 \t `echo "$pass"`">>/tmp/oldboy.log#g'|bash
useradd: user 'stu01' already exists
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
useradd: user 'stu02' already exists
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
useradd: user 'stu03' already exists
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
useradd: user 'stu04' already exists
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
useradd: user 'stu05' already exists
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
useradd: user 'stu06' already exists
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
useradd: user 'stu07' already exists
Changing password for user stu07.
passwd: all authentication tokens updated successfully.
useradd: user 'stu08' already exists
Changing password for user stu08.
passwd: all authentication tokens updated successfully.
useradd: user 'stu09' already exists
Changing password for user stu09.
passwd: all authentication tokens updated successfully.
useradd: user 'stu10' already exists
Changing password for user stu10.
passwd: all authentication tokens updated successfully.
上述命令实际就是再拼N条下面的命令的组合,举一条命令stu01用户的过程拆解如下:
useradd stu01 ;
pass=$((RANDOM+10000000));
echo "$pass"|passwd --stdin stu01;
echo -e "stu01 `echo "$pass"`">>/tmp/oldboy.log
特别说明:如果用shell循环结构会更简单,之所以限制使用循环的目的是锻炼学生的基础命令运用
能力,学到现在还没学到SHELL循环课程呢
##方法2:
[root@server tmp]# echo stu{11..12}|xargs -n1 useradd ;echo stu{11..12}:`cat /dev/urandom|tr -dc 0-9|fold -w8|head -1`|xargs -n1|tee -a pass.txt|chpasswd
##方法3:
[root@server tmp]# echo stu{21..30} | tr ' ' '\n' | sed -e 's/^/useradd /' -e 's/\(stu[0-9]\{2\}\)$/\1 \&\& echo "\1:`echo $[$RANDOM**3] | cut -c1-8`" | tee -a userInfo.txt | cut -d: -f2 | passwd --stdin \1/' | bash
Changing password for user stu21.
passwd: all authentication tokens updated successfully.
Changing password for user stu22.
passwd: all authentication tokens updated successfully.
Changing password for user stu23.
passwd: all authentication tokens updated successfully.
Changing password for user stu24.
passwd: all authentication tokens updated successfully.
Changing password for user stu25.
passwd: all authentication tokens updated successfully.
Changing password for user stu26.
passwd: all authentication tokens updated successfully.
Changing password for user stu27.
passwd: all authentication tokens updated successfully.
Changing password for user stu28.
passwd: all authentication tokens updated successfully.
Changing password for user stu29.
passwd: all authentication tokens updated successfully.
Changing password for user stu30.
passwd: all authentication tokens updated successfully.
功能: 创建10个用户 分别是 stu21-stu30 其密码是用随机数变量RANDOM生成,均保存至 userInfo.txt中,格式: username:passwd 这个写的不算好 如果有更好的一定要分享哦! 上面的随机数 我之前是用日期生成的,是不对的,因为有可能会有重复现象,所以我后来干脆用RANDOM**3取其前8位,可确保唯一性
##方法4:
[root@server tmp]# echo stu{01..10} |tr ' ' '\n'|sed -rn 's@^(.*)$@useradd \1 ; echo $RANDOM|md5sum|cut -c 1-8 >/data/\1;cat /data/\1|passwd --stdin \1@gp'|bash
useradd: user 'stu01' already exists
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
useradd: user 'stu02' already exists
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
useradd: user 'stu03' already exists
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
useradd: user 'stu04' already exists
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
useradd: user 'stu05' already exists
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
useradd: user 'stu06' already exists
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
useradd: user 'stu07' already exists
Changing password for user stu07.
passwd: all authentication tokens updated successfully.
useradd: user 'stu08' already exists
Changing password for user stu08.
passwd: all authentication tokens updated successfully.
useradd: user 'stu09' already exists
Changing password for user stu09.
passwd: all authentication tokens updated successfully.
useradd: user 'stu10' already exists
Changing password for user stu10.
passwd: all authentication tokens updated successfully.