Solved

If any text files exist (CSH)

Posted on 2014-02-17
5
888 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
[X]
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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cron job says it ran, no results 25 128
Choosing the right language for new project 8 66
VBA Shell can't Find Word document 11 130
Powershell Timeout - Exchange 3 94
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…

749 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