简体   繁体   中英

File monitoring Bash script

I created this script for check whether specific files exist or not in the given location. but when I run this its always showing

Failed - Flag_lms_device_info_20160628.txt do not exist
Failed - Flag_lms_weekly_usage_info_20160628 do not exist

but both files are existing.

 PREFIX="/opt/data"
 REPORT="/tmp/report.txt"
 DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )

    rm -f "$REPORT"

                FILENAME="Flag_lms_device_info_${DATE}.txt"
                FULLFN="$PREFIX/$FILENAME"
                if [ -f "$FULLFN" ]; then
                        echo "OK - $FILENAME exists" >> $REPORT
                else
                        echo "Failed - $FILENAME do not exist" >> $REPORT
                fi

                FILENAME="Flag_lms_weekly_usage_info_${DATE}.txt"
                FULLFN="$PREFIX/$FILENAME"
                if [ -f "$FULLFN" ]; then
                        echo "OK - $FILENAME exists" >> $REPORT
                else
                        echo "Failed - $FILENAME do not exist" >> $REPORT
                fi
if [ -f "find "$FULLFN" -type f -name "$FILENAME"" ];then

Here you check for existance of a strange file named find ... Use backquotes

if [ -f `find "$FULLFN" -type f -name "$FILENAME"` ];then

or, in bash ,

if [ -f $(find "$FULLFN" -type f -name "$FILENAME") ];then

to get the command's output as a string.

Furthermore, your find invocation does not look promising. If you need to find a file named Flag_lms_device and so forth somewhere under /opt/data/ , use find "$PREFIX" -type f -name "$FILENAME" . If you know for sure that /opt/data is the exact location, then use

if [ -f "$FULLFN" ]

and you don't need to find the file.

First of all, you have strange output in your question: your second line of output lacks a .txt extension. This might be an accident but if it's not it's worth investigating.

Assuming your date command is working correctly (I don't know that particular command), I would reduce your use of variables. In addition, I would use the -e test operator instead of -f because it's more inclusive. (If you haven't put data in the files yet, -f could return an error even if the file exists.) :

REPORT="/tmp/report.txt"
DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )

echo "" > "$REPORT" # Wipes file instead of completely removing it

            filename="/opt/data/Flag_lms_device_info_$DATE.txt"                
            if [ -e "$filename" ]; then

                    echo "OK - Flag_lms_device_info_$DATE.txt exists" >> $REPORT
            else
                    echo "Failed - Flag_lms_device_info_$DATE.txt doesn't exist" >> $REPORT
            fi


            filename="/opt/data/Flag_lms_weekly_usage_info_$DATE.txt"
            if [ -e "$filename" ]; then

                    echo "OK - Flag_lms_weekly_usage_info_$DATE.txt exists" >> $REPORT
            else
                    echo "Failed - Flag_lms_weekly_usage_info_$DATE.txt doesn't exist" >> $REPORT
            fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM