Shell logic v. C logic

Shell logic v. C logic

Post by Jim Nakamu » Mon, 03 Oct 1994 08:58:05



Is it true that "0" is true under sh while false under C?

--

 
 
 

Shell logic v. C logic

Post by Harald Hanche-Ols » Tue, 04 Oct 1994 03:23:29


Jim> Is it true that "0" is true under sh while false under C?

Yes.  The reason for this choice in sh is that the designers of UNIX
felt it more important to differentiate between various ways for a
program to fail than the ways it can succeed, so 0 was assigned as the
exit code for success while nonzero exit codes are used to signal
failure.  And it makes more sense to let the exit code for success
stand for truth than falsehood in a shell program.

I don't know if there is an equally strong reason for the opposite
choice in C, except for convention (0=false, 1=true, though I have
seen books on mathematical logic using the opposite convention!)
--

  Dept of Mathematical Sciences
  The Norwegian Institute of Technology
  N-7034 Trondheim, NORWAY

 
 
 

Shell logic v. C logic

Post by Tom Christianse » Tue, 04 Oct 1994 07:22:43




:
:Jim> Is it true that "0" is true under sh while false under C?
:
:Yes.  The reason for this choice in sh is that the designers of UNIX
:felt it more important to differentiate between various ways for a
:program to fail than the ways it can succeed, so 0 was assigned as the
:exit code for success while nonzero exit codes are used to signal
:failure.  And it makes more sense to let the exit code for success
:stand for truth than falsehood in a shell program.
:
:I don't know if there is an equally strong reason for the opposite
:choice in C, except for convention (0=false, 1=true, though I have
:seen books on mathematical logic using the opposite convention!)

The question is simply one of whether you want to have many different
meaningful and interesting successful return values or whether you want to
have many failing ones.  Some systems don't reduce to this representation,
like UNIX system calls, which have to rely on errno for their multiplicity
of failure modes, since the success modes are taken.

Also, it's not technically accurate to state that "0" is true under sh.
It depends on what you're trying to do.  The built-in has to run a program.
Just saying

    sh -c 'if [ 0 ]; then echo ok; else echo nok; fi'
    sh -c 'if [ 1 ]; then echo ok; else echo nok; fi'
    sh -c 'if 0 ; then echo ok; else echo nok; fi'
    sh -c 'if 1 ; then echo ok; else echo nok; fi'

isn't very useful.

(Actually, you'll find that "0" is true in C.  It's
 0 that's false. :-)

--tom

--

"...we will have peace, when you and all your works have perished--and the
 works of your dark master to whom you would deliver us.  You are a liar,
 Saruman, and a corrupter of men's hearts."  --Theoden, from perl/src/aint.c

 
 
 

Shell logic v. C logic

Post by A.Park » Tue, 04 Oct 1994 22:59:55



>Is it true that "0" is true under sh while false under C?
>--


This  could  develop  into  a quite  long  discussion  about  the
semantics of truth. You can define true to be anything you  like.
For  instance strcmp, strncmp both return 0 when true, but  could
just  as  easily have been written to return 1.  isupper, islower
etc. all return 0 when false.

Andy.

 
 
 

Shell logic v. C logic

Post by Dan Merc » Wed, 05 Oct 1994 05:02:04



: >Is it true that "0" is true under sh while false under C?

: >--

: This  could  develop  into  a quite  long  discussion  about  the
: semantics of truth. You can define true to be anything you  like.
: For  instance strcmp, strncmp both return 0 when true, but  could
: just  as  easily have been written to return 1.  isupper, islower
: etc. all return 0 when false.

: Andy.
The comparison operations return the value of a lexical comparison of two
items as follows:

      strcmp()       Compares its arguments and returns an integer less
                     than, equal to, or greater than zero, depending on
                     whether s1 is lexicographically less than, equal to, or
                     greater than s2.

That is not the same as returning true or false.  In C,  a non-zero
value is taken as false,  since a zero value (NULL) in pointers generally
equals failure (an NULL returned by fopen or malloc,  for instance).

In the shells,  a zero value is taken for true since a zero exit condition
provides the least information and negative exit conditions do not exist.
Where there are multiple failure conditions,  testing the exit code can
discern among them.

Just to be pedantic.

--
Dan Mercer                                            ClearSystems, Inc

======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.

 
 
 

Shell logic v. C logic

Post by Liam Cril » Wed, 05 Oct 1994 19:09:00


?Is it true that "0" is true under sh while false under C?

Yes.  sh uses negative logic, while csh uses positive logic.

negative logic: 0=True, 1=False
positive logic: 0=False, 1=True

It is dangerous to assume that someone will always use +ve logic.

Boolean arithmetic should state which type of logic it uses.

        Liam.

--
8"88"""8P"48"V"8                Liam Crilly                  8"88"""8P"48"V"8


++  Performance Benchmarking Unit, Client-Server Systems, ICL, Bracknell.  ++

 
 
 

Shell logic v. C logic

Post by Spencer Mull » Wed, 05 Oct 1994 23:56:34



   ?Is it true that "0" is true under sh while false under C?

   Yes.  sh uses negative logic, while csh uses positive logic.

   negative logic: 0=True, 1=False
   positive logic: 0=False, 1=True

   It is dangerous to assume that someone will always use +ve logic.

   Boolean arithmetic should state which type of logic it uses.

           Liam.

Why was this done? What possible purpse could 2 inconsistent logics serve?

 
 
 

Shell logic v. C logic

Post by Paul Southwor » Thu, 06 Oct 1994 01:02:07





>   ?Is it true that "0" is true under sh while false under C?

>   Yes.  sh uses negative logic, while csh uses positive logic.

>Why was this done? What possible purpse could 2 inconsistent logics serve?

Hmm, maybe to make sure you're thoroughly confused?

--
Paul Southworth
CICNet Systems Support

 
 
 

Shell logic v. C logic

Post by Andy Newm » Fri, 07 Oct 1994 13:27:49



>For  instance strcmp, strncmp both return 0 when true, but  could
>just  as  easily have been written to return 1.  isupper, islower
>etc. all return 0 when false.

If we're going to get technical then...

The ISO C functions strcmp and strncmp don't return truth values.
They return comparison results, one of three values. They return
0 when the comparison shows the strings to contain equal characters.

--

"If business is war, the computer business is thermonuclear war". Ted Lewis.

 
 
 

Shell logic v. C logic

Post by Lawrence Kir » Tue, 11 Oct 1994 05:43:59



>That is not the same as returning true or false.  In C,  a non-zero
>value is taken as false,  since a zero value (NULL) in pointers generally
>equals failure (an NULL returned by fopen or malloc,  for instance).

Nearly - C doesn't strictly define truth values at all. However if you
compare the action of operators like && and || to theor boolean counterparts
then zero (or NULL pointer) corresponds to false and nonzero to true.

--
-----------------------------------------


-----------------------------------------

 
 
 

Shell logic v. C logic

Post by Andreas Schw » Wed, 12 Oct 1994 01:22:30


|>> For  instance strcmp, strncmp both return 0 when true, but  could
|>> just  as  easily have been written to return 1.  isupper, islower
|>> etc. all return 0 when false.

|> If we're going to get technical then...

If we're going to get pedantically then...

|> The ISO C functions strcmp and strncmp don't return truth values.
|> They return comparison results, one of three values.

They may return one of more than three values (actually all the range
of -128 to 127, at least), but these values are divided into three
categories (< 0, == 0, > 0).

\end{off-topic}
--
Andreas Schwab                                      "And now for something

 
 
 

Shell logic v. C logic

Post by Dale DePrie » Thu, 13 Oct 1994 01:25:57





|> >
|> >   ?Is it true that "0" is true under sh while false under C?
|> >
|> >   Yes.  sh uses negative logic, while csh uses positive logic.
|> >
|> >Why was this done? What possible purpse could 2 inconsistent logics serve?
|>
|> Hmm, maybe to make sure you're thoroughly confused?
|> Paul Southworth
|> CICNet Systems Support

Actually somebody at berkley thought it should be changed and so they changed
it.  The idea of a 0 for true is because 0 means success in unix.  Commands
return 0 generally when they are successful.  For scripts this means
that some like if command; then xxx will work when the command succeeds.
If you think of success and failure instead of true and false you will do
much better.
--
    _      _     Dale DePriest                  San Jose, California

 o/_/ (_(_X_(`                                  fax: (408) 943-0402

 
 
 

Shell logic v. C logic

Post by A.Park » Thu, 13 Oct 1994 18:43:50





>|>> For  instance strcmp, strncmp both return 0 when true, but  could
>|>> just  as  easily have been written to return 1.  isupper, islower
>|>> etc. all return 0 when false.
>|> If we're going to get technical then...
>If we're going to get pedantically then...
>|> The ISO C functions strcmp and strncmp don't return truth values.
>|> They return comparison results, one of three values.
>They may return one of more than three values (actually all the range
>of -128 to 127, at least), but these values are divided into three
>categories (< 0, == 0, > 0).

You see, I said it would develop into an extensive debate. :)

Whilst  I agree that the values returned by the  strcmp,  strncmp
routines  are not boolean truth values (as you say, look  at  the
man page) the point is that if you look at simply the statement
if (strcmp()), all you are interested in (usually) is that if the
two  strings  you are comparing are identical, you want  a  truth
result  that  you can use to determine what to do next,  and  so,
since  strcmp returns 0 if they are identical, the answer to  the
question  -  are these two strings identical? is 0  if  true  and
non-zero if false.

This of course is English semantics and not programming  language
semantics. (Just being picky :) ).

Andy.

 
 
 

Shell logic v. C logic

Post by Chris Bitme » Thu, 20 Oct 1994 19:37:18



>Whilst  I agree that the values returned by the  strcmp,  strncmp
>routines  are not boolean truth values (as you say, look  at  the
>man page) the point is that if you look at simply the statement
>if (strcmp()), all you are interested in (usually) is that if the
>two  strings  you are comparing are identical, you want  a  truth
>result  that  you can use to determine what to do next,  and  so,
>since  strcmp returns 0 if they are identical, the answer to  the
>question  -  are these two strings identical? is 0  if  true  and
>non-zero if false.

It is sometimes useful to think of strcmp as a boolean function whose
return value means "Are the strings different". When I read a line like:

if (strcmp(x, y) == 0)

I translate this to "Is the diffence between x and y equal to none".

 
 
 

1. Xconfig File for Cirrus Logic CLGD-5424

I am having terrible problems getting X to work on my machine with my
Cirrus Logic CL-GD5424 card.  I have tried using the readme.cirrus,
but all of the options that I have tried fail.

Thank you

Thomas Munn

2. Linux SNA

3. Advance Logic 2302V

4. Disk problem??

5. Avance Logic Driver needed

6. T3 Arrays

7. XF86Config for Cirrus Logic CLGD5430/5434 needed ...

8. X libraries...

9. Cirrus Logic AGP Locking Up, Help!

10. Avance Logic ALS100 & 4.0

11. Trantor T130b SCSI Card / Cirrus Logic 5428 VLB Card

12. HELP! Cirrus Logic 546X card

13. Slackware X dist and Avance Logic Server err 11