4 posts in this topic

Posted

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_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

Share this post


Link to post
Share on other sites

Posted

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

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:
  1. Upper case variable names are a bad (even if common) practice. lower case variable names should be used, words separated with underscores. e.g.
    
    # This...
    
    some_variable=1
    
    
    # ... is better than ...
    
    SOMEVARIABLE=1
    
    
  2. Unless you're using a variable as a number, variable accesses should ALWAYS be wrapped in quotes, since it prevents a great deal of errors, and makes your code safer. e.g.
    
    file_path="/home/zshaikh/filename.txt"
    
    search_term="search term"
    
    grep $search_term $file_path;    # Error: file 'term' not found!
    
    


  3. Unless you're planning to use a variable outside your script (which isn't recommended), you shouldn't export your variables.

Hope this helps :)

2 people like this

Share this post


Link to post
Share on other sites

Posted

Thank you very much . . .for all your suggestions. .. .and the script too. .. you are just great !! . . I will keep everything in mind, ,

Share this post


Link to post
Share on other sites

Posted

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 :).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.