??? What ??? Häääh ???

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

??? What ??? Häääh ???

#1 Post by RSH »

Hello.

I am more than a bit confused! You may laugh about that issue, though it is like it is.

[ $? -ne 0 ] || exit
[ $? -eq 0 ] || exit

[ $? -ne 0 ] && exit
[ $? -eq 0 ] && exit

Can someone translate these 4 lines of code into something like:

if [ $x -xx X ]; then
... ... ...
fi


This i would be able to understand...

Thanks

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#2 Post by jamesbond »

You are not the only one, this kills me too when I started figuring script.

[ $? -ne 0 ] || exit
is identical to
if ! [ $? -ne 0 ]; then
exit
fi

Which is the same as
if [ $? -eq 0 ]; then
exit
fi




[ $? -eq 0 ] || exit
is identical to
if ! [ $? -eq 0 ]; then
exit
fi

which means
if [ $? -ne 0 ]; then
exit
fi


[ $? -ne 0 ] && exit
is identical to
if [ $? -ne 0 ]; then
exit
fi


[ $? -eq 0 ] && exit
is identical to
if [ $? -eq 0 ]; then
exit
fi


I hope I get it all right.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#3 Post by rcrsn51 »

Here's how these operators work.

The || operator is "or". The && operator is "and".

In an "or" operation, if the first part of the statement is true, then the whole statement is true. So bash won't execute the second part of the statement - the "exit" command. Instead, it will continue down the script.

However, if the first part of the statement is false, bash will execute the second part - the "exit".

So using || is the same as saying "if the condition is false, then exit".

But in an "and" operation, both parts must be true. So if the first part is true, bash will also execute the second part - the "exit".

But if the first part is false, bash will ignore the second part and continue down the script.

So using && is the same as saying "if the condition is true, then exit".

nooby
Posts: 10369
Joined: Sun 29 Jun 2008, 19:05
Location: SwedenEurope

#4 Post by nooby »

Thanks! I love such things. I started maybe earlier than 1983 not sure when
me trying to learn Assembler programming and Forth and Pascal and C
and C++ but sadly my brain fail such things. I am an absolute noob
when it comes to being logical. I always fail at it.
But I love that people care about it. What would Linux be without them?
I use Google Search on Puppy Forum
not an ideal solution though

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#5 Post by Karl Godt »

Yes , this can be written i more than a half dozen variants :

-ne :means: not equal
!= :means: not same

so

some variants :

ddcprobe
RETURN_VALUE_OF _DDCPROBE=$?
RV_DDC=$RETURN_VALUE_OF _DDCPROBE # to shorten things

if [ ! "$RV_DDC" = '0' ];then exit $RV_DDC;fi

if [ "$RV_DDC" != "0" ];then exit 999;fi

if [[ $RV_DDC -lt 0 -o $RV_DDC -gt 0 ]];then exit $SOME_EXITCODE;fi


#RV_DDC='' #set as required
[ "$RV_DDC" ] || { echo 'What, ddcprobe gave no exitcode ?';read -n1 -p "Shall i keep going [Y/n]?" GO_KEY;echo;echo $GO_KEY;[ "$GO_KEY" ] || GO_KEY=n;[ "$GO_KEY" = 'Y' ] || { [ "$GO_KEY" = 'y' ] || exit $COWARD; }; }

[[ $RV_DDC = 0 ]] || exit $EXIT


*

With strings there are more variants :

RV_DDC=''
[ "$RV_DDC" ] || echo empty

RV_DDC='Full of Signs'
[ "$RV_DDC" ] && echo full


if [ ! "$RV_DDC" = '' ];then echo full;fi #'' and "" means NULL and empty

if [ "$RV_DDC" != '' ];then echo full;fi

if [ "$RV_DDC" ];then echo full;fi

which means same as

if [ "`echo "$RV_DDC" | grep -E '[[:alnum:]]|[[:punct:]]'`" != "" ];then echo full;fi


To make thing easier it might be really advisable not to tes an exitcode directly, but to put it into a variable : RETVAT=$?

This can be tested to

1) contain content or not [ "$RETVAL" ]

2) to distiguish the content [ "`echo "$RETVAL" | grep '[^0-9]'`" ] && echo "Opps, Error code suddenly human readable ?"
# RETVAL=9a
# [ "`echo "$RETVAL" | grep '[^0-9]'`" ] && echo "Opps, Error code suddenly human readable ?"
Opps, Error code suddenly human readable ?

3) You are able to add the exit code number to the exit builtin, so if another script calls a script and expects an exitcode, this would be necessary .
For exitcodes see /usr/include/asm[-generic]/errno.h and errno-base.h

Post Reply