Continue Execution Based On List

Hi all.

I have a script like this

function check_filesize {
filesize_1="$(ls -la "$1"|awk '{ print $5 }')"
sleep 123
filesize_2="$(ls -la "$1"|awk '{ print $5 }')"
if [ $filesize_1 -eq $filesize_2 ]
then
echo "OK"
else
echo "NOT OK"
sleep 1234
check_filesize $1
fi
}

function check_TR {
chk="$(tail -1 $1|grep TR)"
if [ "$chk" != "" ]
then
recnum="$(echo $chk| sed 's/.*|//')"
else
recnum_tmp="$(wc -l "$1"|awk '{ print $1 }')"
recnum="$(echo $recnum_tmp-1|bc)"
fi
echo $recnum
}

function CMLT_Report {
script="$1"
report="$2"
procedure="$3"
file_dir="$4"
file_size="$(ls -la "$2"|awk '{ print $5 }')"
recnum="$(check_TR $2)"
created="$(ls -la "$2"|awk '{ printf "%s,%02i,%s\n" , $6, $7, $8 }')"
remarks="$5"
frequency="$6"
}

function CMLT_Report_2 {
pattern=$1
script=$2
procedure=$3
folder=$4
frequency=$5
for file in $pattern
do
   check_filesize $file
   CMLT_Report "$script" $file "$procedure" "/dbfs_direct/FS1/cmlrpt/" "$folder" "$frequency"
   sh /dbfs_direct/FS1/MIE/script/CAMELOT_REPORTING_LOG.sql $script $report $procedure $file_dir $file_size $recnum $created $remarks $frequency
done
}

CMLT_Report_2 "TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_*.csv" "10.41.54.62/dbfs_direct/FS1/script/run_custom_t.sh" "SP_RPT_FIN_ACC_CONSOL" "Camelot-Financial" "DAILY"

But I want to extend the script in such a way that only proceed if the $pattern belongs to a list (inside file named list.txt)

For example let's say the list contains

TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130414.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130415.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130416.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130417.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130418.csv

So when reading the file pattern

TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130412.csv - NOT PROCEED
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130413.csv - NOT PROCEED
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130414.csv - Proceed
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130415.csv - Proceed
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130416.csv - Proceed

So where do I start?

Thanks for helping.

Thank you.

Shooting a big piece of code and waiting for people to spend their time to reverse engineer it is not the best way to encourage people to answer.

Please take the time to explain what you have and what you are trying to achieve, even if you already provided the code. Thanks

1 Like

Thanks for your feedback.

Ok, let's make it simple..

I have one function

function CMLT_Report_2 {
pattern=$1
for file in $pattern
do
   echo $file >> pass.txt
done
}

So, to call the function

CMLT_Report_2 "TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_*.csv"

But I only want it to be executed ONLY IF the file_name belongs to this text file named list.txt containing

TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130414.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130415.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130416.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130417.csv
TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130418.csv

In other words, I don't want the function to be executed (or break the loop) if the file_name e.g. TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130412.csv or TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_20130413.csv

Thank you.

You could of course read list.txt and match that against every $file , but maybe you could try it the other way around:

while read file
do
  case $file in
    ($pattern) [ -f "$file" ] && echo "$file"
  esac
done < line.txt > pass.txt
1 Like

So you have a list of file names, a directory full of files, and the intersection of both must match a pattern. It may be a bit tedious to find out the optimum way to boil that down to the actual set of files to be worked upon. Scrutinizer has provided one example; try this as an alternative:

ls $(grep TM_ICP_EDW_FIN04-ACCOUNT_CONSOL_.*\.csv list.txt) >pass.txt 2>/dev/null 

Watch out - grep takes a regex, not a shell pattern!

1 Like

Thanks a lot Scrutinizer and Rudic.

Scrutinizer solution works as expected, thanks.

But actually the real situation is, that CMLT_Report_2 would call another function called CMLT_Report_1. In other words...

function CMLT_Report_1 {
report=$1
for file in $report
do
   echo $file >> pass.txt
done
}
function CMLT_Report_2 {
pattern=$1
for file in $pattern
do
   CMLT_Report_1 $file
done
}

Of course the original function is as posted in post #1, but the concept is like that.

Thanks for helping guys.

---------- Post updated at 03:06 PM ---------- Previous update was at 09:55 AM ----------

I think I have figured it out..

function CMLT_Report_2 {
pattern=$1
script=$2
procedure=$3
folder=$4
frequency=$5
# for file in $pattern
# do
#    check_filesize $file
#    CMLT_Report "$script" $file "$procedure" "/dbfs_direct/FS1/cmlrpt/" "$folder" "$frequency"
#    sh /dbfs_direct/FS1/MIE/script/CAMELOT_REPORTING_LOG.sql $script $report $procedure $file_dir $file_size $recnum $created $remarks $frequency
# done
while read file
do
  case $file in
    ($pattern) [ -f "$file" ] && check_filesize $file \
                              && CMLT_Report "$script" $file "$procedure" "/dbfs_direct/FS1/cmlrpt/" "$folder" "$frequency" \
                              && sh /dbfs_direct/FS1/MIE/script/CAMELOT_REPORTING_LOG.sql $script $report $procedure $file_dir $file_size $recnum $created $remarks $frequency
  esac
done < report_list.txt
}

Thanks.