[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to preexec function error code #46

Merged
merged 1 commit into from
Jun 30, 2017

Conversation

jombooth
Copy link
Contributor

Some friends and I are using bash-preexec in an application wherein our users sometimes load multiple projects that depend on bash-preexec in their shells. One of these uses extdebug, along with the useful feature that the command the user tries to run is skipped if the last function in preexec_functions returns an error - but we run into problems where the last function in preexec ends up being, eg, one that always returns 0, because the user also sources another script which tacks an extra function onto the end of preexec_functions.

This script changes the behavior of bash-preexec so that an error from any function in the array causes the overall debug trap call to return 1, so that extdebug can be used reliably in conjunction with multiple projects that use bash-preexec.

bash-preexec.sh Outdated
@@ -199,14 +199,16 @@ __bp_preexec_invoke_exec() {
if type -t "$preexec_function" 1>/dev/null; then
__bp_set_ret_value $__bp_last_ret_value
$preexec_function "$this_command"
preexec_ret_value="$?"
if (( $? != 0 )); then
Copy link
Owner
@rcaloras rcaloras Jun 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small style comment. Also why don't we set the value to $??

if [[ "$?" != 0 ]]; then
  preexec_ret_value="$?"
fi

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This preserves the "blocking" behavior of preexec returning non-zero even if another preexec function returns 0, which is ideal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to $?.

# Also preserves the return value of the last function executed in preexec
# If `extdebug` is enabled a non-zero return value from the last function
# in prexec causes the command not to execute
# Restore the last argument of the last executed command, and set the return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating comments 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np! :)

@rcaloras
Copy link
Owner

@brandonweeks Any thoughts on this change? You were the original contributor of preserving the return status of the last function :)

Copy link
Contributor
@brandonweeks brandonweeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commits should be squashed. :)

bash-preexec.sh Outdated
@@ -199,14 +199,16 @@ __bp_preexec_invoke_exec() {
if type -t "$preexec_function" 1>/dev/null; then
__bp_set_ret_value $__bp_last_ret_value
$preexec_function "$this_command"
preexec_ret_value="$?"
if (( $? != 0 )); then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This preserves the "blocking" behavior of preexec returning non-zero even if another preexec function returns 0, which is ideal.

@rcaloras
Copy link
Owner

@jombooth I'll stamp and merge once comments are addressed. Thanks for submitting!

@jombooth jombooth force-pushed the error-if-errors branch 3 times, most recently from 0711550 to cafcc6a Compare June 27, 2017 05:40
@jombooth
Copy link
Contributor Author

SGTM - of course!

bash-preexec.sh Outdated
@@ -199,14 +200,18 @@ __bp_preexec_invoke_exec() {
if type -t "$preexec_function" 1>/dev/null; then
__bp_set_ret_value $__bp_last_ret_value
$preexec_function "$this_command"
preexec_ret_value="$?"
preexec_function_ret_value="$?"
if (( $preexec_function_ret_value != 0 )); then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also change this to be double square brackets? That's the style for this file.

if [[ $preexec_function_ret_value != 0 ]]; then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The round brackets are actually a different construct (arithmetic expansions - http://tldp.org/LDP/abs/html/arithexp.html#ARITHEXPREF) that seem pretty reasonable here, since the variable used is guaranteed to be an integer. Doing it the other way would be a string comparison, which works, but I think this approach is preferable. Happy to be overruled on this, as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also - the reason I didn't quote the variable is that they're not needed inside arithmetic expansions as word splitting, etc, don't occur. The $ could be dropped as well, but I find code that does that a little confusing to read since I'm used to $-dereferencing of variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi - decided to go ahead and make the change.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jombooth I think your point is valid! I would have merged :) This way is more consistent I guess.

@jombooth
Copy link
Contributor Author

I made the change requested earlier, from (( to [[. Please let me know if anything else is needed!

@rcaloras
Copy link
Owner

@jombooth Sorry for the delayed response. Thanks for addressing changes and your contributions! Much obliged!

@rcaloras
Copy link
Owner

👍 🍔 🍟

@rcaloras rcaloras merged commit 469cc22 into rcaloras:master Jun 30, 2017
@jombooth jombooth deleted the error-if-errors branch September 11, 2017 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants