【Shell编程】 (函数)判断用户是否存在

要求用户输入一个用户名,查询系统中是否存在该用户,若有打印用户存在,若不存在添加该用户

#!/bin/bash 

check_user()
{
	#判断用户是否存在passwd中
	i=`cat /etc/passwd | cut -f1 -d':' | grep -w "$1" -c`
	if [ $i -le 0 ]; then
		echo "User $1 is not in the passwd"
		return 0
	else
	#显示用户存在
		echo "User $1 is in then passwd"
		return 1
	fi
}

echo "Iput username:"
read uname
check_user $uname
if [ $? -eq 0 ]
then
	#添加此用户
	sudo useradd $uname
	echo "user $uname add!!!"
fi

猜你喜欢

转载自blog.csdn.net/feit2417/article/details/81603390