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??
#!/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.
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.
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.