How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsError handling in shell scriptHow to trigger error using Trap commandPrevent SIGINT from interrupting function call and child process(es) withinHow to keep last exit status after testKeep exit codes when trapping SIGINT and similar?How to make `local` capture the exit code?Restoring tty correctly with sttyPerl: Error code returned to open3 after killed programexceptions to ERR trapShell_exec of dynamically created script with else
Wolves and sheep
How do you keep chess fun when your opponent constantly beats you?
Why use ultrasound for medical imaging?
Did the new image of black hole confirm the general theory of relativity?
How does ice melt when immersed in water?
In horse breeding, what is the female equivalent of putting a horse out "to stud"?
How long does the line of fire that you can create as an action using the Investiture of Flame spell last?
What's the point in a preamp?
Keeping a retro style to sci-fi spaceships?
Python - Fishing Simulator
How can I protect witches in combat who wear limited clothing?
Can the DM override racial traits?
What LEGO pieces have "real-world" functionality?
Windows 10: How to Lock (not sleep) laptop on lid close?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
What aspect of planet Earth must be changed to prevent the industrial revolution?
When did F become S in typeography, and why?
Is this wall load bearing? Blueprints and photos attached
How to pronounce 1ターン?
How to test the equality of two Pearson correlation coefficients computed from the same sample?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
How can I define good in a religion that claims no moral authority?
How should I replace vector<uint8_t>::const_iterator in an API?
Why can't wing-mounted spoilers be used to steepen approaches?
How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsError handling in shell scriptHow to trigger error using Trap commandPrevent SIGINT from interrupting function call and child process(es) withinHow to keep last exit status after testKeep exit codes when trapping SIGINT and similar?How to make `local` capture the exit code?Restoring tty correctly with sttyPerl: Error code returned to open3 after killed programexceptions to ERR trapShell_exec of dynamically created script with else
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
PROBLEM:
I have a shell program that I have been writing but I can't find out how to make sure that trap
is trapping for cleanup at the end or because of a error in some command, it cleans up either way.
Here is the code:
################################### Successful exit then this cleanup ###########################################################3
successfulExit()
echo "Failed to remove the install directory!!!!!!!!"; exit 155;
###############################################################################################################################33
####### Catch the program on successful exit and cleanup
trap successfulExit EXIT
QUESTION:
How can I make trap
only trap EXIT
on program finish?
Here is the full script:
debianConfigAwsome.5.3.sh
shell-script exit-status trap
add a comment |
PROBLEM:
I have a shell program that I have been writing but I can't find out how to make sure that trap
is trapping for cleanup at the end or because of a error in some command, it cleans up either way.
Here is the code:
################################### Successful exit then this cleanup ###########################################################3
successfulExit()
echo "Failed to remove the install directory!!!!!!!!"; exit 155;
###############################################################################################################################33
####### Catch the program on successful exit and cleanup
trap successfulExit EXIT
QUESTION:
How can I make trap
only trap EXIT
on program finish?
Here is the full script:
debianConfigAwsome.5.3.sh
shell-script exit-status trap
add a comment |
PROBLEM:
I have a shell program that I have been writing but I can't find out how to make sure that trap
is trapping for cleanup at the end or because of a error in some command, it cleans up either way.
Here is the code:
################################### Successful exit then this cleanup ###########################################################3
successfulExit()
echo "Failed to remove the install directory!!!!!!!!"; exit 155;
###############################################################################################################################33
####### Catch the program on successful exit and cleanup
trap successfulExit EXIT
QUESTION:
How can I make trap
only trap EXIT
on program finish?
Here is the full script:
debianConfigAwsome.5.3.sh
shell-script exit-status trap
PROBLEM:
I have a shell program that I have been writing but I can't find out how to make sure that trap
is trapping for cleanup at the end or because of a error in some command, it cleans up either way.
Here is the code:
################################### Successful exit then this cleanup ###########################################################3
successfulExit()
echo "Failed to remove the install directory!!!!!!!!"; exit 155;
###############################################################################################################################33
####### Catch the program on successful exit and cleanup
trap successfulExit EXIT
QUESTION:
How can I make trap
only trap EXIT
on program finish?
Here is the full script:
debianConfigAwsome.5.3.sh
shell-script exit-status trap
shell-script exit-status trap
edited 2 hours ago
muru
37.6k589165
37.6k589165
asked 7 hours ago
somethingSomethingsomethingSomething
1,850103460
1,850103460
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
On entry to the EXIT
trap, $?
contains the exit status. That's the same value you'd find as $?
after calling this script in another shell: either the argument passed to exit
(truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e
, it's the return status of the command that triggered the implicit exit
.
Usually you should save $?
and exit again with the same status.
cleanup ()
if [ -n "$1" ]; then
echo "Aborted by $1"
elif [ $status -ne 0 ]; then
echo "Failure (status $status)"
else
echo "Success"
fi
trap 'status=$?; cleanup; exit $status' EXIT
trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512331%2fhow-to-make-trap-know-if-the-exit-is-after-successful-program-finish-or-becaus%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
On entry to the EXIT
trap, $?
contains the exit status. That's the same value you'd find as $?
after calling this script in another shell: either the argument passed to exit
(truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e
, it's the return status of the command that triggered the implicit exit
.
Usually you should save $?
and exit again with the same status.
cleanup ()
if [ -n "$1" ]; then
echo "Aborted by $1"
elif [ $status -ne 0 ]; then
echo "Failure (status $status)"
else
echo "Success"
fi
trap 'status=$?; cleanup; exit $status' EXIT
trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM
add a comment |
On entry to the EXIT
trap, $?
contains the exit status. That's the same value you'd find as $?
after calling this script in another shell: either the argument passed to exit
(truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e
, it's the return status of the command that triggered the implicit exit
.
Usually you should save $?
and exit again with the same status.
cleanup ()
if [ -n "$1" ]; then
echo "Aborted by $1"
elif [ $status -ne 0 ]; then
echo "Failure (status $status)"
else
echo "Success"
fi
trap 'status=$?; cleanup; exit $status' EXIT
trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM
add a comment |
On entry to the EXIT
trap, $?
contains the exit status. That's the same value you'd find as $?
after calling this script in another shell: either the argument passed to exit
(truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e
, it's the return status of the command that triggered the implicit exit
.
Usually you should save $?
and exit again with the same status.
cleanup ()
if [ -n "$1" ]; then
echo "Aborted by $1"
elif [ $status -ne 0 ]; then
echo "Failure (status $status)"
else
echo "Success"
fi
trap 'status=$?; cleanup; exit $status' EXIT
trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM
On entry to the EXIT
trap, $?
contains the exit status. That's the same value you'd find as $?
after calling this script in another shell: either the argument passed to exit
(truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e
, it's the return status of the command that triggered the implicit exit
.
Usually you should save $?
and exit again with the same status.
cleanup ()
if [ -n "$1" ]; then
echo "Aborted by $1"
elif [ $status -ne 0 ]; then
echo "Failure (status $status)"
else
echo "Success"
fi
trap 'status=$?; cleanup; exit $status' EXIT
trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM
answered 6 hours ago
GillesGilles
547k13011131631
547k13011131631
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512331%2fhow-to-make-trap-know-if-the-exit-is-after-successful-program-finish-or-becaus%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown