[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


In a korn shell script, how would I check to see if a...

<< Back to: Unix - Frequently Asked Questions (1/7) [Frequent posting]

Question by doodles
Submitted on 6/6/2004
Related FAQ: Unix - Frequently Asked Questions (1/7) [Frequent posting]
Rating: Not yet rated Rate this question: Vote
In a korn shell script, how would I check to see if a filename exists (i.e. Jackfile) in my current directory and if so, do a set of script commands.


Answer by mfowler1
Submitted on 4/7/2005
Rating: Not yet rated Rate this answer: Vote
Here's a Korn shell script that does exactly that.

if test -r file.extension ; then
echo good;
else
echo bad;
fi;

 

Answer by noodles
Submitted on 8/18/2005
Rating: Not yet rated Rate this answer: Vote
if [ -f Jackfile]; then
    your command here
fi

 

Answer by KirkTech
Submitted on 3/12/2007
Rating: Not yet rated Rate this answer: Vote
if [ -f yourfile.txt ]
   <do something here>
else
   <do something else here>
fi


Note:  This also works with wildcards
ie. if [ -f *.txt ]

Also, spacing matters, don't leave it out!

 

Answer by ElCaito
Submitted on 6/14/2007
Rating: Not yet rated Rate this answer: Vote
use the IF ; THEN command ...

if [[ -f <file> ]] ; then
  echo "file found!"
else
  echo "file not found!"
fi

The problem for me, is how to do this with wildcards?

if [[ -f file*.txt ]] ; then
  echo "File found"
else
  echo "not found!"
fi

The above does not work. It actually tries to find a file with an asterisk as part of the file name - literally!

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Unix - Frequently Asked Questions (1/7) [Frequent posting]


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.