Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
Hyppy Avatar asked Jan 21 '26 13:01

Hyppy


1 Answers

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
like image 199
Hyppy Avatar answered Jan 24 '26 07:01

Hyppy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!