0

I have a parameters.txt with days parameter that store different days in the month.

Days: 1,5,31

I want to create a bash script that will read the parameter "Days" and compare the days that separated with "," to current day. And if current day equal to one of those days it will echo "true".

But i'm facing some troubles: how do I read the days parameter from the bash script, and read each day and compare to current day? I succeeded to read the whole line with the

, ,

Also when I try to get current day with $d I get the days like 01 ,02 and so on. but in my parameters the days are 1 , 2 ...

2
  • Please provide an example parameters.txt file. Commented Nov 6, 2020 at 7:59
  • it will just have this: Days: 1,3, 4, 26 Commented Nov 6, 2020 at 8:15

3 Answers 3

1

If you have control over this file, a much better format would be one number per line, or perhaps a whitespace-separated line of just the numbers.

If not, you can preprocess this wicked format into something which is easier to process with shell script with a simple Awk snippet.

awk '/^Days: / {
  n = split($2, d, /,/)
  for (i=1; i<=n; ++i) print d[i] }' parameters.txt |
while read -r number; do
   date -d "$number days ago"
done
Sign up to request clarification or add additional context in comments.

Comments

0

This should do it:

#!/bin/bash

filename="parameters.txt"

while read -r line; do 
   # Look for the correct line (if you have other parameters)                                                                                                                                                                             
   if [[ $line == Days:* ]]; then 
      # Remove line prefix
      days=$(echo "$line" | cut -c 7-); 
      # Change the delimiter
      IFS=', '; 
      read -ra arr <<< "$days"
      for i in "${arr[@]}"; do
         # Check for current day
         if (( $(date +%d) ==  i )); 
            then echo "Found current day"; 
         fi; 
      done; 
   fi; 
done < $filename

Beware that changing IFS may cause you some problems if you have to make other read inside the script, in this case I advise you to change it back to its original value.

If your parameters.txt only contains the days parameter you can do

#!/bin/bash

filename="parameters.txt"
# Remove line prefix
days=$(cut "$filename" -c 7-); 
# Change the delimiter
IFS=', '; 
# etc ...

Comments

0
awk 'BEGIN { 
             split(strftime(),dat," ");
             day=dat[3]
            } 
     /^Days/ { 
              split($2,arr,",");
              for (i in arr) 
                          { 
                           if (day ==arr[i]) 
                                           { 
                                             print "True" 
                                           }  
                           } 
              }' parameters.txt    

One-liner

awk 'BEGIN {split(strftime(),dat," ");day=dat[3]} /^Days/ { split($2,arr,",");for (i in arr) { if (day ==arr[i]) { print "True" }  } }' parameters.txt

We begin by getting today's date using awk's strftime function and then further splitting this into a dat array using awk's split function and " " as the delimiter. We then set day to the 3rd index of the array. Then when we encounter a line beginning with Days, we split the section after the space ($2) into an array arr based on commas as the delimiter. We then loop through this array comparing the days with the current day and print True if there is a match

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.