Yes, this is a simple script, do not write do not know, write to find out how many shortcomings.
The following script is mainly used to bulk add the user's script, originally very simple script, but added some judgment, to verify their own learning.
1. Determine if user input is incorrect
2. Determine if the user exists, the existence of skip no longer processing
The code is as follows:
#/bin/bash# Batch Add user script #判断用户是否存在函数function panduan () { for nametmp in $ (cat /tmp/user.txt) do if [ $nametmp = $1 ] then return 6 break fi done }cat /etc/passwd|awk -f : ' {print $1} ' &>>/tmp /user.txt# Read user input information read -p "Please enter the prefix:" nameread -p "Please enter the number:" numread -p " Please enter the initial password: password #判断输入是否为空if [ -z $name -o -z $num -o -z "$password" ] then echo "Input cannot be empty" else# determine whether the number of users is formatted as a number, exit echo $num without a number | grep ^[0-9]*$&>/dev/null if [ $? -eq 0 ] then for i in ' seq 1 $num ' do #判断用户是否存在 panduan $name $i&>/dev/null if [ $? -ne 6 ] then useradd $name $i>>/dev/null echo "$password" |passwd -- stdin $name $i>>/dev/null echo "User name: $name $i Password: $password Create success" else echo "User $name$i exist, do not process" fi done else echo "Number of users entered incorrectly, please enter a number" fi fi
Ask for advice, there is no easier way.
This article is from the "Tommy Lin" blog, so be sure to keep this source http://linyingyong.blog.51cto.com/6609544/1812382
Shell script Batch Add user