The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to write a script in UNIX to find a file if another file exists mmdawg Shell Programming and Scripting 1 05-04-2008 07:40 PM
Checking the file if it exists ksmbabu Shell Programming and Scripting 3 04-02-2008 02:16 AM
Check File Exists and compare to previous day file script rbknisely Shell Programming and Scripting 3 02-07-2008 07:53 AM
checking a connection still exists? fishman2001 IP Networking 2 12-14-2007 03:45 AM
Please help me with this script meant for checking /etc/passwd if a name exists TRUEST Shell Programming and Scripting 4 03-04-2002 07:29 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 35
Checking if a file exists

How can I check if a file exists in shell script. Basically, I want to check if a file Test_msgs has been created today. If it has been then append data to it. Otherwise, create it. I have written the following but it does not work.

todaysdate=$(date +%d%m%Y)
timenow=$(date +%H%M%S)
checktestfile='Test_msgs_$todaysdate_*.txt'
testfile='Test_msgs_'$todaysdate'_'$timenow'.txt'

if [ -f checkTestfile ]; then
echo "File appended to" >> testfile
else
echo "File created" > testfile
fi
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 60
Just a silly error; you forgot a "$" before "checkTestfile" inside the -f test. You also mis-capitalized "checkTestfile"; it should be "checktestfile" to match the variable declaration above it.
Reply With Quote
  #3 (permalink)  
Old 07-23-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 533
Wink There are a few good tests for file operators

You used the -f, but there are a couple of others also that may be of interest.

if [ -r $checktestfile ] ==> to see if you can read it (may not have access)
if [ -s $checktestfile ] ==> to see if the file is non-zero or not empty

Also, in your scripting, you set a variable
Code:
testfile='Test_msgs_'$todaysdate'_'$timenow'.txt'
But you access it as in -- need the $ before variable
Code:
echo "File created" > $testfile
Reply With Quote
  #4 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 155
@gugs:

there is no need for an if condition. You may directly use >> as it will append if file exists, or will create file if it does not exist.

Code:
echo "some text" >> testfile
Reply With Quote
  #5 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 60
Quote:
Originally Posted by krishmaths View Post
@gugs:

there is no need for an if condition. You may directly use >> as it will append if file exists, or will create file if it does not exist.

Code:
echo "some text" >> testfile
True, but he wanted the first message in the file to be different based on whether it was created or appended-to. That then requires a test.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0