Unix Shell - using the return code of a statement directly

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > using the return code of a statement directly





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author using the return code of a statement directly
Keve Nagy

2007-05-22, 7:18 am

Hi Everyone,

I run into this problem enough times now to ask a question about it here.

Q: Under FreeBSD using the sh shell, what is the proper way (or rather, is
there a way) to use the return code of a statement directly?

Things I am trying to achieve are like assign the return code of a command
directly to the value of a variable. Like, consider:

ls -la /k*
v_return_code=$?

This above was the indirect way, and the only way I managed to use so far.
v_return_code=`ls -la /k*` assigns the output of the command to the
variable, not the return code which I am after.

I expected that v_return_code=(ls -la /k*) does the trick, but it generates
a syntax error instead.

$ myvar=(ls -la /k*)
Syntax error: word unexpected (expecting ")")

Leaving a blank/space between the equal sign and the opening curly brace
results in the exact same thing.

myvar={ ls -la;clear }
This runs without any error, does list the directory and then clears the
screen, but $myvar doesn't get the expected value (of 0, or another num).
$ echo "xxx${myvar}yyy"
xxxyyy

I also tried:
$ myvar=$((ls))
$ echo $myvar
0

which looked very promising, but then:
$ myvar=$((ls zzz*))
arithmetic expression: syntax error: "ls zzz*"

So it is not what I hoped it to be.

Similarly, using a return code directly in
"test" is still an unsolved problem for me.

ls -la /k*
if [ $? -ne 0 ]
then
echo "not zero"
fi

Instead, I'd expect to use something like:
if [ (ls -la /k*) -ne 0 ]
then
echo "not zero"
fi

So, is there a way to use the return codes directly?

Regards,
Keve

--
if you need to reply directly:
keve(at)mail(dot)poliod(dot)hu
Janis

2007-05-22, 1:24 pm

On 22 Mai, 13:04, Keve Nagy <nos...@address.invalid> wrote:
> Hi Everyone,
>
> I run into this problem enough times now to ask a question about it here.
>
> Q: Under FreeBSD using the sh shell, what is the proper way (or rather, is
> there a way) to use the return code of a statement directly?
>
> Things I am trying to achieve are like assign the return code of a command
> directly to the value of a variable. Like, consider:
>
> ls -la /k*
> v_return_code=$?
>
> This above was the indirect way, and the only way I managed to use so far.
> v_return_code=`ls -la /k*` assigns the output of the command to the
> variable, not the return code which I am after.
>
> I expected that v_return_code=(ls -la /k*) does the trick, but it generates
> a syntax error instead.
>
> $ myvar=(ls -la /k*)
> Syntax error: word unexpected (expecting ")")
>
> Leaving a blank/space between the equal sign and the opening curly brace
> results in the exact same thing.
>
> myvar={ ls -la;clear }
> This runs without any error, does list the directory and then clears the
> screen, but $myvar doesn't get the expected value (of 0, or another num).
> $ echo "xxx${myvar}yyy"
> xxxyyy
>
> I also tried:
> $ myvar=$((ls))
> $ echo $myvar
> 0
>
> which looked very promising, but then:
> $ myvar=$((ls zzz*))
> arithmetic expression: syntax error: "ls zzz*"
>
> So it is not what I hoped it to be.
>
> Similarly, using a return code directly in
> "test" is still an unsolved problem for me.
>
> ls -la /k*
> if [ $? -ne 0 ]
> then
> echo "not zero"
> fi
>
> Instead, I'd expect to use something like:
> if [ (ls -la /k*) -ne 0 ]
> then
> echo "not zero"
> fi
>
> So, is there a way to use the return codes directly?


if ls -la /k*
then echo succeeded
else echo failed
fi

You may optionally redirect output of ls if necessary.

Janis


> Regards,
> Keve
>
> --
> if you need to reply directly:
> keve(at)mail(dot)poliod(dot)hu



Keve Nagy

2007-05-22, 1:25 pm

Janis wrote:

>
> if ls -la /k*
> then echo succeeded
> else echo failed
> fi
>
> You may optionally redirect output of ls if necessary.
>
> Janis


Shame on me!
This is so obvious that I should have known it in my sleep. :-(
I even have scripts where I have already used this method. This is basic
shell scripting.

Thank you, Janis!
The problem of assigning the return code to a variable redirected my mind
away from the simple and natural way of thinking. I was so much limited to
that problem that it controlled my thoughts related to the other issue too.

The question remains though, if I am not checking for a zero or non-zero
return code, but for a specific non-zero return code.
ls -la /k*
if [ $? -eq 2 ]
then echo "two"
fi

Could this be done using the return code directly, without the $? variable ?

Anyway, you have given me a very useful lesson today. And I am extremely
grateful for that. Thank you!

Best regards,
Keve

--
if you need to reply directly:
keve(at)mail(dot)poliod(dot)hu
Glenn Jackman

2007-05-22, 1:25 pm

At 2007-05-22 09:18AM, "Keve Nagy" wrote:
> The question remains though, if I am not checking for a zero or non-zero
> return code, but for a specific non-zero return code.
> ls -la /k*
> if [ $? -eq 2 ]
> then echo "two"
> fi
>
> Could this be done using the return code directly, without the $? variable ?


I think not, but here is another way to test:

case $( ls -la /k* ; echo $? ) in
2) echo ls returns two ;;
default) echo ls does not return two ;;
esac

Or, generalize the test:

exit_status () { eval "$@" ; echo $?; }
case $( exit_status ls -la /k* ) in
#...

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Janis

2007-05-22, 1:25 pm

On 22 Mai, 15:18, Keve Nagy <nos...@address.invalid> wrote:
> Janis wrote:
>
>
>
>
>
>
>
> Shame on me!
> This is so obvious that I should have known it in my sleep. :-(
> I even have scripts where I have already used this method. This is basic
> shell scripting.
>
> Thank you, Janis!
> The problem of assigning the return code to a variable redirected my mind
> away from the simple and natural way of thinking. I was so much limited to
> that problem that it controlled my thoughts related to the other issue too.
>
> The question remains though, if I am not checking for a zero or non-zero
> return code, but for a specific non-zero return code.
> ls -la /k*
> if [ $? -eq 2 ]
> then echo "two"
> fi
>
> Could this be done using the return code directly, without the $? variable ?


Not that I know of. I think I'd do it that way...

if ls -la /k*
then echo success
else case $? in
2) echo my case ;;
*) echo other cases ;;
esac
fi

....if I want to handle the error cases in one block but separated from
the success case.

Janis

> Anyway, you have given me a very useful lesson today. And I am extremely
> grateful for that. Thank you!
>
> Best regards,
> Keve
>
> --
> if you need to reply directly:
> keve(at)mail(dot)poliod(dot)hu



Janis

2007-05-22, 1:25 pm

On 22 Mai, 15:18, Keve Nagy <nos...@address.invalid> wrote:
> Janis wrote:
>
>
>
>
>
>
>
> Shame on me!
> This is so obvious that I should have known it in my sleep. :-(
> I even have scripts where I have already used this method. This is basic
> shell scripting.
>
> Thank you, Janis!
> The problem of assigning the return code to a variable redirected my mind
> away from the simple and natural way of thinking. I was so much limited to
> that problem that it controlled my thoughts related to the other issue too.
>
> The question remains though, if I am not checking for a zero or non-zero
> return code, but for a specific non-zero return code.
> ls -la /k*
> if [ $? -eq 2 ]
> then echo "two"
> fi
>
> Could this be done using the return code directly, without the $? variable ?


Not that I know of. I think I'd do it that way...

if ls -la /k*
then echo success
else case $? in
2) echo my case ;;
*) echo other cases ;;
esac
fi

....if I want to handle the error cases in one block but separated from
the success case.

Janis

> Anyway, you have given me a very useful lesson today. And I am extremely
> grateful for that. Thank you!
>
> Best regards,
> Keve
>
> --
> if you need to reply directly:
> keve(at)mail(dot)poliod(dot)hu



Chris F.A. Johnson

2007-05-22, 7:26 pm

On 2007-05-22, Keve Nagy wrote:
> Hi Everyone,
>
> I run into this problem enough times now to ask a question about it here.
>
> Q: Under FreeBSD using the sh shell, what is the proper way (or rather, is
> there a way) to use the return code of a statement directly?
>
> Things I am trying to achieve are like assign the return code of a command
> directly to the value of a variable. Like, consider:
>
> ls -la /k*
> v_return_code=$?
>
> This above was the indirect way, and the only way I managed to use so far.
> v_return_code=`ls -la /k*` assigns the output of the command to the
> variable, not the return code which I am after.
>
> I expected that v_return_code=(ls -la /k*) does the trick, but it generates
> a syntax error instead.
>
> $ myvar=(ls -la /k*)
> Syntax error: word unexpected (expecting ")")


If you are using a shell with arrays that would store "ls" "-la"
"/k*" as the elements of the array. The last element would be
expanded if there were any matching files. I think you mean:

myvar=$(ls -la /k*)

> Leaving a blank/space between the equal sign and the opening curly brace
> results in the exact same thing.
>
> myvar={ ls -la;clear }
> This runs without any error, does list the directory and then clears the
> screen, but $myvar doesn't get the expected value (of 0, or another num).
> $ echo "xxx${myvar}yyy"
> xxxyyy


That is a variable assignment and a compound command:

myvar=
{ ls -la;clear }

> I also tried:
> $ myvar=$((ls))
> $ echo $myvar
> 0
>
> which looked very promising, but then:
> $ myvar=$((ls zzz*))
> arithmetic expression: syntax error: "ls zzz*"
>
> So it is not what I hoped it to be.


No, $(( ... )) is the syntax for shell arithmetic.

> Similarly, using a return code directly in
> "test" is still an unsolved problem for me.
>
> ls -la /k*
> if [ $? -ne 0 ]
> then
> echo "not zero"
> fi
>
> Instead, I'd expect to use something like:
> if [ (ls -la /k*) -ne 0 ]
> then
> echo "not zero"
> fi
>
> So, is there a way to use the return codes directly?


if ls -la /k*
then
... successful
else
... unsuccessful
fi

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com