写一个脚本:
1、使用函数实现:
判断一个用户是否存在,用户名通过参数传递而来;
如果存在,就显示此用户的shell和UID
如果不存在,就说此用户不存在;
2、提示用户输入用户名,而后将其传递给上面的函数;
3、判断结束后不退出,而是提示用户可继续输入其它用户名,或输入(q)退出;
#!/bin/bash
##Author: Yang Jing
##time: 2014-10-17
##description: for judge the user exists or not
user_exists() {
read -p "please input a user name: " User
[ $User == "q" ] && exit 0
cat /etc/passwd | grep "^$User:" &> /dev/null
RETRAL=$?
if [ $RETRAL -ne 0 ];then
echo " no such user $User"
else
echo "$User uid is `id -u $User`"
echo "$User shell is `echo $RETRAL | cut -d: -f 7`"
fi
}
while true;do
user_exists
done
时间: 2024-10-03 17:49:00