Check if file exists, create new file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if file exists, create new file?
# 1  
Old 12-24-2008
Check if file exists, create new file?

Hi everyone, I'm brand new to shell scripts (and UNIX in general) and I've been able to figure everything out except this...

I want to write a script that will take a filename as an argument, append the date to it (IE Dec 24 2008) and make sure it doesn't already exist, if so, add an integer to the filename. So for example:

file0_Dec_24_2008
file1_Dec_24_2008 etc

This is what I have so far, but it keeps going into an endless loop I think because the script never finishes =\

#!/bin/bash
FILE=$1
X=$2
set $(date)
if [ ! -e "$FILE""$X"_$2_$3_$6 ]
then
touch "$FILE""$X"_$2_$3_$6
else
X=(( $X + 1 ))
bash filename1 $FILE $X
fi

so theoretically it should check to see if the file exists, and if not, create it. If it does, then it would recursively run the script again with the X increased by one. What am I missing??
# 2  
Old 12-24-2008
Try a while loop instead.

Code:
#!/bin/bash
FILE=$1
X=$2
set $(date)
CREATED=FALSE
while [[ $CREATED == FALSE ]]; do
  if ! [ -e "$FILE""$X"_$2_$3_$6 ]; then
    touch "$FILE""$X"_$2_$3_$6
    CREATED=TRUE
  else
    X=(( $X + 1 ))
  fi
done
fi

I'm I ksh guy, and I'm taking for granted that the rest of your script works.
Padow
# 3  
Old 12-24-2008
Padow, thanks so much for replying but I actually JUST figure it out shortly after I posted..

I messed up the part where I add one to the X variable, it should be this:
X=$(( X + 1 ))

The script works now! It starts with file_Dec_24_2008 and adds one to the filename from there.

The problem though is it's creating a LOT of processes...like over a hundred...is that normal?
# 4  
Old 12-24-2008
I'd take out the part where you call your own program, replace that with a loop and see if that still occurs. With the way it is written now, you will have one copy of the script running for each version of the file that already exists.
Padow
# 5  
Old 12-24-2008
Thanks again Padow I'll try that

EDIT: Using a while loop instead of that recursive script worked great, I just put it in the "else" part so that it would generate an unused filename followed by a touch to create it.

Writing scripts is fun =)

Last edited by solarnoise; 12-24-2008 at 01:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if file exists

I need to check whether a file exists and has been changed. The file should contain a specific string. The file should also have been changed within the last ten seconds. How do I do that? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

To check if file exists

Hi, I have the below code written. However I am not getting the desired output I am checking if the particular path has file in it. #!/bin/bash ls -l /IRS2/IRS2_ODI/INFILE/*LS* 1>/dev/null 2>/dev/null if then echo $? echo "File Exists" fi ... (3 Replies)
Discussion started by: Shanmugapriya D
3 Replies

3. Shell Programming and Scripting

See if file exists and if not create it

Hi, I am using an expect script to roll out an ssh key to multiple servers. I have a problem as on some of them the authorized_keys file exists and on some it doesn't. Where it exists I need to >> the key in and where it doesn't I need to create the file then sftp the file over. So I need some... (2 Replies)
Discussion started by: Grueben
2 Replies

4. Shell Programming and Scripting

how to check if a directory exists or not.if not need to create it

Hi, I am using solaris 10 OS and bash shell.just checking for small code snippet which follows below. /export/home/vomappservers/spa/common/5.0 /export/home/vomappservers/spa/common/scripts /export/home/vomappservers/spa/tools /export/home/vomappservers/spa/scm5.0/SCCS... (5 Replies)
Discussion started by: muraliinfy04
5 Replies

5. Shell Programming and Scripting

how to check to see if a file exists?

I want to write a script to see if various files exist. What I want to do is have the script search in various directories if a file exist, and if not, then output something like "/path/file does not exist". I don't actually know of how to check and see if a file exists or not. What I have in mind... (2 Replies)
Discussion started by: astropi
2 Replies

6. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

7. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

8. Shell Programming and Scripting

check a file is exists in multiple tar file?

I have list of ‘tar' format files in a folder. I want to check a particular file is exists in the tar files. Currently I am using the below command to check the scenario. tar -tvf first.tar | grep ‘myfile.txt' The command able to searched a single ‘tar' file only. But I want to search... (7 Replies)
Discussion started by: k_manimuthu
7 Replies

9. UNIX for Dummies Questions & Answers

Check if file exists, and create file

hi i have 2 questions: 1. how can i check if file exist in vi? i tryed to do: if ; then echo file exist but i get from the compiler if: Expression Syntax. 2. how can i create file in vi? when im using cat - so the script stop and wait for me to write somthing into the new file when im... (2 Replies)
Discussion started by: nirnir26
2 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question