Linux shell scripts How to create a user with several looping statements

Source: Internet
Author: User
Tags stdin

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.