Results 1 to 4 of 4
Hiii.
How are you all. .. I have started to learn bash scripting.. . and I am pretty much trying to execute this script which I am still not successful.. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 07-28-2012 #1Just Joined!
- Join Date
- Mar 2011
- Posts
- 30
Unable to execute it successfully
Hiii.
How are you all. .. I have started to learn bash scripting.. . and I am pretty much trying to execute this script which I am still not successful.. .
This is what I am trying to do. ..
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 with date / count to the new file.
Below is the script I came up with, ,,there may be many mistakes. . . . but . .through mistakes you learn
Code:#!/bin/bash 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
- 07-30-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,657
Hi,
you code is pretty close, but i'd change a few things. i've commented it to try and explain what it is doing (except for the if-then part, that should be self-explanatory).
Code:#!/bin/bash # get file from command line arguments [ $# -ne 1 ] && echo "Usage: $0 <FILENAME>" && exit 1 file=$1 # make sure file exists ! [ -f $file ] && echo "$file: No such file" && exit 1 # define output direcory SAVEPATH="/home/zshaikh/" # count number of lines in file lines=$(wc -l $file|awk '{print $1}') #echo file $file has $lines lines # today as string today=$(date +%Y%m%d) # base filename FILENAME=$(basename $file) # get a listing of files with a ".NUMBER" extension (if any) outfiles=$(ls ${SAVEPATH}${FILENAME}.$today.[0-9]* 2>/dev/null) declare -i next=1 if [ -n "$outfiles" ]; then # loop thru output files containing number extension for outfile in $outfiles; do # get just the number from the filename num=$(basename $outfile|sed -e "s|^${FILENAME}.${today}.||") # see if it is the highest number [ $num -gt $next ] && next=$num done # add one to the last number found let next+=1 ofile=${SAVEPATH}${FILENAME}.${today}.${next} elif [ -f ${SAVEPATH}${FILENAME}.${today} ]; then ofile=${SAVEPATH}${FILENAME}.${today}.1 elif [ -f ${SAVEPATH}${FILENAME} ]; then ofile=${SAVEPATH}${FILENAME}.${today} else ofile=${SAVEPATH}${FILENAME} fi # now that we know the output filename, write the number of lines to it echo "Writing count of lines ($lines) to $ofile" echo $lines > $ofileright you are!Below is the script I came up with, ,,there may be many mistakes. . . . but . .through mistakes you learn
- 08-03-2012 #3Just Joined!
- Join Date
- Mar 2011
- Posts
- 30
Thank you !! . .
Though I figured out with something different !!
Code:#!/bin/bash #USE : ./script_name path_to_file no_of_lines #Change the SAVEPATH where you need to save export SAVEPATH="/home/zshaikh/" export FILENAME=$(basename $1) COUNT=1 if ! [ -r "$1" ]; then echo "File does not exist!!" exit 1 fi if ! [ "$2" ]; then echo "Specify no of lines!!!" fi if ! [ -e "${SAVEPATH}${FILENAME}" ]; then tail -n $2 $1 > ${SAVEPATH}${FILENAME} echo "File saved at ${SAVEPATH}${FILENAME}" elif ! [ -e "${SAVEPATH}${FILENAME}.$(date +%Y%m%d)" ]; then tail -n $2 $1 > ${SAVEPATH}${FILENAME}.$(date +%Y%m%d) echo "File saved at ${SAVEPATH}${FILENAME}.$(date +%Y%m%d)" else while [ -e "${SAVEPATH}${FILENAME}.$(date +%Y%m%d).${COUNT}" ]; do COUNT=`expr $COUNT + 1` done tail -n $2 $1 > ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT echo "File saved at ${SAVEPATH}${FILENAME}.$(date +%Y%m%d).$COUNT" fi
- 08-03-2012 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,657
ah, so you were passing the number of lines in as an argument. i was wondering what $2 was.
your code looks great to me!


Reply With Quote

