Sample Header Ad - 728x90

I fail to match the standard output of a posix shell function with the text I want to test it against

1 vote
1 answer
59 views
I want to test if the output of a function matches the text I envision it should write to standard output. But the code below never prints
PASS
and I can't figure out why. I also tried putting various
\n
characters or deleting them in the variable
but that didn't help.
error_out()
{
    printf "%s\n" "${ERROR}"
    printf "Return to exit\n"
    read -r THROW_AWAY
    exit
}

ERROR="Test output."
OUTPUT_TO_TEST=$(error_out)
GROUND_TRUTH="Test output.\nReturn to exit\n"
{ [ "${OUTPUT_TO_TEST}" = "${GROUND_TRUTH}" ] \
         && printf "error_out test PASS\n"; } \
    || { \
        printf "error_out test FAIL\n"; \
    }
Asked by branco (13 rep)
Dec 8, 2024, 09:25 PM
Last activity: Dec 9, 2024, 09:53 AM