0

I'm in a process of writing a script which checks the log folders, files and tails the log contents of the file for yesterday and today.

I have written the following code to check the folder existence for yesterday and today.

#!/bin/bash
YESTERDAY=$(date +%Y/%m/%d -d yesterday)
echo $YESTERDAY
TODAY=$(date +"%Y/%m/%d")
echo $TODAY
FOLDER="Alert1 Alert2 ..."
DATE="$YESTERDAY $TODAY"
#checks the log folder existence
for D in $FOLDER, E in $DATE; do
    LOG="/usr/processing/logs/checks/$E/country/$D"
    echo $LOG
    test -d "$LOG" && echo "Located file @ $LOG"
done 

I was expecting to get output like:

2022/10/09
2022/10/10
/usr/processing/logs/checks/2022/10/09/country/Alert1                       #yesterday folder1
Located file @ /usr/processing/logs/checks/2022/10/09/country/Alert1
/usr/processing/logs/checks/2022/10/09/country/Alert2                       #yesterday folder2
Located file @ /usr/processing/logs/checks/2022/10/09/country/Alert2 
/usr/processing/logs/checks/2022/10/10/country/Alert1                       #Today folder1
Located file @ /usr/processing/logs/checks/2022/10/10/country/Alert1
/usr/processing/logs/checks/2022/10/10/country/Alert2                        #Today folder2
Located file @ /usr/processing/logs/checks/2022/10/10/country/Alert2

...but instead the actual output i got is:

2022/10/09
2022/10/10
/usr/processing/logs/checks//country/Alert1                     
/usr/processing/logs/checks//country/Alert2,
/usr/processing/logs/checks//country/E                      
/usr/processing/logs/checks//country/in 
/usr/processing/logs/checks//country/2022/10/09        
/usr/processing/logs/checks//country/2022/10/10

I'm actually trying to use two variables (i.e. $FOLDER and $DATE )in a single for loop but unfortunately the above loop with variables clearly doesn't work as expected. The variables reassigned in the for loop gives away an output which doesn't iterate expected statement/directory path with corresponding variable values. Hopefully someone can help me resolve this.

Thanks!

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Light605
  • 37
  • 4
  • You never assign a value to variable `E`. Do a `echo "D=$D ; E=$E"` inside your loop to see what's going on. – user1934428 Oct 11 '22 at 05:12
  • BTW, a cleaner solution would be to represent `FOLDER` and `DATE` as arrays. In your case, the difference does not matter, but if one day you have a folder with a space in its name, it does. – user1934428 Oct 11 '22 at 05:13
  • Your question is is in core not about bash, but about programming basics: When you have two collections and want to perform an action for every two combinations of the collections (for instance arrays), you need one inner loop nested into an outer loop. If you need help with this (since your question is already closed), I suggest that you ask it again, but tag it with _algorithm_, and make it clear that you foremost are struggling with a suitable algorithm. – user1934428 Oct 11 '22 at 05:17

0 Answers0