Multiple condition in IF with multiple awk statements for execution

I am trying to execute multiple AWK statements that should work for following scenarios.
a. If any one of arrives.
b. Both the files arrives.
c. If any file did not arrive.

Below is my code:

file_count1=`ls /Acknowledgement/"$cur_dt"_*_ABCD.DAT.001.dat | wc -l`
file_count2=`ls /Acknowledgement/"$cur_dt"_*_ABCD.DAT.004.dat | wc -l`

#if [ \( "$file_count1" -eq 0 \) -o \( "$file_count2" -eq 0 \) ]
if [[ "$file_count1" -eq 0 || "$file_count2" -eq 0 ]];
then
echo ""$cur_dt"_ABCD.DAT.001-004.dat Files Not Received"
else
awk -f /Acknowledgement/abcd_acp_01_04.awk "$cur_dt"_*_ABCD.DAT.001.dat > /Acknowledgement/abcd_acp_ack_001
sed '/A2 Report type contained in the batch \//d;/Genre de/d' -i /Acknowledgement/abcd_acp_ack_001

awk -f /Acknowledgement/abcd_acp_01_04.awk "$cur_dt"_*_ABCD.DAT.004.dat > /Acknowledgement/abcd_acp_ack_004
sed '/A2 Report type contained in the batch \//d;/Genre de/d' -i /Acknowledgement/abcd_acp_ack_004
fi

Note: I have tried implementing it using ELIF, but could not achieve required output, script is getting terminated.

Below is the code with ELIF:

file_count1=`ls /Acknowledgement/"$cur_dt"_*_ABCD.DAT.001.dat | wc -l`
if [ "$file_count1" -eq 0 ];
then
awk -f /Acknowledgement/ABCD_acp_01_04.awk "$cur_dt"_*_ABCD.DAT.001.dat > /Acknowledgement/abcd_acp_ack_001
sed '/A2 Report type contained in the batch \//d;/Genre de/d' -i /Acknowledgement/abcd_acp_ack_001
#fi

file_count2=`ls /ABCD_ACK/Acknowledgement/"$cur_dt"_*_ABCD.DAT.004.dat | wc -l`
elif [ "$file_count2" -eq 0 ];
then
awk -f /Acknowledgement/ABCD_acp_01_04.awk "$cur_dt"_*_ABCD.DAT.004.dat > /Acknowledgement/abcd_acp_ack_004
sed '/A2 Report type contained in the batch \//d;/Genre de/d' -i /Acknowledgement/abcd_acp_ack_004
else
echo "The 001 and  004 acknowledgement files not recieved"
fi

Kindly suggest . Thanks in advance.

Issue resolved: Instead of OR (||), I used AND (&&) .

1 Like

I edited your code sample to use the proper markdown tags - otherwise it was hard distriguish your shell's `` from the markdown inline notation.
I'd suggest using $(command) idiom instead of old shell's ``

1 Like

The shell can take multiple commands between if/elif and then

if
  file_count1=`ls /Acknowledgement/"$cur_dt"_*_ABCD.DAT.001.dat | wc -l`
  [ "$file_count1" -eq 0 ]
then
  ...
elif
  file_count2=`ls /ABCD_ACK/Acknowledgement/"$cur_dt"_*_ABCD.DAT.004.dat | wc -l`
  [ "$file_count2" -eq 0 ]
then
  ...
fi

The exit status before the then matters.
Perhaps does not make sense here, but it's good to know that this possibility exists.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.