LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   (Bash Scripting) Another set of eyes please (http://www.linuxquestions.org/questions/programming-9/bash-scripting-another-set-of-eyes-please-304609/)

merana 03-22-2005 04:43 AM

(Bash Scripting) Another set of eyes please
 
Hi All,

I was doing some old log clean up with this script I wrote and this morning it is barfing with the following error:

me@server:/var/log/apache/oldlogs# ./renerror.sh default 2
./renerror.sh: line 49: syntax error: unexpected end of file

Script is as follows (yes I know its ugly but I don't script as much as I'd like...)

Code:

#!/bin/bash
#

# Did I get any parameters?
if if [ ! $1 ]
        then
                echo "  "
                echo "  "
                echo "  "
                echo "I need some parameters to work with!"
                echo "  command syntax:"
                echo "  "
                echo "  ./renerror.sh logname number"
                echo "  "
                echo "  "
                echo "  "               
                exit 2
fi

target_file=$1
target_number=$2
full_target=$target_file.error.log.$target_number
compressed_target=$full_target.gz
echo $full_target
echo $compressed_target

gunzip -d $compressed_target

tail -n1 $full_target | awk '{
print $2 "\t" $3 "\t" $5
month = $2
day = $3
year = substr($5,3,2)
if (month == "Jan") month="01"
if (month == "Feb") month="02"
if (month == "Mar") month="03"
if (month == "Apr") month="04"
if (month == "May") month="05"
if (month == "Jun") month="06"
if (month == "Jul") month="07"
if (month == "Aug") month="08"
if (month == "Sep") month="09"
if (month == "Oct") month="10"
if (month == "Nov") month="11"
if (month == "Dec") month="12"
if (length(day) == 1) day="0" day
print filename ".error-" year month day ".log"
}' filename=$target_file | xargs -i mv $full_target /mnt/bigd/oldlogs/{}

It's sister script (the one that works with access.log) works just fine and they are almost identical (with the exception of the field layout). I've looked at this for a bit this morning but can't find the bug. I think I've looked at the code too long and need another set of eyes... :scratch:

Your assistance appreciated!

Regards,

cate@d.o 03-22-2005 05:06 AM

line 5: double "if"

The following command help you to debug bash scripts:
Code:

bash -x script.sh

gbonvehi 03-22-2005 05:07 AM

If my memory doesn't fail you have to append a \ at the end of the line to tell bash you're continuing a command between lines, like:
Code:

tail -n1 $full_target | awk '{
print $2 "\t" $3 "\t" $5 \
month = $2 \
day = $3 \


merana 03-22-2005 05:14 AM

Thanks a bunch cate!

That was it... Definitely needed a fresh set of eyes for that one. The tip on the -x is also appreciated. Will work with that some more!

gbonvehi: Actually once the awk segment of the script is invoked, the '{ }' acts as the sort of 'global' container for any text within. Helps with the readability... :D


All times are GMT -5. The time now is 12:10 AM.