Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if today is a weekend in bash?

How to check if today is a weekend using bash or even perl?

I want to prevent certain programs to run on a weekend.

like image 311
vehomzzz Avatar asked Aug 16 '10 01:08

vehomzzz


1 Answers

You can use something like:

if [[ $(date +%u) -gt 5 ]]; then echo weekend; fi 

date +%u gives you the day of the week from Monday (1) through to Sunday (7). If it's greater than 5 (Saturday is 6 and Sunday is 7), then it's the weekend.

So you could put something like this at the top of your script:

if [[ $(date +%u) -gt 5 ]]; then     echo 'Sorry, you cannot run this program on the weekend.'     exit fi 

Or the more succinct:

[[ $(date +%u) -gt 5 ]] && { echo "Weekend, not running"; exit; } 

To check if it's a weekday, use the opposite sense (< 6 rather than > 5):

$(date +%u) -lt 6 
like image 65
paxdiablo Avatar answered Sep 28 '22 00:09

paxdiablo