在编写 BASH 脚本时,我试图通过引用 /etc/passwd 来确认系统上是否存在作为 cmd 行参数传递的用户名。我的语法必须关闭,因为我的代码在现有和不存在的用户上都失败了。
username=${!#}
username_exists(){
exists=$(awk -F ':' '$1 == "$username" {print $1}' /etc/passwd | wc -l)
if [[ "$exists" == 0 ]];
then
echo "Username does not exist"
exit
fi
}
我怀疑我的用户名变量没有被正确读取。和/或 if 比较是错误的。任何帮助都会很棒!