![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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.
|
|
|||
|
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.
|
|||
| Google UNIX.COM |