Solved

If any text files exist (CSH)

Posted on 2014-02-17
5
701 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 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

18 Experts available now in Live!

Get 1:1 Help Now