#! /bin/bash
read -p "请输入修改的用户名" user
num=` cat /etc/passwd | cut -f1 -d':' |grep -w $user -c ` #查询user是否在/etc/passwd ,并计算个数
#grep -q "$username" /etc/passwd 另一种方法查询user是否在user
if [ $num -le 0 ]
then echo "$user is not in the passwd"
else
read -p "请输入修改的密码 " passwd
echo "$passwd" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ] # 判断上一个命令是否执行成功 $?
then
echo "${user}密码修改成功"
else
echo "${user}修改失败"
fi
fi
另一种方式判断用户是否在这个passwd中