Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

If any text files exist (CSH)

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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