zoheb Posted July 29, 2012 Share Posted July 29, 2012 Hii all. .. 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_linesexport SAVEPATH="/home/zshaikh/"export FILENAME=$(basename $1)COUNT=1if [ -r $FILE ]; thentail -n $2 $1 >> ${SAVEPATH}${FILENAME}elif [ -r ${SAVEPATH}${FILENAME} ]; thentail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d)elif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d) ]; thentail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNTelif [ -r ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT ]; thenCOUNT=`expr $COUNT + 1`tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNTfi[/CODE] Link to comment Share on other sites More sharing options...
+Majesticmerc MVC Posted July 29, 2012 MVC Share Posted July 29, 2012 I don't have access to a bash interpreter at the minute, but I suspect it's breaking because you don't have a variable called "$FILE". This is causing the 'if' statement to break and just run all your tail commands. To fix this you'd normally put double-quotes round your if conditions (e.g. 'if [ -r "$FILE" ]; then ...') I think the logic in your code is wrong though. It should be this: #!/bin/bash #USE : ./script_name path_to_file no_of_lines export SAVEPATH="/home/zshaikh/" export FILENAME=$(basename $1) COUNT=1 # Check the file exists, and exit if it doesn't. if ! [ -r "$1" ]; then echo "ERROR: File '${1}' does not exist!" exit 1 fi # Check a second parameter is specified if ! [ "$2" ]; then echo "ERROR: Number of lines not specified!" fi if ! [ -e "${SAVEPATH}${FILENAME}" ]; then tail -n $2 $1 >> ${SAVEPATH}${FILENAME} echo "Written to ${SAVEPATH}${FILENAME}" elif ! [ -e "${SAVEPATH}${FILENAME}.$(date +%Y%m%d)" ]; then tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d) echo "Written to ${SAVEPATH}${FILENAME}.$(date +%Y%m%d)" else while [ -e "${PATH}${FILENAME}.$(date +%Y%m%d).${COUNT}" ]; do COUNT=`expr $COUNT + 1` done tail -n $2 $1 > ${PATH}${FILENAME}.$(date +%Y%m%d).$COUNT echo "Written to ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT" fi [/CODE] The code is untested, but should hopefully work. Since you're a newbie, I hope you don't mind some constructive criticism. A few style points:Upper case variable names are a bad (even if common) practice. lower case variable names should be used, words separated with underscores. e.g. [code] # This... some_variable=1 # ... is better than ... SOMEVARIABLE=1 Hope this helps :) Charisma and zoheb 2 Share Link to comment Share on other sites More sharing options...
zoheb Posted July 30, 2012 Author Share Posted July 30, 2012 Thank you very much . . .for all your suggestions. .. .and the script too. .. you are just great !! . . I will keep everything in mind, , Link to comment Share on other sites More sharing options...
+Majesticmerc MVC Posted July 30, 2012 MVC Share Posted July 30, 2012 You're more than welcome :). I forgot to mention in my previous post: If you're planning to export a variable, you SHOULD use upper case, not lower case :). Link to comment Share on other sites More sharing options...
Recommended Posts