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.
On Mon, May 30, 2011 at 10:38 AM, Dotan Cohen dotancohen@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
On Mon, May 30, 2011 at 17:55, Bob Beers bob.beers@gmail.com wrote:
You can check the return code.
$ ls $ echo $?
0 (usually) indicates success.
Thank you Bob, that is exactly what I was looking for!
Dotan Cohen wrote:
On Mon, May 30, 2011 at 17:55, Bob Beers bob.beers@gmail.com wrote:
You can check the return code.
$ ls $ echo $?
0 (usually) indicates success.
Thank you Bob, that is exactly what I was looking for!
Take notice that you can use $? *only* once. So if you ever need to reuse that status, you must first assign exit code to a variable and then evaluate variable.
Ljubomir
On Mon, May 30, 2011 at 18:05, Ljubomir Ljubojevic office@plnet.rs wrote:
Take notice that you can use $? *only* once. So if you ever need to reuse that status, you must first assign exit code to a variable and then evaluate variable.
Actually, that was kink of obvious to me, but good thing that you pointed it out. Thanks.
On 05/30/2011 10:00 AM, Dotan Cohen wrote:
On Mon, May 30, 2011 at 17:55, Bob Beersbob.beers@gmail.com wrote:
You can check the return code.
$ ls $ echo $?
0 (usually) indicates success.
Thank you Bob, that is exactly what I was looking for!
And when you have several commands in a pipeline, the PIPESTATUS array is your friend:
$ true | false | false | true | true $ echo ${PIPESTATUS[*]} 0 1 1 0 0
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.htmlQuick 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@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@centos.org http://lists.centos.org/mailman/listinfo/centos
On Mon, 30 May 2011, Christopher J. Buckley wrote:
To: CentOS mailing list centos@centos.org From: Christopher J. Buckley chris@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.htmlQuick example:
#!/bin/bash
ls foobar
if [ $? -eq 0 ] ; then echo "successful" else echo "not successful" fi
You get the idea..
Cheers, Chris
Excellent Bash tutorial and reference with loads of working examples. I whole-heartedly recommend it and there are different versions such as PDF format under:
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] -----------------------------------------------------------------
On Mon, May 30, 2011 at 17:59, Christopher J. Buckley chris@cjbuckley.net wrote:
Have a read up on using return codes in Bash. http://tldp.org/LDP/abs/html/exit-status.html
Thanks, Chris, the link was very informative. I should spend more time at the tldp site, I know.
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.
On Tue, May 31, 2011 at 01:14, fred smith fredex@fcshome.stoneham.ma.us wrote:
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.
I also learned in C that main should be an int. Now that I'm studying Java, main is always a void and nobody has been able to explain to me why.
On 05/30/2011 05:14 PM, fred smith wrote:
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.
In a bash script, "exit" with no parameter and just falling off the end of the script are both equivalent to "exit $?" and return the status from the last command or shell construct executed in the script.