Jump to content



Photo

Bash script: what's wrong with if condition?

bash script

  • Please log in to reply
3 replies to this topic

#1 zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 29 July 2012 - 19:34

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



#2 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 29 July 2012 - 23:26

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:
  • 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
    
  • 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!
    
  • Unless you're planning to use a variable outside your script (which isn't recommended), you shouldn't export your variables.

Hope this helps :)

#3 OP zoheb

zoheb

    Neowinian

  • Joined: 02-January 10
  • Location: haLLuNicaTeD pAradISe

Posted 30 July 2012 - 02:10

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

#4 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 30 July 2012 - 07:28

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



Click here to login or here to register to remove this ad, it's free!