Search code examples
bashshellcentos.htpasswd

How to get the exit status of htpasswd?


I am working on a shell script to ease htpasswd management for accounts.

I am trying to check if the htpasswd was created properly using the expected exit status from the docs

this is what I have so far:

Existing htpasswd:

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

New htpasswd:

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

For some reason, this is being successful but I am not able to capture the exit status.

This is my check:

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

Is there a better method to get the exit status?


Solution

  • You can just do:

    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