[comp.unix.questions] The "if

eas@turing.acs.virginia.edu (Edward A. Schwab) (06/18/91)

Help!  I'm trying to run a simple csh shell file that sets $OUT.out to an
output filename and $PROG to a program name, will search for $OUT.out, and
delete it...  I'm trying to do an "if ( -e $OUT.out ) set x=$<", which will
wait for an ENTER before continuing with the program...  The only thing is
that the "if ( -e )" statement (that searches for the existance of the file)
runs if there is an actual $OUT.out file or not...  **sigh**  How can I make
this wait for an enter keypress ONLY if $OUT.out exists???  All of the other
if -e's work (the echos and the rm...)  BTW, the actual executable is called
.$PROG; this is called up by $PROG...  :)

I hope this message makes SOME sense when you look at the actual shell script
below:
-----------------------------------------------------------------------------  
#!/bin/csh
setenv OUT for005
setenv PROG asymtc1
if ( -e $OUT.out ) clear
if ( -e $OUT.out ) echo '******************************************************'
if ( -e $OUT.out ) echo 'You have a leftover '$OUT'.out file.  Since '$PROG''
if ( -e $OUT.out ) echo 'will end up choking on this leftover file,  I am going'
if ( -e $OUT.out ) echo 'to  delete  it  for  you.  If  you  do not  want me to'
if ( -e $OUT.out ) echo 'delete it, hit ^C, ENTER, and rename it to a different'
if ( -e $OUT.out ) echo '                      filename.'
if ( -e $OUT.out ) echo '******************************************************'
if ( -e $OUT.out ) echo ' '
if ( -e $OUT.out ) echo ' If you want it deleted, press ENTER now to continue.'
if ( -e $OUT.out ) set x=$<
if ( -e $OUT.out ) rm $OUT.out
.$PROG
echo 'Now filtering...'
mv $OUT.out $OUT.asa
asa $OUT.asa >$OUT.out
rm $OUT.asa
echo ' '
echo 'Done!  File written to '$OUT.out'.'
echo ' '
-------------------------------------------------------------------------------
 
			Thanks, 
				- Ed  (eas@turing.acs.virginia.edu)
				      (eschwab@polaris.cv.nrao.edu)

jik@cats.ucsc.edu (Jonathan I. Kamens) (06/18/91)

In article <1991Jun17.195519.12713@murdoch.acc.Virginia.EDU>, eas@turing.acs.virginia.edu (Edward A. Schwab) writes:
|> Help!  I'm trying to run a simple csh shell file that sets $OUT.out to an
|> output filename and $PROG to a program name, will search for $OUT.out, and
|> delete it...  I'm trying to do an "if ( -e $OUT.out ) set x=$<", which will
|> wait for an ENTER before continuing with the program...  The only thing is
|> that the "if ( -e )" statement (that searches for the existance of the file)
|> runs if there is an actual $OUT.out file or not...

In order to realize why this is happening, you have to keep in mind exactly
what $< is.  In particular, it's a gross hack to do keyboard input using a
variable substitution.  *Variable substition* is the key here.  On a single
line "if" statement in C-shell input, variable substitution happens on the
entire line *before* the boolean of the "if" statement is checked.  Since the
$< hack is a variable substitution, the shell reads a line of input in order
to put a value in place of $< *before* it checks if the boolean is true.

The fix is simple.  Change your script to read:

if ( -e $OUT.out) then
	set x=$<
endif

In fact, a large portion of your script can be enclosed in that if statement. 
Rather than checking if $OUT.out exists before each line of the warning is
printed, you can do:

if ( -e $OUT.out ) then
	clear
	echo '******************************************************'
	echo 'You have a leftover '$OUT'.out file.  Since '$PROG''
	echo 'will end up choking on this leftover file,  I am going'
	echo 'to  delete  it  for  you.  If  you  do not  want me to'
	echo 'delete it, hit ^C, ENTER, and rename it to a different'
	echo '                      filename.'
	echo '******************************************************'
	echo ' '
	echo ' If you want it deleted, press ENTER now to continue.'
	set x=$<
	rm $OUT.out
endif

-- 
Jonathan Kamens					jik@CATS.UCSC.EDU

tchrist@convex.COM (Tom Christiansen) (06/18/91)

From the keyboard of eas@turing.acs.virginia.edu (Edward A. Schwab):
:Help!  I'm trying to run a simple csh shell file that sets $OUT.out to an
:output filename and $PROG to a program name, will search for $OUT.out, and
:delete it...  I'm trying to do an "if ( -e $OUT.out ) set x=$<", which will
:wait for an ENTER before continuing with the program...  The only thing is
:that the "if ( -e )" statement (that searches for the existance of the file)
:runs if there is an actual $OUT.out file or not...  **sigh**  How can I make
:this wait for an enter keypress ONLY if $OUT.out exists???  All of the other
:if -e's work (the echos and the rm...)  BTW, the actual executable is called
:.$PROG; this is called up by $PROG...  :)

you need to say

if (-e foo) then
    set x = "$<"
endif

so it doesn't try to eval the $< even when it doesn't need to.

:
:I hope this message makes SOME sense when you look at the actual shell script
:below:
:-----------------------------------------------------------------------------  
:#!/bin/csh

you forgot the -f.  

:setenv OUT for005
:setenv PROG asymtc1

now you have a WHOLE LOT of -e's.  why don't you just do one and make 
a block and save a lot of stat's, plus the $< headache?

there's nothing in this script demanding csh.  why don't you do yourself
a favor and use sh?  it sure beats a boot to the head, which is all
csh will ever give you.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time."  

muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) (06/19/91)

In article <1991Jun17.195519.12713@murdoch.acc.Virginia.EDU> Edward Schwab
writes:
>Help!  I'm trying to run a simple csh shell file that sets $OUT.out to an
>output filename and $PROG to a program name, will search for $OUT.out, and
>delete it...  I'm trying to do an "if ( -e $OUT.out ) set x=$<", which will
>wait for an ENTER before continuing with the program...  The only thing is
>that the "if ( -e )" statement (that searches for the existance of the file)
>runs if there is an actual $OUT.out file or not...  **sigh**  How can I make
>this wait for an enter keypress ONLY if $OUT.out exists???  All of the other
>if -e's work (the echos and the rm...)  BTW, the actual executable is called
>.$PROG; this is called up by $PROG...  :)
>
>I hope this message makes SOME sense when you look at the actual shell script
>below:
>-----------------------------------------------------------------------------  
>#!/bin/csh
>setenv OUT for005
>setenv PROG asymtc1
>if ( -e $OUT.out ) clear
>if ( -e $OUT.out ) echo '******************************************************'
>if ( -e $OUT.out ) echo 'You have a leftover '$OUT'.out file.  Since '$PROG''
>if ( -e $OUT.out ) echo 'will end up choking on this leftover file,  I am going'
>if ( -e $OUT.out ) echo 'to  delete  it  for  you.  If  you  do not  want me to'
>if ( -e $OUT.out ) echo 'delete it, hit ^C, ENTER, and rename it to a different'
>if ( -e $OUT.out ) echo '                      filename.'
>if ( -e $OUT.out ) echo '******************************************************'
>if ( -e $OUT.out ) echo ' '
>if ( -e $OUT.out ) echo ' If you want it deleted, press ENTER now to continue.'
>if ( -e $OUT.out ) set x=$<
>if ( -e $OUT.out ) rm $OUT.out
>.$PROG
>echo 'Now filtering...'
>mv $OUT.out $OUT.asa
>asa $OUT.asa >$OUT.out
>rm $OUT.asa
>echo ' '
>echo 'Done!  File written to '$OUT.out'.'
>echo ' '
>-------------------------------------------------------------------------------
> 
>			Thanks, 
>				- Ed  (eas@turing.acs.virginia.edu)
>				      (eschwab@polaris.cv.nrao.edu)


What about this?

-----------------------------------------
#!/bin/csh
setenv OUT for005
setenv PROG asymtc1
if ( -e $OUT.out ) then 
echo -n "continue [y]?"
set x=$<
if ( $x != n ) then
echo "Things you want to do here"
endif
else
echo "file does not exist, exiting shell"
endif
------------------------------------------
is this what you want?
Thanks.

- -
:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:
: Muhammad A. Muquit                 Office: 126 Graham Hall          :
: Graduate Student                   Phone: (919) 334-7737 ext 53     :
: Dept. of Civil Engineering         e-mail: muquit@garfield.ncat.edu :
: North Carolina A&T State University    or, muquit@vanity.ncat.edu   :
: Greensboro, NC 27411                                                :
:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:+:

--