[comp.lang.c] Help! How 2 get info from C to Unix script

bact@ssc-vax.UUCP (12/03/87)

I've written a graphical file-selector (in C) which pops up on the screen, let's the user
select a file with a mouse button, and then disappears off the screen.  Is there a way
that I can call my program within a Unix script and set a variable in the Unix script
to equal the filename the user selected?  What I would like to do is:

(Unix script follows):

xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
FILE_PICKED=(call C program here, have it return filename)
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx

(where xxxxxxxxxx = other stuff)

I've tried this approach, but it doesn't work.  My only other alternative is
to write 2 different scripts, where the first script does the stuff it needs
to before getting the filename (call it script1), and another script which
does the stuff it needs to after getting the filename (call it script 2).
Then I can:  script 1; file_selector; script 2.  This is slow, cause it has
to start up script 1, then the file_selector program, and finally script 2.

Any help will be greatly appreciated...
...I'm not Archie, I'm David (using Archie's login)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~	"...You can't always get what you want..."	~
~			Rolling Stones			~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 
***********************************************************
* David Geary, Boeing Aerospace Co., Seattle, WA 	  *
* (206)773-5249.  --I disclaim everything I've ever said--*
***********************************************************

barmar@think.COM (Barry Margolin) (12/04/87)

In article <1569@ssc-vax.UUCP> bact@ssc-vax.UUCP (Archie A. Auxter) writes:
>  Is there a way
>that I can call my program within a Unix script and set a variable in the Unix script
>to equal the filename the user selected?  What I would like to do is:

>FILE_PICKED=(call C program here, have it return filename)

The way to do this is to have your program print the filenames to
stdout.  You can then pipe the output into the command line using
`command line`, e.g.

FILE_PICKED=`program args`

Note that since this redirects stdout, when your program interacts
with the user it will have to open /etc/tty directly, not use stdout.

---
Barry Margolin
Thinking Machines Corp.

barmar@think.com
seismo!think!barmar

gwyn@brl-smoke.ARPA (Doug Gwyn ) (12/04/87)

In article <1569@ssc-vax.UUCP> bact@ssc-vax.UUCP (Archie A. Auxter) writes:
>FILE_PICKED=(call C program here, have it return filename)

Change this to:
	FILE_PICKED=`(call C program here, have it return filename)`

ron@topaz.rutgers.edu (Ron Natalie) (12/04/87)

The way of making the output a command into an argument for something
in the shell is to use the accents to quote the command to run.

Suppose you have a program then that returns "foo.c" on it's standard
output:

    $ pick_prog
    foo.c

Using the accents, you can pass that as an argument to other programs:

    $ ls -l `pick_prog`
    total 1
    -rw-rw-r--  1 ron           104 Nov 26 23:00 foo.c

And likewise, you can use this to set a variable:

    $ FILE=`pick_prog`
    $ echo $FILE
    foo.c

-Ron

ljz@fxgrp.UUCP (Lloyd Zusman, Master Byte Software) (12/05/87)

In article <1569@ssc-vax.UUCP> bact@ssc-vax.UUCP (Archie A. Auxter) writes:

> ...
>Is there a way that I can call my program within a Unix script and
>set a variable in the Unix script to equal the filename the user
>selected? ...

Have the program write the file name to stdout (e.g., via printf).
Then use the backquote convention.  Assume your C program is called
"prog" ...

1) Bourne shell:

    VARIABLE=`prog`
or
    VARIABLE="`prog`"


2) C Shell using a local variable:

    set VARIABLE = `prog`
or
    set VARIABLE = "`prog`"

3) C Shell using an environment variable:

    setenv VARIABLE `prog`
or
    setenv VARIABLE "`prog`"



    


-------------------------------------------------------------------------
 Lloyd Zusman
 Master Byte Software
 Los Gatos, California
 "We take things well in hand."        UUCP:   ...!ames!fxgrp!ljz

 NOTE:  It's difficult to reach this site.  If you've sent mail to me
	and not gotten a reply, it's probably because the net couldn't
        find this site and I never got your note.  Mailing to this site
        will yield best results if you explicitly enter a UUCP path name
        (as shown above) instead of counting on your news poster or on
        your "sendmail" to handle things properly.  Any help with net
        paths to 'ames' (the only feed to my site) would be greatly
        appreciated.  Sorry for the inconvenience.

nrowe@runx.ips.oz (Nigel Rowe) (12/15/87)

In article <12702@think.UUCP> barmar@sauron.UUCP (Barry Margolin) writes:
>In article <1569@ssc-vax.UUCP> bact@ssc-vax.UUCP (Archie A. Auxter) writes:
>>  Is there a way
>>that I can call my program within a Unix script and set a variable in the Unix script
>The way to do this is to have your program print the filenames to
>stdout.  You can then pipe the output into the command line using
>`command line`, e.g.
>
>FILE_PICKED=`program args`
>
>Note that since this redirects stdout, when your program interacts
>with the user it will have to open /etc/tty directly, not use stdout.
>
There are other tricks like this, for example I use a menu driver
that outputs (to stdout) lines like

	name='value'
	othername='something else'
	... etc

and is called from (bourne) shell scripts like this

	eval `menu screen_file` || exit

which will then set numerous shell vars. The " || exit" copes with the
menu "cancel" key by causing menu to exit(1) which causes the script to
exit as well.

Rather than directly opening /dev/tty I send the user interaction to stderr,
this works just as well (as stderr is connected to the tty by default), but
allows me to do tests without having to look at the menu screen.

eg.	eval `menu screen_file 2> /dev/null` || exit

or use a log file instead of /dev/null.

-- Nigel Rowe

ACSnet: nrowe@runx.ips				CSNET:	nrowe@runx.ips.oz
ARPA:	nrowe%runx.ips.oz@seismo.css.gov	JANET:  runx.ips.oz!nrowe@ukc
UUCP:	...!{seismo,mcvax,ukc,ubc-vision}!munnari!runx.ips.oz!nrowe