0

How can I stop/abort a bash-script when:

  • is last Sunday in March? OR
  • is last Sunday in October?

Both should be checked here and if so the bash script should be stopped/aborted.

Thanks in advance.

EDIT: MAYBE THAT WAY

weekday=$(date +%a)
day=$(date +%d)
month=$(date +%m)

if [[ ( $month == 03 || $month == 10 ) && $weekday = "Sun" && $day > 24 ]]
then
 exit 1
fi
20
  • What do you mean by "here"? I would use the date command to find out which day and month is today. For example, date +"%a %d %m" | read weekday dayofmonth month sets the three relevant variables. After that, a little arithmetic and you are done. Mar 28 at 13:58
  • Need to check if today is is last Sunday in March OR is last Sunday in October and if so exit/stop the bash script, how this can be done?
    – Omexlu
    Mar 28 at 14:12
  • You use the if statement. It can process boolean expressions with OR. To find out whether it is the last Sunday in a month, first check if it's Sunday, then if it's less then seven days before the end of the month. To end the script, use exit. To end a different script, use kill. Mar 28 at 14:16
  • By the way, my date script in the first comment doesn't work because the variable assignment takes place in a sub-shell, but you can do weekday=$(date +%a) and so on. Oh, and read weekday dayofmonth month <<< $(date +"%a %d %m") works. And by the way, I still don't understand what you mean by "Both should be checked here". What is "here"? Serverfault? Mar 28 at 14:19
  • Do you have a full version of that if statement for this purpose, I need to check both "dates" in the same statement that is "here" 😊
    – Omexlu
    Mar 28 at 14:31
0

To have the script work in sh (so it works in cron), do:

#!/bin/sh

eval $(date +'m=%m d=%d dow=%w')

if ( [ "$m" -eq 03 ] || [ "$m" -eq 10 ] ) && [ "$dow" -eq 0 ] && [ "$d" -gt 24 ]
then
    exit 1
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy