Hello everyone, I am the lone cloud twilight rain, brought to you today is the "Linux shell script several circular statements to create the user's method"
Add USER1~USER20 User
For loop:
#!/bin/bashfor i in {1..20}douseradd User$iecho ' user$i Users to add success ' done
For loop (c-style):
#!/bin/bashfor ((i=1;i<=20;i++)) Douseradd user$i && echo-e "\033[32;1m created user$i success! \033[0m "| | echo-e" \033[31;1M Create user$i failed! \033[0m "Done
Add USER1~USER20 user, password and user name and output the user's password, and the user must change the password at the next logon, the output of the keyword bold display
While loop:
#!/bin/bash#by:zdf#blog:zhangdaifu.blog.51cto.comdeclare-i i=0declare-i total=0while [$i-lt]dolet i++if useradd US Er$i &>/dev/null;then echo "user$i, create success!" echo "User$i" | passwd--stdin user$i &>/dev/null echo-e "user$i Password: \033[1m" user$i "\033[0m" passwd-e user$i >/dev/nullle T total++ echoelse echo-e "User: \033[1m" user$i "\033[0m already exists!" Fidoneechoecho-e "Altogether created: \033[1m" $Total "\033[0m users!"
Add USER1~USER20 user, password and user name and output the user's password, and the user must change the password at the next logon, all the output code is color display, the keyword bold display
Until cycle:
#!/bin/bash#by:zdf#blog:zhangdaifu.blog.51cto.comdeclare-i i=0declare-i total=0until [$i-ge]dolet i++if useradd US er$i &>/dev/null;then echo-e "\033[1m" user$i "\033[0m created successfully!" echo "User$i" | passwd--stdin user$i &>/dev/null echo-e "user$i Password: \v\033[1m" user$i "\033[0m" passwd-e user$i >/dev/null Let Total++else echo-e "\033[31m user \033[31;1m" user$i "\033[0m\033[31m already exists!\033[0m" FIDONEECHOECHO-E "\033[32m altogether created: \ 033[32;1m "$Total" \033[0m "\033[32m user!\033[0m"
The script output is Chinese, if the system can not display Chinese, change to English!
Original works, reproduced please indicate the source, thank you!
Linux shell scripts How to create a user with several looping statements