There are two ways to perform a “is it a weekday” logic in shell scripts. One way is to use cron
. The other is to get the day of week from the date
command.
To run a shell script every weekday via cron, set the “day of week” field to “1-5
” (0 is Sunday).
1 |
0 8 * * 1-5 /path/to/weekday-script |
The above crontab entry will run the script every weekday at 8am.
The other method is to add some logic in the script to determine if the current day is a weekday.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/sh # get day of week day=`date +%u` # reset weekday indicator flag weekday=0 # if day of week is between 1 to 5, then it's a weekday [ $day -le 5 ] && weekday=1 # exit if not weekday [ $weekday -ne 1 ] && exit 0 # enter commands to be executed on weekdays here # ... |
On Bash 4.2 and above, you can use the builtin printf
command to determine the day of the week.
1 2 3 4 |
$ echo $BASH_VERSION 4.2.25(1)-release $ (( $(printf '%(%u)T' -1) <= 5 )) && echo weekday weekday |