【问题标题】:How to get the exit status of htpasswd?如何获取 htpasswd 的退出状态?
【发布时间】:2014-07-22 22:14:49
【问题描述】:

我正在编写一个 shell 脚本来简化帐户的 htpasswd 管理。

我正在尝试检查是否使用来自docs 的预期退出状态正确创建了 htpasswd

这是我目前所拥有的:

现有的 htpasswd:

local _result=$(htpasswd "${HTPASSWD_LOCATION}" "${_htpasswdLogin}")

新的 htpasswd:

local _result=$(htpasswd -c "${HTPASSWD_LOCATION}" "${_htpasswdLogin}")

由于某种原因,这是成功的,但我无法捕获退出状态。

这是我的支票:

if [ "${_result}" = "0" ]; then
    echo "User successfully added to the .htpasswd file"
else
    echo "Failed to add the user to .htpasswd file"
fi

有没有更好的方法来获取退出状态?

【问题讨论】:

    标签: bash shell centos .htpasswd


    【解决方案1】:

    你可以这样做:

    if htpasswd -c "${HTPASSWD_LOCATION}" "${_htpasswdLogin}"; then
        echo "User successfully added to the .htpasswd file"
    else
        echo "Failed to add the user to .htpasswd file"
    fi
    

    【讨论】:

    • 我厌倦了使用这种方法,但它给了我这个错误:/usr/bin/manageAccount: line 735: [: too many arguments 我想这可能是因为我没有发送密码,因为我希望它保持提示。第 735 行:if [ htpasswd -c "${HTPASSWD_LOCATION}" "${_htpasswdLogin}" ]; then
    • 如果你注意到我没有 [ and ] 在 if 条件下。
    • 我意识到我把括号留在里面了。删除它们解决了这个问题。谢谢!将在 2 分钟内接受,因为在那之前它不会允许我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2015-02-15
    • 2018-02-28
    • 1970-01-01
    • 2010-12-28
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多