-1

I have written a script for clean Up activity based on some date condition. But I am getting an error.

#!/bin/bash
echo "Process Started"
Current_Date=`date +%Y-%m-%d`
echo "todays Date ==> $Current_Date"
fromDate=$1
toDate=$2
oldDate=`date --date="3 years ago" +%Y-%m-%d`
echo "Two Yrs Back Date ==> $oldDate"
if [ $toDate -le $oldDate ]
then
find . -type f -newermt $fromDate ! -newermt $toDate -exec truncate -s 0 {} \; && echo "truncated"
else
echo "todate should be less than three years"
fi
echo "Done"

Getting the error - line 15: syntax error: unexpected end of file Although line 15 is not there script has only 14 line. Also the bash script runs fine until the command echo "Two Yrs Back Date ==> $oldDate". After that it gives the error when the if condition starts. Just wanted to check any syntax error which I am making.

Jens
  • 69,818
  • 15
  • 125
  • 179
Sam
  • 728
  • 1
  • 9
  • 26

3 Answers3

1

You have quite a lot of expansions that need quotes:

if [ "$toDate" -le "$oldDate" ]

find . -type f -newermt "$fromDate" ! -newermt "$toDate"

Without seeing how you invoked the script, it's hard to know whether these are contributing to your problem, but they should be fixed anyway.

You may find it helps to be consistent and quote the variables for assignment, too:

fromDate="$1"
toDate="$2"

Your script also fails at line 9, as -le requires an integer - you probably meant to give date a format string such as +%s to get comparable integers.

As an aside, please don't put destructive commands such as truncate in your example code - it should be adequate to merely echo or something instead.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
0

Use this:

#!/bin/bash

echo "Process Started"
Current_Date=$(date +%Y-%m-%d)
echo "todays Date ==> $Current_Date"

fromDate=$1
toDate=$2
oldDate=$(date --date="3 years ago" +%Y-%m-%d)
echo "Two Yrs Back Date ==> $oldDate"

if [[ "$toDate" < "$oldDate" ]] || [[ "$toDate" = "$oldDate" ]]; then
    find . -type f -newermt "$fromDate" ! -newermt "$toDate" -exec truncate -s 0 {} \; && echo "truncated"
else
    echo "todate should be less than three years"
fi
echo "Done"

You can compare lexicographically with the conditional construct [[ ]] . To compare dates in bash you need to use:

[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression

Extracted from whoan answer on this post

Is clean of warnings using shellcheck tool. And don't forget to quote the vars to avoid problems!! shellcheck is showing things like this: ^-- SC2053: Quote the rhs of = in [[ ]] to prevent glob matching

Community
  • 1
  • 1
OscarAkaElvis
  • 5,384
  • 4
  • 27
  • 51
-1

The operator -le is for comparing integers, not for strings.

Try

if [[ "$toDate" < "$oldDate" ]]

for strict less-than or

if [[ "$toDate" < "$oldDate" ]] || [[ "$toDate" = "$oldDate" ]]

for less-or-equal.

(See http://www.tldp.org/LDP/abs/html/comparison-ops.html)

Bazinga
  • 39
  • 5
  • This doesn't solve the syntax error. Also, POSIX doesn't specify `<` as an operator for `[`, so you may as well use `[[ $toDate < $oldDate ]]`, because you are already relying on `bash` implementing <`. Additionally, `-o` is considered obsolete; you should use two commands joined with `||` instead: `[ ... ] || [ ... ]`. – chepner Dec 08 '16 at 12:18