Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF-THEN-ELSE does not work!

Status
Not open for further replies.

morenoaebi

Technical User
Feb 17, 2003
2
CH
Why following if-then-else statement inj my csh doesen't work?

if(-e *.inf||-e *.gbr) then
rm *.inf *.gbr
endif



The following works:

if(-e a.inf||b.gbr) then
rm *.inf *.gbr
endif

thanx to all
 
Maybe you can't test for the existence of wildcards?

How about:

[tt]if { ls *.inf *.gbr >& /dev/null } then
rm *.inf *.gbr
endif[/tt]

(C shell newbie here) Annihilannic.
 
Thank you. It is a good idea. But it also doesen't work.

For information I work with solaris 1.1.1b.
Pherhaps it has a bug?

I can't upgrade to a higher solaris because I have a Software which does only work till solaris 1.1.1b.

Thank you. Bye.
Moreno
 
I tried this syntax and it worked without any issues...

#!/bin/csh

if ( -e *.inf || -e *.gbr ) then
rm *.inf *.gbr
endif

Then I modified the statement to look exactly like yours and it also worked without any problems. How about setting this for verbose output so we can see where it is failing:

#!/bin/csh -xv

if ( -e *.inf || -e *.gbr ) then
rm *.inf *.gbr
endif

This should display what the script is doing and the exact failure point. I would be interested to find out what the issue is.
 
Thank you.

The Message is:
*.inf: Ambiguous


Bye Moreno
 
When I ran my script, it was just with two test files, one named some.inf and hello.gbr...this ambiguous message would indicate that more than one reference is being found in the directory. I believe the if statement can only test for one file at a time. Perhaps you would be better off throwing this into a foreach loop since you just want to delete these anyway.

foreach FILE (*.inf *.gbr)
rm -f $FILE
end

That should resolve the issue.
 
My solution works for me for none, one or some of each file on the oldest version of Solaris I have access to (2.5.1). Are you sure you copied it correctly and used the squiggly brackets? Annihilannic.
 
Here my whole script:



#!/bin/csh -xv

if(-e *.inf||-e *.hcp||-e *.info||-e *.hcpy||-e *.gbr||-e *.drl)then

echo ----------------------------------------------------------------------------
echo Files will be copied into Folder $1 on server fileserv1
echo ----------------------------------------------------------------------------

if(-e tsf.hcpy||-e tsd.hcpy||-e ncdrill.info) then
echo ----------------------------------------------------------------------------
echo Files with extensions with four characters will be renamed into Files with three characters.
echo ----------------------------------------------------------------------------
if(-e tsf.hcpy) then
mv tsf.hcpy tsf.hcp
echo tsf.hcpy renamed into tsf.hcp
endif

if(-e tsd.hcpy) then
mv tsd.hcpy tsd.hcp
echo tsd.hcpy renamed into tsd.hcp
endif

if(-e ncdrill.info) then
mv ncdrill.info ncdrill.inf
echo ncdrill.info renamed into ncdrill.inf
endif
else
endif

if( -d /net/fileserv1/leiterplatten/gerberdaten/$1) then
echo ----------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1
echo ----------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be copied into folder $1
echo on server fileserv1.
cp *.inf *.drl *.hcp *.gbr /net/fileserv1/leiterplatten/gerberdaten/$1
echo ----------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1/*
echo ----------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be erased.
rm *.inf *.drl *.hcp *.gbr
echo ----------------------------------------------------------------------------
echo Finished to copy files.
echo ----------------------------------------------------------------------------
else
echo ----------------------------------------------------------------------------
echo Folder $1 will be created on server fileserv1.
mkdir /net/fileserv1/leiterplatten/gerberdaten/$1
echo ----------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1
echo ----------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be copied into folder $1
echo on server fileserv1.
cp *.inf *.drl *.hcp *.gbr /net/fileserv1/leiterplatten/gerberdaten/$1
echo ----------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1/*
echo ----------------------------------------------------------------------------
echo Files with extensions.inf .drl .hcp and .gbr will be erased.
rm *.inf *.drl *.hcp *.gbr
echo ----------------------------------------------------------------------------
echo Finished to copy files.
echo ----------------------------------------------------------------------------
endif
else
echo ----------------------------------------------------------------------------
echo No files to copy
echo ----------------------------------------------------------------------------
endif

this doesn't work!
 
Annihilannic,

I tried your example with the braces on the command line and this did not work out to well, it simply dropped me back to the command prompt without waiting for more input, but when I put this into a test script, it worked without any issues. Good catch. Thanks.
 
I tryed to make the script as following:


#!/bin/csh -xv

ls *.inf *.hcp *.info *.hcpy *.gbr *.drl >& dir.lst

if(! -z dir.lst) then
echo no files to copy
else
echo ---------------------------------------------------------------------------
echo Files will be copied into folder /net/fileserv1/leiterplatten/gerberdaten/$1
echo ---------------------------------------------------------------------------

if(-e tsf.hcpy||-e tsd.hcpy||-e ncdrill.info) then
echo ---------------------------------------------------------------------------
echo Files with extensions with four characters will be renamed into Files with three characters.
echo ---------------------------------------------------------------------------

if(-e tsf.hcpy) then
mv tsf.hcpy tsf.hcp
echo tsf.hcpy renamed to tsf.hcp
endif

if(-e tsd.hcpy) then
mv tsd.hcpy tsd.hcp
echo tsd.hcpy renamed to tsd.hcp
endif

if(-e ncdrill.info) then
mv ncdrill.info ncdrill.inf
echo ncdrill.info renamed to ncdrill.inf
endif
else
echo no files to rename
endif

if(-d /net/fileserv1/leiterplatten/gerberdaten/$1) then
echo ---------------------------------------------------------------------------
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1
echo Files with extensions .inf .drl .hcp and .gbr will be copied into folder $1
echo on server fileserv1.
cp *.inf *.drl *.hcp *.gbr /net/fileserv1/leiterplatten/gerberdaten/$1
echo ---------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1/*
echo ---------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be erased.
rm *.inf *.drl *.hcp *.gbr
echo ---------------------------------------------------------------------------
echo Finished to copy files.
echo ---------------------------------------------------------------------------
else
echo ---------------------------------------------------------------------------
echo Verzeichnis $1 wird auf Fileserv1 erstellt.
mkdir /net/fileserv1/leiterplatten/gerberdaten/$1
echo ---------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1
echo ---------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be copied into folder $1
echo on server fileserv1.
cp *.inf *.drl *.hcp *.gbr /net/fileserv1/leiterplatten/gerberdaten/$1
echo ---------------------------------------------------------------------------
echo
chmod 777 /net/fileserv1/leiterplatten/gerberdaten/$1/*
echo ---------------------------------------------------------------------------
echo Files with extensions .inf .drl .hcp and .gbr will be erased.
rm *.inf *.drl *.hcp *.gbr
echo ---------------------------------------------------------------------------
echo Finished to copy files.
echo ---------------------------------------------------------------------------
endif
rm dir.lst
endif



The sixt if-then line is working but it doesen't when the file dir.lst is empty.
This script works till the sixt if-then line!


If file dir.lst is empty then the text "no files to copy
" should be displayed and the script schould be finished but it doesn't!
:-(
 
:-( About my second script:

The first if-then-else does not work.
Why??

Thank you for your answers.

Bye Moreno
 
Probably because ">&" redirects standard error to the file as well. If the files don't exist you'll probably get the string "No match" in that file, so it isn't zero characters long. Annihilannic.
 
I have a Solution for my problem now!
I use "goto"! Now my script is working.
:)

#!/bin/csh

set m_path = "/net/fileserv1/leiterplatten/gerberdaten/$1"

(ls *.inf *.hcp *.info *.hcpy *.gbr *.drl > dir.lst) >& error.lst

start:
if(! -z error.lst) goto nofile

if(-e tsf.hcpy||-e tsd.hcpy||-e ncdrill.info) then
echo ----------------------------------------------
echo Files with four char extension will be renamed
echo ----------------------------------------------
if(-e tsf.hcpy) then
mv tsf.hcpy tsf.hcp
echo tsf.hcpy renamed to tsf.hcp
endif

if(-e tsd.hcpy) then
mv tsd.hcpy tsd.hcp
echo tsd.hcpy renamed to tsd.hcp
endif

if(-e ncdrill.info) then
mv ncdrill.info ncdrill.inf
echo ncdrill.info renamed to ncdrill.inf
endif
endif
echo ----------------------------------------------
if(-d $m_path) then
echo Folder $m_path exist
chmod 777 $m_path
echo Files will be copied to Folder $m_path
cp *.inf *.drl *.hcp *.gbr $m_path
chmod 777 $m_path/*
echo Files in current Folder will be erased
rm *.inf *.drl *.hcp *.gbr
else
echo Folder $m_path not exist
echo Folder will be created
mkdir $m_path
chmod 777 $m_path
echo Files will be copied to Folder $m_path
cp *.inf *.drl *.hcp *.gbr $m_path
chmod 777 $m_path/*
echo Files in current Folder will be erased
rm *.inf *.drl *.hcp *.gbr
endif
echo ----------------------------------------------
goto ende

nofile:
echo ----------------------------------------------
echo no files to copy
echo ----------------------------------------------
goto ende

ende:
rm dir.lst
rm error.lst


Thanks to all and bye
Moreno
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top