2012/2/18 franz.reitinger <franz.reitinger@htl-wels.at>:
> First of all I suppose you're using the bash.
Yes, sorry for not mentioning that.
> Amoung others the shell treats several parameters specially; these
> parameters may only be referenced and assignement to them is not allowed.
But I didn't assign anything to it, did I?
>
> One of them is $?, which expands to the exit status of the most recently
> executed command. However, the exit status 0 means that the command has been
> executed without errors; any other return value has to be interpreted as
> error code.
Yes, but [[ $? ]] returns the same result no matter the value of $?,
so [[ $Something ]] doesn't seem to be a valid syntax, is it? Maybe I
just confuse bash scripting with other things, such as C and even
Basic:
if (x) {
dostuff;
domorestuff;
}
or
if x then
dostuff
domorestuff
endif
> e.g.
> # any command ...
> if [ $? -ne 0 ]; then
> * # error handling
> fi
I suppose ”if [[ $? == 0 ]]” is equal to ”if [ $? -ne 0 ]”, right? And
I think that even ”if [[ $? = 0 ]]” is allowed and means the same
thing, as far as I have seen.
Kind regards
Johnny Rosenberg
ジョニー・*ーゼンバーグ
>
> /franzR
>
> On Sat, 18 Feb 2012 13:56:50 +0100, Johnny Rosenberg wrote:
>>
>> I tried this with the $? variable, but failed. Seems like [[ $? ]] is
>> always true no matter the value of $?. [[ $? == 0 ]] seems to work,
>> though.
>>
>> I thought that ”if [[ $? ]]” means ”if $? is True”. What is True
>> anyway? 0? 1? Anything but 0?
>>
>> I am not sure what I am asking for here, but any kind of input in the
>> subject is welcome. Thanks.
>>
>>
>> Kind regards
>>
>> Johnny Rosenberg
>> ジョニー・*ーゼンバーグ
>
> /* ************************************************** **********
> * Mail checked by Avira virus scanner (mail.htl-wels.at).
> ************************************************** **********
> */
>
>
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 02:02 PM
"Robert P. J. Day"
if – in what situations does this work?
On Sat, 18 Feb 2012, franz.reitinger wrote:
> First of all I suppose you're using the bash.
> Amoung others the shell treats several parameters specially; these
> parameters may only be referenced and assignement to them is not
> allowed.
>
> One of them is $?, which expands to the exit status of the most
> recently executed command. However, the exit status 0 means that the
> command has been executed without errors; any other return value has to
> be interpreted as error code.
> e.g.
> # any command ...
> if [ $? -ne 0 ]; then
> # error handling
> fi
i'd have to check but i think the constuct
if [[ $VAR ]]
does nothing but check if either that variable is defined, or if it
has a non-zero string length. so no matter what the value of $? is,
if [[ $? ]]
will always evaluate to true. you should verify that yourself,
though.
if you're checking the returned value of $?, you should always
compare it explicitly.
rday
--
================================================== ======================
Robert P. J. Day Ottawa, Ontario, CANADA
http://crashcourse.ca
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 02:26 PM
J
if – in what situations does this work?
On Sat, Feb 18, 2012 at 10:02, Robert P. J. Day <rpjday@crashcourse.ca> wrote:
> On Sat, 18 Feb 2012, franz.reitinger wrote:
>
>> First of all I suppose you're using the bash.
>> Amoung others the shell treats several parameters specially; these
>> parameters may only be referenced and assignement to them is not
>> allowed.
>>
>> One of them is $?, which expands to the exit status of the most
>> recently executed command. However, the exit status 0 means that the
>> command has been executed without errors; any other return value has to
>> be interpreted as error code.
>> e.g.
>> # any command ...
>> if [ $? -ne 0 ]; then
>> * * # error handling
>> fi
>
> *i'd have to check but i think the constuct
>
> *if [[ $VAR ]]
>
> does nothing but check if either that variable is defined, or if it
> has a non-zero string length. *so no matter what the value of $? is,
Correct:
bladernr@klaatu:~$ A=foo
bladernr@klaatu:~$ if [[ $A ]]; then echo YES; fi
YES
bladernr@klaatu:~$ if [[ $B ]]; then echo YES; fi
AFAIK, that syntax will only work on user defined variables...
>
> *if [[ $? ]]
>
> will always evaluate to true. *you should verify that yourself,
> though.
>
> *if you're checking the returned value of $?, you should always
> compare it explicitly.
>
> rday
>
> --
>
> ================================================== ======================
> Robert P. J. Day * * * * * * * * * * * * * * * * Ottawa, Ontario, CANADA
> * * * * * * * * * * * *http://crashcourse.ca
>
> Twitter: * * * * * * * * * * * * * * * * * * * http://twitter.com/rpjday
> LinkedIn: * * * * * * * * * * * * * * * http://ca.linkedin.com/in/rpjday
> ================================================== ======================
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 02:31 PM
Johnny Rosenberg
if – in what situations does this work?
2012/2/18 Robert P. J. Day <rpjday@crashcourse.ca>:
> On Sat, 18 Feb 2012, franz.reitinger wrote:
>
>> First of all I suppose you're using the bash.
>> Amoung others the shell treats several parameters specially; these
>> parameters may only be referenced and assignement to them is not
>> allowed.
>>
>> One of them is $?, which expands to the exit status of the most
>> recently executed command. However, the exit status 0 means that the
>> command has been executed without errors; any other return value has to
>> be interpreted as error code.
>> e.g.
>> # any command ...
>> if [ $? -ne 0 ]; then
>> * * # error handling
>> fi
>
> *i'd have to check but i think the constuct
>
> *if [[ $VAR ]]
>
> does nothing but check if either that variable is defined, or if it
> has a non-zero string length. *so no matter what the value of $? is,
>
> *if [[ $? ]]
>
> will always evaluate to true. *you should verify that yourself,
> though.
>
> *if you're checking the returned value of $?, you should always
> compare it explicitly.
Yes, you're right!
~$ xyz=""
~$ if [[ $xyz ]]; then echo "True"; else echo "False"; fi
False
~$ xyz="a"
~$ if [[ $xyz ]]; then echo "True"; else echo "False"; fi
True
~$
Thanks!
Kind regards
Johnny Rosenberg
ジョニー・*ーゼンバーグ!
>
> rday
>
> --
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 02:41 PM
"Robert P. J. Day"
if – in what situations does this work?
On Sat, 18 Feb 2012, J wrote:
> On Sat, Feb 18, 2012 at 10:02, Robert P. J. Day <rpjday@crashcourse.ca> wrote:
> > On Sat, 18 Feb 2012, franz.reitinger wrote:
> >
> >> First of all I suppose you're using the bash.
> >> Amoung others the shell treats several parameters specially; these
> >> parameters may only be referenced and assignement to them is not
> >> allowed.
> >>
> >> One of them is $?, which expands to the exit status of the most
> >> recently executed command. However, the exit status 0 means that the
> >> command has been executed without errors; any other return value has to
> >> be interpreted as error code.
> >> e.g.
> >> # any command ...
> >> if [ $? -ne 0 ]; then
> >> * * # error handling
> >> fi
> >
> > *i'd have to check but i think the constuct
> >
> > *if [[ $VAR ]]
> >
> > does nothing but check if either that variable is defined, or if it
> > has a non-zero string length. *so no matter what the value of $? is,
>
> Correct:
>
> bladernr@klaatu:~$ A=foo
> bladernr@klaatu:~$ if [[ $A ]]; then echo YES; fi
> YES
> bladernr@klaatu:~$ if [[ $B ]]; then echo YES; fi
>
> AFAIK, that syntax will only work on user defined variables...
a simpler sequence of statements demonstrating that:
obviously, the value of $? is different in those two cases, but
testing it as above evaluates to "true" each time.
again, if you're testing $?, you should do an actual comparison.
rday
p.s. not to brag or anything but if you have a copy of o'reilly's
"classic shell scripting", yes, that's my name in the
acknowledgements. :-)
--
================================================== ======================
Robert P. J. Day Ottawa, Ontario, CANADA
http://crashcourse.ca
Twitter: http://twitter.com/rpjday
LinkedIn: http://ca.linkedin.com/in/rpjday
================================================== ======================--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 04:23 PM
"franz.reitinger"
if – in what situations does this work?
...
just confuse bash scripting with other things, such as C and even
Basic:
if (x) {
dostuff;
domorestuff;
}
or
if x then
dostuff
domorestuff
endif
In programming languages like C a return value of 0 means that an
error has occurred.
e.g.
if ( expr ) { // expr <> 0 ==> TRUE
...
} else ...
A script executed within a bash shell works contrariwise;
e.g.
if [ expr ]; # you have to check the expression (expr == 0) and this
evalutes to TRUE/FALSE
Unix based apps return a 0 for success and a value <> 0 is used for
returning the error code.
In C (and other programming languages) a return value of 0 evalutes to
FALSE; all other values evaluate to TRUE
e.g.
# any command ...
if [ $? -ne 0 ]; then
* # error handling
fi
I suppose ”if [[ $? == 0 ]]” is equal to ”if [ $? -ne 0 ]”, right?
And
I think that even ”if [[ $? = 0 ]]” is allowed and means the same
thing, as far as I have seen.
Yes, there are several possibilities to write a correct command. Have a
look to the start/stop scripts in /etc/init.d.
* Mail checked by Avira virus scanner (mail.htl-wels.at).
************************************************** **********
*/
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 04:33 PM
"Robert P. J. Day"
if – in what situations does this work?
On Sat, 18 Feb 2012, franz.reitinger wrote:
> ...
> > just confuse bash scripting with other things, such as C and even
> > Basic:
> > if (x) {
> > dostuff;
> > domorestuff;
> > }
> >
> > or
> >
> > if x then
> > dostuff
> > domorestuff
> > endif
> >
> >
>
> In programming languages like C a return value of 0 means that an
> error has occurred.
> e.g.
> if ( expr ) { // expr <> 0 ==> TRUE
> ...
> } else ...
>
> A script executed within a bash shell works contrariwise;
> e.g.
> if [ expr ]; # you have to check the expression (expr == 0) and this
> evalutes to TRUE/FALSE
>
> Unix based apps return a 0 for success and a value <> 0 is used for
> returning the error code.
> In C (and other programming languages) a return value of 0 evalutes to
> FALSE; all other values evaluate to TRUE
let me throw in a bit of info that might clear up a lot of
confusion. one of the forms of the "if" construct in shell
programming is:
if <some command> ; then
where you're testing the exit code ($?) of some command you're
running. linux commands normally return 0 if they work, and the "if"
construct treats that value as "true". (the above is normally called
a "command test" in shell programming.)
there are four general tests in shell programming:
but (and this is important), they are ***all*** just variations of
command tests. ***all*** of them. in every single case, you should
interpret the "if" construct as operating on the result being returned
as if a command had just been run.
this confuses people who look at something like:
if [ $VAR = "rday' ] ; then
and think it's a string comparison. well, ok, it is, but more
fundamentally, it's the *command*
[ $VAR = "rday" ]
being executed, then returning the appropriate value of $?
that '[' thing? it's not just a bracket. it's a command:
$ type [
[ is a shell builtin
$
it is (for the most part) precisely equivalent to forms you might
have seen in older shell programs:
if test $VAR = "rday" ; then
the '[' object is simply another form of the "test" command. is all
this making sense?
one last time -- every usage of "if" in a shell script can be
thought of as a command test.
rday
================================================== ======================
Robert P. J. Day Ottawa, Ontario, CANADA
http://crashcourse.ca
Twitter: http://twitter.com/rpjday
LinkedIn: http://ca.linkedin.com/in/rpjday
================================================== ======================--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
02-18-2012, 06:44 PM
Steven Davies-Morris
if – in what situations does this work?
On 02/18/2012 09:33 AM, Robert P. J. Day wrote:
On Sat, 18 Feb 2012, franz.reitinger wrote:
...
just confuse bash scripting with other things, such as C and even
Basic:
if (x) {
dostuff;
domorestuff;
}
or
if x then
dostuff
domorestuff
endif
In programming languages like C a return value of 0 means that an
error has occurred.
e.g.
if ( expr ) { // expr<> 0 ==> TRUE
...
} else ...
A script executed within a bash shell works contrariwise;
e.g.
if [ expr ]; # you have to check the expression (expr == 0) and this
evalutes to TRUE/FALSE
Unix based apps return a 0 for success and a value<> 0 is used for
returning the error code.
In C (and other programming languages) a return value of 0 evalutes to
FALSE; all other values evaluate to TRUE
let me throw in a bit of info that might clear up a lot of
confusion. one of the forms of the "if" construct in shell
programming is:
if<some command> ; then
where you're testing the exit code ($?) of some command you're
running. linux commands normally return 0 if they work, and the "if"
construct treats that value as "true". (the above is normally called
a "command test" in shell programming.)
there are four general tests in shell programming:
but (and this is important), they are ***all*** just variations of
command tests. ***all*** of them. in every single case, you should
interpret the "if" construct as operating on the result being returned
as if a command had just been run.
this confuses people who look at something like:
if [ $VAR = "rday' ] ; then
and think it's a string comparison. well, ok, it is, but more
fundamentally, it's the *command*
[ $VAR = "rday" ]
being executed, then returning the appropriate value of $?
that '[' thing? it's not just a bracket. it's a command:
$ type [
[ is a shell builtin
$
it is (for the most part) precisely equivalent to forms you might
have seen in older shell programs:
if test $VAR = "rday" ; then
the '[' object is simply another form of the "test" command. is all
this making sense?
one last time -- every usage of "if" in a shell script can be
thought of as a command test.
rday
================================================== ======================
Robert P. J. Day Ottawa, Ontario, CANADA
http://crashcourse.ca
A nice and very clear synopsis, Robert. Thanks.
--
SDM a 21st century schizoid man in SoCal
Systems Theory website www.systemstheory.net
"overfulnoisecascade" coming soon
--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users