[comp.unix.shell] Accessing a "c" program value from a csh script

wes@uh.msc.umn.edu (Wes Barris) (09/21/90)

I have a "c" program that returns an integer value.  How does one run this
program from within a csh script and access the returned value?  I have
tried various forms of the following lines with no luck.

#!/bin/csh
#
#  NOTE: WF_DIR is an environment variable.
#
set intro_data = "data"
@ nobjs = `"$WF_DIR/lbin/read_intro $intro_data"`




When run, this script yields the following error message on a Sun4-280:

/usr/local/wave/lbin/read_intro data: Command not found.

If I change the csh line to the following, it runs properly but does not
(of course) return the integer value.

$WF_DIR/lbin/read_intro $intro_data

Does anyone know what's the deal?


      o o o o o o o . . .   ________________________________ _____=======_____
    o      _____            |Wes Barris                    | | wes@msc.edu   |
  .][__n_n_|DD[  ====_____  |Minnesota Supercomputer Center| |(612) 626-1854 |
 >(________|__|_[_________]_|University of Minnesota_______|_|_FAX:_624-6550_|_
 _/oo OOOOO oo`  ooo   ooo  'o^o^o                    o^o^o` 'o^o         o^o`
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
God helps them that help themselves. -- Benjamin Franklin

ables@lot.ACA.MCC.COM (King Ables) (09/22/90)

From article <2629@uc.msc.umn.edu>, by wes@uh.msc.umn.edu (Wes Barris):
> I have a "c" program that returns an integer value.  How does one run this
> program from within a csh script and access the returned value?  I have

You have two problems.

> set intro_data = "data"
> @ nobjs = `"$WF_DIR/lbin/read_intro $intro_data"`
> 
> When run, this script yields the following error message on a Sun4-280:
> 
> /usr/local/wave/lbin/read_intro data: Command not found.

Problem number 1:  If you put the command AND the arguments inside
of quotes, then the shell that gets started looks at it as a single token
(so it's looking for a command containing a space, which of course, it
doesn't find).  You don't have to quote the command inside of the grave
accents (`).

Problem number 2: Grave accents isn't what you want anyway.  That will
store the OUTPUT of the command in the symbol nobjs, not the exit status.
The exit status can be found in the $status symbol.

So if your program doesn't generate any output that would confuse the
user running the script, you should do something like this:

	set intro_data = "data"
	$WF_DIR/lbin/read_intro $intro_data
	set nobjs = $status

THEN nobjs will contain the return code.  Take care that you use $status
*right after* you run the command.  The value changes when the next command
is executed.  $status always contains the exit status of the most recently
executed command.
-----------------------------------------------------------------------------
King Ables                    Micro Electronics and Computer Technology Corp.
ables@mcc.com                 3500 W. Balcones Center Drive
+1 512 338 3749               Austin, TX  78759
-----------------------------------------------------------------------------
We don't inherit the Earth from our parents, we borrow it from our children.

jfrancis@umaxc.weeg.uiowa.edu (Jeff Francis,Home Number,,3540765) (09/22/90)

From article <2629@uc.msc.umn.edu>, by wes@uh.msc.umn.edu (Wes Barris):
> I have a "c" program that returns an integer value.  How does one run this
> program from within a csh script and access the returned value?  I have
> tried various forms of the following lines with no luck.

The value returned from any program is "remembered" in $status.

-jpf

-------------------------------------
Jeff Francis
THINK Class Library Evangelist and
CS Student, University of Iowa
jfrancis@umaxc.weeg.uiowa.edu
-------------------------------------

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (09/22/90)

  I'm not sure if you mean "returns an integer value" as status (in
$status) or prints it out, in which case you run the program in `` to
get the value.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
    VMS is a text-only adventure game. If you win you can use unix.