bash – 检查用户是否存在
发布时间:2020-12-15 16:38:40 所属栏目:安全 来源:网络整理
导读:我想创建一个脚本来检查用户是否存在。我使用的逻辑如下: # getent passwd test /dev/null 21# echo $?0# getent passwd test1 /dev/null 21# echo $?2 所以如果用户存在,那么我们有成功,否则用户不存在。我已经把上面的命令在bash脚本如下: #!/bin/bash
我想创建一个脚本来检查用户是否存在。我使用的逻辑如下:
# getent passwd test > /dev/null 2&>1 # echo $? 0 # getent passwd test1 > /dev/null 2&>1 # echo $? 2 所以如果用户存在,那么我们有成功,否则用户不存在。我已经把上面的命令在bash脚本如下: #!/bin/bash getent passwd $1 > /dev/null 2&>1 if [ $? -eq 0 ]; then echo "yes the user exists" else echo "No,the user does not exist" fi 现在,我的脚本总是说用户存在,无论什么: # sh passwd.sh test yes the user exists # sh passwd.sh test1 yes the user exists # sh passwd.sh test2 yes the user exists 为什么上述条件总是评估为TRUE并且说用户存在? 我在哪里错了? 更新: 阅读所有的答复后,我发现我的脚本中的问题。问题是我重定向getent输出的方式。所以我删除所有的重定向东西,并使得getent线看起来像这样: getent passwd $user > /dev/null 现在我的脚本工作正常。
你可以改变shebang到#!/ bin / bash -vx来调试?
也,尝试下面的方法… ret=false getent passwd $1 >/dev/null 2>&1 && ret=true if $ret; then echo "yes the user exists" else echo "No,the user does not exist" fi (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |