思路:
判断用户是否存在,标记flag
存在,修改用户密码
不存在,创建用户并修改密码
#!/bin/bash
#the script act on a batch of add user,username at the same password
name="admin"
password="demo"
userList="cat /etc/passwd | awk -F ':' '{print $1}'"
flag=false
for name in $userList; do
#statements
flag=true
done
if [ $flag=true ]; then
#change password
echo "user exist"
else
echo "create user admin"
useradd /home/$name $name
fi
echo "change password to demo"
echo $password | passwd --stdin $name
echo "succeed"
输出如下:
实际:
用户不存在且没有创建成功
Wouldn’t it be more convenient to use the id command to determine?
Note that your useradd specifies the home directory parameter, otherwise the execution will be unsuccessful.
And to determine whether the user exists in /etc/passwd, you can also use:
See if it is equal to 0.
The reason why your user should include the } symbol