Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1676
  • Last Modified:

If any text files exist (CSH)

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
stakor
Asked:
stakor
  • 3
1 Solution
 
ozoCommented:
cat *.txt 2>/dev/null ||  echo "No Text Files"
0
 
Peter KwanCommented:
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
 
ozoCommented:
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
 
stakorAuthor Commented:
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
 
ozoCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now