You are not logged in.

#1 2009-06-08 16:58:11

murffatksig
Member
From: Atl
Registered: 2004-05-17
Posts: 362

Simple programming question

I'm examining some PKGBUILD's to hopefully learn to make my own.  And I've seen this line

make || return 1

.  I understand that's where the code is compiled according to the makefile, but why the

|| return 1

I know  return 1 just returns success, but why would you want that?  and what does the || do?


"Oh, they have the internet on computers now."

Offline

#2 2009-06-08 17:03:57

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Simple programming question

&& and || check a previous command for successful completion or failure respectively (more exactly, a zero or nonzero exit status)

i.e.

cd $HOME && rm ./*

will only do the rm if the cd completes successfully

or

make || return 1

will only return (with a 1 exit status) if the make command fails.  this is a good way to break out of a script/function/loop if something fails

Offline

#3 2009-06-08 17:23:04

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: Simple programming question

If the build function returns 1 then it is considered an error (most applications return 0 upon success and something else when there is an error).

"make || return 1" could be read as "Try to compile the code. If there is an error, exit the build function and report an error, otherwise continue.".


My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone

Offline

#4 2009-06-09 00:15:20

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: Simple programming question

You can try to understand it this way;

&& looks like the logical AND
|| looks like the logical OR

So, in that case - either do command1 with success OR do command2.

Last edited by moljac024 (2009-06-09 00:17:01)


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#5 2009-06-09 00:20:20

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Simple programming question

In the land of shell scripting, 0 is success and anything else is failure.

Also, you might find this informative: http://en.wikipedia.org/wiki/Short-circuit_evaluation

Last edited by Peasantoid (2009-06-09 00:20:37)

Offline

Board footer

Powered by FluxBB