Solved

If any text files exist (CSH)

Posted on 2014-02-17
5
580 Views
Last Modified: 2014-02-17
In CSH, I am trying to write an if statement in csh, that will only happen if text files exist. It would look something like this:

if (-e *.txt) then
   cat *.txt
else
   echo "No Text Files"
endif

This will work for a specific text file, but not a wild card. Any ideas on how to use the -e for any file that ends in ".txt"?
0
Comment
Question by:stakor
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39866607
cat *.txt 2>/dev/null ||  echo "No Text Files"
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
ID: 39866618
How about the following:

if ( `find . -maxdepth 1 -type f -name "*txt" | wc -l` > 0 ) then
        cat *.txt
else
        echo "No Text Files"
endif

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39866629
Sorry, that was bash, this is csh
set nonomatch x=(*.txt)
if [ -e ${x[1]} ]  ; then
        cat *.txt
else
        echo "No Text Files"
endif

Open in new window

0
 

Author Comment

by:stakor
ID: 39866663
I like the simplicity of and straighforwardness of Ozo's:
cat *.txt 2>/dev/null ||  echo "No Text Files"

But inside of csh, it was still showing an error when there were no files present. I think I remember being told that csh does not split off standard error, so it ends up being an all or nothing output. Which stinks, but that's csh. Pkwan's solution was the first working solution in CSH.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39866691
Oops, still remnants of bash.
This should be plain csh
set nonomatch x=(*.txt)
if ( -e $x[1] ) then
        cat *.txt
else
        echo "No Text Files"
endif

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now