Executing multiple Bash commands after OR
I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:
sudo apt-get install fortune || (echo "Installation failed" ; exit)
echo "Installation successful"
I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?
linux bash error-handling scripting
add a comment |
I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:
sudo apt-get install fortune || (echo "Installation failed" ; exit)
echo "Installation successful"
I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?
linux bash error-handling scripting
add a comment |
I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:
sudo apt-get install fortune || (echo "Installation failed" ; exit)
echo "Installation successful"
I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?
linux bash error-handling scripting
I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:
sudo apt-get install fortune || (echo "Installation failed" ; exit)
echo "Installation successful"
I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?
linux bash error-handling scripting
linux bash error-handling scripting
asked Nov 9 at 21:21
AndreasKralj
1637
1637
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Parentheses create a subshell, which is what the exit
commands exits. You want a command group, defined with braces.
sudo apt-get install fortune || echo "Installation failed" >&2; exit 1;
(Note the last semicolon and the spaces around the braces; all are important.)
To keep things more readable, typically you define a function to act as the single command following ||
:
abort () echo "Installation of $1 failed" >&2; exit 1;
sudo apt-get install fortune || abort fortune
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to theabort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
– AndreasKralj
Nov 9 at 21:42
2
@AndreasKralj, considerdie() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus,foo || die "foo failed"
will pass through the exit status offoo
, whereasdie whatever
with a$?
of 0 will use 1.
– Charles Duffy
Nov 9 at 21:47
2
This all become a lot easier if you stop trying to be excessively verbose.apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just writesudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same asexit $?
, and if you want to be explicit just writeexit $?
).
– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53233421%2fexecuting-multiple-bash-commands-after-or%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
Parentheses create a subshell, which is what the exit
commands exits. You want a command group, defined with braces.
sudo apt-get install fortune || echo "Installation failed" >&2; exit 1;
(Note the last semicolon and the spaces around the braces; all are important.)
To keep things more readable, typically you define a function to act as the single command following ||
:
abort () echo "Installation of $1 failed" >&2; exit 1;
sudo apt-get install fortune || abort fortune
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to theabort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
– AndreasKralj
Nov 9 at 21:42
2
@AndreasKralj, considerdie() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus,foo || die "foo failed"
will pass through the exit status offoo
, whereasdie whatever
with a$?
of 0 will use 1.
– Charles Duffy
Nov 9 at 21:47
2
This all become a lot easier if you stop trying to be excessively verbose.apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just writesudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same asexit $?
, and if you want to be explicit just writeexit $?
).
– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
|
show 1 more comment
Parentheses create a subshell, which is what the exit
commands exits. You want a command group, defined with braces.
sudo apt-get install fortune || echo "Installation failed" >&2; exit 1;
(Note the last semicolon and the spaces around the braces; all are important.)
To keep things more readable, typically you define a function to act as the single command following ||
:
abort () echo "Installation of $1 failed" >&2; exit 1;
sudo apt-get install fortune || abort fortune
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to theabort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
– AndreasKralj
Nov 9 at 21:42
2
@AndreasKralj, considerdie() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus,foo || die "foo failed"
will pass through the exit status offoo
, whereasdie whatever
with a$?
of 0 will use 1.
– Charles Duffy
Nov 9 at 21:47
2
This all become a lot easier if you stop trying to be excessively verbose.apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just writesudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same asexit $?
, and if you want to be explicit just writeexit $?
).
– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
|
show 1 more comment
Parentheses create a subshell, which is what the exit
commands exits. You want a command group, defined with braces.
sudo apt-get install fortune || echo "Installation failed" >&2; exit 1;
(Note the last semicolon and the spaces around the braces; all are important.)
To keep things more readable, typically you define a function to act as the single command following ||
:
abort () echo "Installation of $1 failed" >&2; exit 1;
sudo apt-get install fortune || abort fortune
Parentheses create a subshell, which is what the exit
commands exits. You want a command group, defined with braces.
sudo apt-get install fortune || echo "Installation failed" >&2; exit 1;
(Note the last semicolon and the spaces around the braces; all are important.)
To keep things more readable, typically you define a function to act as the single command following ||
:
abort () echo "Installation of $1 failed" >&2; exit 1;
sudo apt-get install fortune || abort fortune
edited Nov 9 at 21:41
answered Nov 9 at 21:34
chepner
244k31231323
244k31231323
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to theabort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
– AndreasKralj
Nov 9 at 21:42
2
@AndreasKralj, considerdie() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus,foo || die "foo failed"
will pass through the exit status offoo
, whereasdie whatever
with a$?
of 0 will use 1.
– Charles Duffy
Nov 9 at 21:47
2
This all become a lot easier if you stop trying to be excessively verbose.apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just writesudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same asexit $?
, and if you want to be explicit just writeexit $?
).
– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
|
show 1 more comment
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to theabort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
– AndreasKralj
Nov 9 at 21:42
2
@AndreasKralj, considerdie() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus,foo || die "foo failed"
will pass through the exit status offoo
, whereasdie whatever
with a$?
of 0 will use 1.
– Charles Duffy
Nov 9 at 21:47
2
This all become a lot easier if you stop trying to be excessively verbose.apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just writesudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same asexit $?
, and if you want to be explicit just writeexit $?
).
– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
– Gordon Davisson
Nov 9 at 21:38
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the
abort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?– AndreasKralj
Nov 9 at 21:42
This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the
abort
function you just wrote? Would I be able to assign $? to a variable in it and then echo that?– AndreasKralj
Nov 9 at 21:42
2
2
@AndreasKralj, consider
die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus, foo || die "foo failed"
will pass through the exit status of foo
, whereas die whatever
with a $?
of 0 will use 1.– Charles Duffy
Nov 9 at 21:47
@AndreasKralj, consider
die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 ));
-- thus, foo || die "foo failed"
will pass through the exit status of foo
, whereas die whatever
with a $?
of 0 will use 1.– Charles Duffy
Nov 9 at 21:47
2
2
This all become a lot easier if you stop trying to be excessively verbose.
apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same as exit $?
, and if you want to be explicit just write exit $?
).– William Pursell
Nov 9 at 21:55
This all become a lot easier if you stop trying to be excessively verbose.
apt-get
prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit
, and the value returned by sudo will be returned by the script. (exit
with you argument is the same as exit $?
, and if you want to be explicit just write exit $?
).– William Pursell
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
That's perfect @Charles Duffy, thank you very much!
– AndreasKralj
Nov 9 at 21:55
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fstackoverflow.com%2fquestions%2f53233421%2fexecuting-multiple-bash-commands-after-or%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