Hyppy
Hyppy

Reputation: 146

Script logic from within bash command substitution

I have the following snippet in a script:

exec 3<<<"$(zenity --entry --title="Enter PIN" | validate_pin || error_handler )"

Elsewhere, I must pass the PIN through a file descriptor, hence the redirection and command substitution here.

The output of validate_pin is a modified value of the entered PIN, but the exit code determines if it was successful and valid. What can I put into error_handler in order to manage any failures?

If I have exit, it only exits the command substitution subshell. Assigned variables (FAIL=1, etc) are also wiped out as soon as the command substitution subshell is closed. Reading the file descriptor to check works, but then it's closed and the subsequent process can't use it.

Upvotes: 3

Views: 59

Answers (1)

Hyppy
Hyppy

Reputation: 146

The best method I've found so far is to use a temporary file.

exec 3<<<"$(zenity --entry --title="Enter PIN" | validate_pin || touch .fail )"
if [[ -f .fail ]] ; then
    do_stuff
fi

Upvotes: 1

Related Questions