common.sh
#获取当前时间
getTime(){
currentDate=`date +%Y-%m-%d`
currentTime=`date +%H:%M:%S`
echo ${currentDate}" "${currentTime}
}
createuser.sh
#!/bin/bash
#引入公共函数文件
source ./common.sh
#读取自定义用户文件
for line in $(cat users)
do
#字符串分割
OLD_IFS="$IFS"
IFS=":"
arr=($line)
IFS="$OLD_IFS"
username=${arr[0]}
passwd=${arr[1]}
group=${arr[2]}
#判断用户组是否存在,不存在则创建
cat /etc/group | grep "^$group">& /var/null
if [ $? -ne 0 ]
then
groupadd $group
fi
#判断用户是否存在,不存在则创建
cat /etc/passwd | grep "^$username">& /var/null
if [ $? -ne 0 ]
then
useradd -g $group $username
echo $passwd | passwd --stdin $username
fi
done
echo "create user successfully $(getTime)"
users
user001:user001pwd:testgroup
user002:user002pwd:group002
user003:user003pwd:testgroup
user004:user004pwd:group004