Bash - run one script when previous is sucessful else run another script?Bash - how to run a command after...
Why don't electrons take the shorter path in coils?
What to say to a student who has failed?
Can pay be witheld for hours cleaning up after closing time?
How do I request a longer than normal leave of absence period for my wedding?
Does travel insurance for short flight delays exist?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
Sun setting in East!
Why can't an Airbus A330 dump fuel in an emergency?
See details of old sessions
How to use "Du hast/ Du hattest'?
Is a player able to change alignment midway through an adventure?
Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?
Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?
What is the history of the university asylum law?
Why were movies shot on film shot at 24 frames per second?
Does norwegian.no airline overbook flights?
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Are there account age or level requirements for obtaining special research?
In an emergency, how do I find and share my position?
Singleton Design Pattern implementation in a not traditional way
Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?
Start from ones
What is the hex versus octal timeline?
Why is my Earth simulation slower than the reality?
Bash - run one script when previous is sucessful else run another script?
Bash - how to run a command after the previous finished?Run a script after some command were executedCron automated bash script to run run 1 bash script then another, plus integrity checkDouble click Linux Bash Script and debug with pause commandsBash script to extract message id and force sendBash trap in function, but executed when entire script exits?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I think my question is similar to this one but with one extrat step - say I have 3 scripts:
- main_script.sh
- report_success.sh
- report_failure.sh
How can I do something like this pseudo-code does:
if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh
?
Thanks!
bash shell-script control-flow
add a comment |
I think my question is similar to this one but with one extrat step - say I have 3 scripts:
- main_script.sh
- report_success.sh
- report_failure.sh
How can I do something like this pseudo-code does:
if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh
?
Thanks!
bash shell-script control-flow
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago
add a comment |
I think my question is similar to this one but with one extrat step - say I have 3 scripts:
- main_script.sh
- report_success.sh
- report_failure.sh
How can I do something like this pseudo-code does:
if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh
?
Thanks!
bash shell-script control-flow
I think my question is similar to this one but with one extrat step - say I have 3 scripts:
- main_script.sh
- report_success.sh
- report_failure.sh
How can I do something like this pseudo-code does:
if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh
?
Thanks!
bash shell-script control-flow
bash shell-script control-flow
edited 2 days ago
user3768495
asked 2 days ago
user3768495user3768495
1646 bronze badges
1646 bronze badges
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago
add a comment |
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
Assuming your main_script.sh
is fairly standard and passes a 0
for a successful run and 1
for an unsuccessful run, you can probe the status via $?
like so:
./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi
There is almost never a reason to compare or even use$?
directly, unless you need to save it for later (which you don't need to here).
– Kusalananda♦
2 days ago
add a comment |
if main_script.sh; then
report_success.sh
else
report_failure.sh
fi
This uses the exit status of main_script.sh
to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.
New contributor
add a comment |
It's as simple as this:
true && echo ok || echo fail
false && echo ok || echo fail
A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.
./main_script.sh && ./report_success.sh || ./report_failure.sh
If you had to run several commands for the ok or fail case, use braces. No need for several scripts:
./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }
Note that your commands with&&
and||
would run./report_failure.sh
if either of./main_script.sh
or./report_success.sh
failed. This is not the same thing as an explicitif-then-else
.
– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
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');
});
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%2f536539%2fbash-run-one-script-when-previous-is-sucessful-else-run-another-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your main_script.sh
is fairly standard and passes a 0
for a successful run and 1
for an unsuccessful run, you can probe the status via $?
like so:
./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi
There is almost never a reason to compare or even use$?
directly, unless you need to save it for later (which you don't need to here).
– Kusalananda♦
2 days ago
add a comment |
Assuming your main_script.sh
is fairly standard and passes a 0
for a successful run and 1
for an unsuccessful run, you can probe the status via $?
like so:
./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi
There is almost never a reason to compare or even use$?
directly, unless you need to save it for later (which you don't need to here).
– Kusalananda♦
2 days ago
add a comment |
Assuming your main_script.sh
is fairly standard and passes a 0
for a successful run and 1
for an unsuccessful run, you can probe the status via $?
like so:
./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi
Assuming your main_script.sh
is fairly standard and passes a 0
for a successful run and 1
for an unsuccessful run, you can probe the status via $?
like so:
./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi
answered 2 days ago
Jason K LaiJason K Lai
2797 bronze badges
2797 bronze badges
There is almost never a reason to compare or even use$?
directly, unless you need to save it for later (which you don't need to here).
– Kusalananda♦
2 days ago
add a comment |
There is almost never a reason to compare or even use$?
directly, unless you need to save it for later (which you don't need to here).
– Kusalananda♦
2 days ago
There is almost never a reason to compare or even use
$?
directly, unless you need to save it for later (which you don't need to here).– Kusalananda♦
2 days ago
There is almost never a reason to compare or even use
$?
directly, unless you need to save it for later (which you don't need to here).– Kusalananda♦
2 days ago
add a comment |
if main_script.sh; then
report_success.sh
else
report_failure.sh
fi
This uses the exit status of main_script.sh
to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.
New contributor
add a comment |
if main_script.sh; then
report_success.sh
else
report_failure.sh
fi
This uses the exit status of main_script.sh
to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.
New contributor
add a comment |
if main_script.sh; then
report_success.sh
else
report_failure.sh
fi
This uses the exit status of main_script.sh
to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.
New contributor
if main_script.sh; then
report_success.sh
else
report_failure.sh
fi
This uses the exit status of main_script.sh
to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.
New contributor
edited 2 days ago
Kusalananda♦
160k18 gold badges318 silver badges504 bronze badges
160k18 gold badges318 silver badges504 bronze badges
New contributor
answered 2 days ago
dhanlindhanlin
414 bronze badges
414 bronze badges
New contributor
New contributor
add a comment |
add a comment |
It's as simple as this:
true && echo ok || echo fail
false && echo ok || echo fail
A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.
./main_script.sh && ./report_success.sh || ./report_failure.sh
If you had to run several commands for the ok or fail case, use braces. No need for several scripts:
./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }
Note that your commands with&&
and||
would run./report_failure.sh
if either of./main_script.sh
or./report_success.sh
failed. This is not the same thing as an explicitif-then-else
.
– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
add a comment |
It's as simple as this:
true && echo ok || echo fail
false && echo ok || echo fail
A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.
./main_script.sh && ./report_success.sh || ./report_failure.sh
If you had to run several commands for the ok or fail case, use braces. No need for several scripts:
./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }
Note that your commands with&&
and||
would run./report_failure.sh
if either of./main_script.sh
or./report_success.sh
failed. This is not the same thing as an explicitif-then-else
.
– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
add a comment |
It's as simple as this:
true && echo ok || echo fail
false && echo ok || echo fail
A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.
./main_script.sh && ./report_success.sh || ./report_failure.sh
If you had to run several commands for the ok or fail case, use braces. No need for several scripts:
./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }
It's as simple as this:
true && echo ok || echo fail
false && echo ok || echo fail
A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.
./main_script.sh && ./report_success.sh || ./report_failure.sh
If you had to run several commands for the ok or fail case, use braces. No need for several scripts:
./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }
answered 2 days ago
JankaJanka
3065 bronze badges
3065 bronze badges
Note that your commands with&&
and||
would run./report_failure.sh
if either of./main_script.sh
or./report_success.sh
failed. This is not the same thing as an explicitif-then-else
.
– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
add a comment |
Note that your commands with&&
and||
would run./report_failure.sh
if either of./main_script.sh
or./report_success.sh
failed. This is not the same thing as an explicitif-then-else
.
– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
Note that your commands with
&&
and ||
would run ./report_failure.sh
if either of ./main_script.sh
or ./report_success.sh
failed. This is not the same thing as an explicit if-then-else
.– Kusalananda♦
2 days ago
Note that your commands with
&&
and ||
would run ./report_failure.sh
if either of ./main_script.sh
or ./report_success.sh
failed. This is not the same thing as an explicit if-then-else
.– Kusalananda♦
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
Well, yes. But failure of the success report is a failure, too.
– Janka
2 days ago
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');
});
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%2f536539%2fbash-run-one-script-when-previous-is-sucessful-else-run-another-script%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');
});
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');
});
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');
});
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
I guess you are aware tht the syntax you show is not bash. It looks like python.
– guillermo chamorro
2 days ago
Thanks for your comment. I just edited my question.
– user3768495
2 days ago