0

I have 3 files which will come everyday with date in file name. I need to check all 3 files date if matching with today's date. and need to wait for 3 files, because all files may not come at same time. if all file arrives then only need to copy all 3 files to target directory.

below sample code is working for all files if available. but i need to make some while loop or something sleep command where my script can wait/look for all 3 files in src dir then move all together.

#!/bin/ksh
src_dir=/data/SIN/src;
tgt_dir=/data/SIN/tmp;
cd $src_dir;
RUN_DATE=`date +%Y%m%d`;

file1=file1_${RUN_DATE}.txt
file2=file2_${RUN_DATE}.txt
file3=file3_${RUN_DATE}.txt

file_count=`ls -lrt ${src_dir}/*.txt | grep $RUN_DATE | wc -l` ;

if [ "$file_count" == 3 ]
then
        echo "all 3 files are available";
        cp ${src_dir}/${file1} $tgt_dir;
        cp ${src_dir}/${file2} $tgt_dir;
        cp ${src_dir}/${file3} $tgt_dir;
    echo "files copied";
else
        echo "file missing";
fi
  • If you know all three filenames, then just test for existence of those three filenames. – Kusalananda Aug 31 '18 at 9:21
  • you could try sleep 60 && sh basename "$0" ` instead of echo "file missing" – msp9011 Aug 31 '18 at 9:26
0

You're so close, just switching your if statement with a while loop, or just adding a while loop as you suggest should do the trick for you.

#!/bin/ksh
src_dir=/data/SIN/src;
tgt_dir=/data/SIN/tmp;
cd $src_dir;
RUN_DATE=`date +%Y%m%d`;

file1=file1_${RUN_DATE}.txt
file2=file2_${RUN_DATE}.txt
file3=file3_${RUN_DATE}.txt

file_count=`ls -lrt ${src_dir}/*.txt | grep $RUN_DATE | wc -l` ;

# check every 5 seconds if the files are all there
while [ "$file_count" -ne 3 ]; do
    sleep 5
    file_count=`ls -lrt ${src_dir}/*.txt | grep $RUN_DATE | wc -l`
done

if [ "$file_count" == 3 ]
then
    echo "all 3 files are available";
    cp ${src_dir}/${file1} $tgt_dir;
    cp ${src_dir}/${file2} $tgt_dir;
    cp ${src_dir}/${file3} $tgt_dir;
    echo "files copied";
else
    echo "file missing";
fi

This would work, though it would hang should there ever be more than 3 files in that dir, or if one or more of the files never turn up. So I'd recommend adding a break to the while loop:

# check every 5 seconds if the files are all there, up to 10 times
count=0
while [ "$file_count" -ne 3 ]; do
    sleep 5
    file_count=`ls -lrt ${src_dir}/*.txt | grep $RUN_DATE | wc -l`
    found_files=$(ls -lrt ${src_dir}/*.txt | grep $RUN_DATE)
    echo -e "files found:\n\t$found_files" # print the files that have been found so you know which are missing
    ((count++))
    if [ "$count" = 10 ]; then
        break
    fi
done
  • thanks everyone.... both of you answer is correct. i was new for loop, so have idea but no proper syntax knowledge ... thanks for helping..... just need to know if i can print the file name in console which one is missing in that loop while check the names..... i have 3 files so which one is missing i need to print there... thanks in advance – goldenbutter Aug 31 '18 at 11:45
  • I've edited my answer, so that it will print the files that have been found, so you know which are missing whilst the while loop is running. – RobotJohnny Aug 31 '18 at 12:10
1

try

file_count=$(ls -lrt ${src_dir}/*.txt | grep -c $RUN_DATE)

while [ "$file_count" != 3 ]
do
    echo "file missing";
    sleep 60
    file_count=$(ls -lrt ${src_dir}/*.txt | grep -c $RUN_DATE)
else
        echo "all 3 files are available";
        cp ${src_dir}/${file1} $tgt_dir;
        cp ${src_dir}/${file2} $tgt_dir;
        cp ${src_dir}/${file3} $tgt_dir;
    echo "files copied";
    return

done

note that

  • it is a bad idea to parse ls's output, unless you sure you don't hava any funny char (space, new line ...)
  • I replaced grep | wc -l by grep -c which do the same
  • In same way I replaced back quote by $( )

Edit : which file is missing ?

replace

echo "file missing";

by

test -f ${src_dir}/${file1} || echo ${file1} missing
test -f ${src_dir}/${file2} || echo ${file2} missing
test -f ${src_dir}/${file3} || echo ${file3} missing
  • thanks for help.....both of your answer is correct. i was new for loop, so have little idea but no proper syntax knowledge ... thanks for helping..... just need to know if i can print the file name in console which one is missing in that loop while check the names..... i have 3 files so which one is missing i need to print there... thanks in advance – goldenbutter Aug 31 '18 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.