如果用户不存在,我有以下脚本应该存在。
#check if user currently exists on system
if id $User > /dev/null 2>&1
then
#user exists no need to exit program
echo "blah blah, what a waste of space"
else
echo "This user does NOT exists. Please create that user before using this script.\n"
exit
fi
Run Code Online (Sandbox Code Playgroud)
我的问题是,理想情况下,我希望在第一个 if 语句中放置一个“not”,以便我可以减少 if、else 语句。理想情况下,我想要这样的东西:
if !(id $User > /dev/null 2>&1)
then
echo "This user does NOT exists. Please create that user before using this script.\n"
exit
fi
Run Code Online (Sandbox Code Playgroud)
“Not”拼写为!,没有标点符号,后面有一个空格。
if ! id "$user_name" > /dev/null 2>&1
then
echo 1>&2 "This user does NOT exists. Please create that user before using this script.\n"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
您的建议实际上可行,但括号创建了一个子shell 来运行 one 命令id,这是多余的。
其他变化:
"$user_name"USER,它是当前登录用户的名称。变量名区分大小写,但人类不那么敏感。