0

could someone help me to write my first script in bash, I want to write a script that works on specific files that generate daily with $date at the end of the filename and make a decision whether file size is less than a value then do something. sample:

#!/bin/bash

PATH="/home/me/myscript"
FILE="OT_*_'$DATE'.csv"
FILESIZE=$(wc -c < "$FILE")
DATE="`date +%Y-%m-%d`"

if ((FILESIZE<=201));
then
        echo "Input file is empty";
else
        echo "Input file has data";
fi
  • 1
    The part that's broken here is `FILE="OT_*_'$DATE'.csv` -- globs don't expand in quotes, _or_ on the right hand side of a regular string assignment. The problem has nothing at all to do with the `if`/`else`. – Charles Duffy Nov 01 '21 at 14:41
  • (also, you need to assign `DATE` _before_ you use it -- and should remove the single quotes; they're just data here, not syntax, and they do nothing useful unless there are actually quotes in the name of the file whose size you want to inspect) – Charles Duffy Nov 01 '21 at 14:44
  • Also, `PATH` is a reserved variable name (without it at its original value the shell won't be able to find executables such as `date` or `wc`). To avoid using reserved names by mistake, use lower-case names for your own variables; upper-case names are used for variables with meaning to the shell or operating-system-provided tools. – Charles Duffy Nov 01 '21 at 15:35

0 Answers0