If conditional using Awk to search for exact Bash 变量字符串
If conditional using Awk to search for exact Bash variable string
在编写 BASH 脚本时,我试图通过引用 /etc/passwd 来确认作为 cmd 行参数传递的用户名是否存在于系统中。我的语法必须关闭,因为我的代码在现有和不存在的用户上失败。
username=${!#}
username_exists(){
exists=$(awk -F ':' ' == "$username" {print }' /etc/passwd | wc -l)
if [[ "$exists" == 0 ]];
then
echo "Username does not exist"
exit
fi
}
我怀疑我的用户名变量没有被正确读取。 And/or if比较错误。
任何帮助都会很棒!
将用户名作为变量传递给 awk,然后让 awk 完成所有处理。
awk -F ':' -v username="$username" ' == "$username" { fnd=1 } END { if ( fnd != 1 ) { print "Username does not exist" } }' /etc/passwd
在编写 BASH 脚本时,我试图通过引用 /etc/passwd 来确认作为 cmd 行参数传递的用户名是否存在于系统中。我的语法必须关闭,因为我的代码在现有和不存在的用户上失败。
username=${!#}
username_exists(){
exists=$(awk -F ':' ' == "$username" {print }' /etc/passwd | wc -l)
if [[ "$exists" == 0 ]];
then
echo "Username does not exist"
exit
fi
}
我怀疑我的用户名变量没有被正确读取。 And/or if比较错误。 任何帮助都会很棒!
将用户名作为变量传递给 awk,然后让 awk 完成所有处理。
awk -F ':' -v username="$username" ' == "$username" { fnd=1 } END { if ( fnd != 1 ) { print "Username does not exist" } }' /etc/passwd