1. 程式人生 > >shell指令碼編寫改密功能

shell指令碼編寫改密功能

#! /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中

在這裡插入圖片描述