I am learning shell programming. . .newbie though. ..
This is what I need to do. . .
I need to get "n" number of lines from the specified file and store the output to the new file in some other specified directory.. .and if the file with the same name exists. .the script should create a new file following with today's date. If the later file too exists,the new file should be created with incremental count to the end of the file.
Below is the script I came up with. But the problem is, all the files are created at once. i.e
abc
abc.20120729
abc.20120729.1
abc.20120729.2
Something is wrong with the if condition. . .can anyone let me know what ?
Thanks in advance. ..
#!/bin/bash
#USE : ./script_name path_to_file no_of_lines
export SAVEPATH="/home/zshaikh/"
export FILENAME=$(basename $1)
COUNT=1
if [ -r $FILE ]; then
tail -n $2 $1 >> ${SAVEPATH}${FILENAME}
elif [ -r ${SAVEPATH}${FILENAME} ]; then
tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d)
elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d) ]; then
tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT
elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT ]; then
COUNT=`expr $COUNT + 1`
tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT
fi






