while statement and batch create users!

1, the role of the while loop statements: repeat the test a condition, as long as the condition is true then repeated
2, while sentence structure
while testing operating condition
do
command sequence is
done

============================================================

Create a user

[root@ns2 ~]# vim w.sh

! # / bin / bash 

the Read -p " Enter to create a user name prefix: " QZ 
the Read -p " Please enter the number of user created: " NUM 
the Read -p " Please enter the user's initial password: " PS 
the Read -p " [optional settings] enter the user time to failure: " the EXT 

I = . 1 
the while [$ I - Le $ NUM]
 do 
 IF - [! ; the EXT the then Z $] 
the useradd - E, QZ $ $ I $ the EXT
 the else 
  the useradd $ $, QZ i 
  fi 
  echo " $ PS " | passwd --stdin $QZ$i &> /dev/null
chage -d 0 $QZ$i
let i++
done

[root @ ns2 ~] # bash w.sh
Enter to create a user name prefix: cao
Please enter the number of user created: 5
Please enter the user's initial password: 123123
[Optional Settings] Enter a user expiration time: 2019-09 -02

cao1:x:1011:1011::/home/cao1:/bin/bash
cao2:x:1012:1012::/home/cao2:/bin/bash
cao3:x:1013:1013::/home/cao3:/bin/bash
cao4:x:1014:1014::/home/cao4:/bin/bash
cao5:x:1015:1015::/home/cao5:/bin/bash

======================================================

delete users

[root@ns2 ~]# vim s.sh
# ! / Bin / bash 

the Read -p " Enter the user prefix to be deleted: " QZ 
the Read -p " Please enter the number you want to delete a user: " NUM 

i = 1 
the while [$ i -le $ NUM]; do 
userdel - r $ i $ QZ 
the let i ++ 
DONE

[root@ns2 ~]# tail -10 /etc/passwd
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
maliu:x:1004:1004::/home/maliu:/bin/bash
sunqi:x:1005:1005::/home/sunqi:/bin/bash
cao1:x:1011:1011::/home/cao1:/bin/bash
cao2:x:1012:1012::/home/cao2:/bin/bash
cao3:x:1013:1013::/home/cao3:/bin/bash
cao4:x:1014:1014::/home/cao4:/bin/bash
cao5:x:1015:1015::/home/cao5:/bin/bash

===============================================================

Multiplication table

[root@ns2 ~]# vim s.sh
#!/bin/bash

for i in {1..9};do
for j in {1..9};do
echo -n "$j*$i=$(($j*$i))  "
if [ $i == $j ];then
echo -e "\n"
break
fi
done
done

[root@ns2 ~]# bash s.sh
1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

 

Guess you like

Origin www.cnblogs.com/cxm123123form/p/11448706.html