To trigger script on particular instance

Hi Guys,

I have a main_script.sh which runs every day and scheduled in crontab.

in the main script i read data from config file

test.config
apple
mango
orange
main_script.sh
for i in `cat test.config`
do 
if [ $i == 'apple' ]
then 
echo 'Apple'
..
..
..


i Need to print based on below. for eg apple and mango should not run on sunday. Main_script will be keep running all days

apple - Monday to friday
mango - monday to saturday
orange - monday to sunday

cat test.config

apple=12345
mango=123456
orange=1234567

cat main_script.sh

#!/bin/bash

wd=$(date +%u)
for i in $(cat test.config); do
        if [[ ${i/*=} =~ $wd ]]; then
                echo ${i/=*}
        fi
done
2 Likes

If you use a while loop instead of the for, you could read the proposed changed config file into different variables and act upon those:

wd=$(date +%u) 
while IFS="=" read ACTION DAYS
  do  if [[ "$DAYS" =~ $wd ]]

      then echo "$ACTION"
          .
          .
          .
      fi
  done < test.config

You could also modify the crontab settings if the config file is not too long...

1 Like

Very nice, except that sunday would be a zero.
Alter test.config so that all instances of 7 are 0 :

apple=12345
mango=123456
orange=1234560

Also, please consider using $(< test.config) instead of $(cat test.config) . The former does the same thing without invoking cat.

Andrew

1 Like