Dotan Cohen
2011-May-30 14:38 UTC
[CentOS] Getting the return value of the last command run
All commands return a value, usually 0 if run properly. For instance, try: $ ls && echo "done" $ lsd && echo "done" The echo command is only executed if the ls command exited successfully. If one did not add the echo command with the && after a command, how can he determine if the command exited successfully? I have a particularly troubling script that gives does not mention if it exits successfully or not. I could modify it (and probably will some day) but in general I'd like to know the answer to this question as a learning experience. Thanks. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com
On Mon, May 30, 2011 at 10:38 AM, Dotan Cohen <dotancohen at gmail.com> wrote:> All commands return a value, usually 0 if run properly. For instance, try: > $ ls && echo "done" > $ lsd && echo "done" > > The echo command is only executed if the ls command exited > successfully. If one did not add the echo command with the && after a > command, how can he determine if the command exited successfully?You can check the return code. $ ls $ echo $? 0 (usually) indicates success. - Bob
Christopher J. Buckley
2011-May-30 14:59 UTC
[CentOS] Getting the return value of the last command run
Have a read up on using return codes in Bash. http://tldp.org/LDP/abs/html/exit-status.html <http://tldp.org/LDP/abs/html/exit-status.html>Quick example: #!/bin/bash ls foobar if [ $? -eq 0 ] ; then echo "successful" else echo "not successful" fi You get the idea.. Cheers, Chris On 30 May 2011 15:38, Dotan Cohen <dotancohen at gmail.com> wrote:> All commands return a value, usually 0 if run properly. For instance, try: > $ ls && echo "done" > $ lsd && echo "done" > > The echo command is only executed if the ls command exited > successfully. If one did not add the echo command with the && after a > command, how can he determine if the command exited successfully? I > have a particularly troubling script that gives does not mention if it > exits successfully or not. I could modify it (and probably will some > day) but in general I'd like to know the answer to this question as a > learning experience. > > Thanks. > > -- > Dotan Cohen > > http://gibberish.co.il > http://what-is-what.com > _______________________________________________ > CentOS mailing list > CentOS at centos.org > http://lists.centos.org/mailman/listinfo/centos >-- Kind Regards, Christopher J. Buckley -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20110530/e4c11e0d/attachment-0005.html>
Keith Roberts
2011-May-30 15:04 UTC
[CentOS] Getting the return value of the last command run
On Mon, 30 May 2011, Christopher J. Buckley wrote:> To: CentOS mailing list <centos at centos.org> > From: Christopher J. Buckley <chris at cjbuckley.net> > Subject: Re: [CentOS] Getting the return value of the last command run > > Have a read up on using return codes in Bash. > > http://tldp.org/LDP/abs/html/exit-status.html > > <http://tldp.org/LDP/abs/html/exit-status.html>Quick example: > > #!/bin/bash > > ls foobar > > if [ $? -eq 0 ] ; then > echo "successful" > else > echo "not successful" > fi > > You get the idea.. > > Cheers, > ChrisExcellent Bash tutorial and reference with loads of working examples. I whole-heartedly recommend it and there are different versions such as PDF format under: http://tldp.org/LDP/abs/ Kind Regards, Keith Roberts ----------------------------------------------------------------- Websites: http://www.karsites.net http://www.php-debuggers.net http://www.raised-from-the-dead.org.uk All email addresses are challenge-response protected with TMDA [http://tmda.net] -----------------------------------------------------------------
Dotan Cohen
2011-May-30 15:22 UTC
[CentOS] Getting the return value of the last command run
On Mon, May 30, 2011 at 17:59, Christopher J. Buckley <chris at cjbuckley.net> wrote:> Have a read up on using return codes in Bash. > http://tldp.org/LDP/abs/html/exit-status.htmlThanks, Chris, the link was very informative. I should spend more time at the tldp site, I know. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com
fred smith
2011-May-30 22:14 UTC
[CentOS] Getting the return value of the last command run
On Mon, May 30, 2011 at 05:38:56PM +0300, Dotan Cohen wrote:> All commands return a value, usually 0 if run properly. For instance, try: > $ ls && echo "done" > $ lsd && echo "done" > > The echo command is only executed if the ls command exited > successfully. If one did not add the echo command with the && after a > command, how can he determine if the command exited successfully? I > have a particularly troubling script that gives does not mention if it > exits successfully or not. I could modify it (and probably will some > day) but in general I'd like to know the answer to this question as a > learning experience.Yes, all commands return a value UNLESS it was written by one of the idi,... er, misguided programmers who thinks its ok to write (in C): void main (void) { ... exit(); } because, of course, in C main() always returns SOMETHING. I'm sure it's the same in a bash script, even if the script doesn't explicitly provide a return value I imagine the shell returns something anyway, it's just that it's meaningless when that happens. -- ---- Fred Smith -- fredex at fcshome.stoneham.ma.us ----------------------------- I can do all things through Christ who strengthens me. ------------------------------ Philippians 4:13 -------------------------------