首页 > shell创建用户,判断同名后,程序没有终止

shell创建用户,判断同名后,程序没有终止

全部代码

#!/bin/bash
echo "please input username"
read username
user="cat /etc/passwd | awk -F ':' '{print $1}' | grep '\<$username\>'" 
if [ "$username" == "$user" ]; then
echo "The user exist"
exit 0
fi
echo "please input password"
read password
useradd -d /home/$username $username
echo $password | passwd --stdin $username

为什么这样执行脚本的时候当用户名同名时不会提示The user exist?
而把user改为下文这样就可以了

user=`awk -F : '{print $1}' /etc/passwd | grep "\<$username\>"`

话说,你的

user="cat /etc/passwd | awk -F ':' '{print $1}' | grep '\<$username\>'" 

后面是字符串吧
``, 使用这个反引号才能执行命令 或者 这样

$(cat /etc/passwd | awk -F ':' '{print $1}' | grep '\<$username\>')

获得命令的结果请使用键盘左上角,数字1旁边的` 符号,如:

user=`cat /etc/passwd | awk -F ":" '{print $1}\' | grep '\<$username\>'`
【热门文章】
【热门文章】