Sometime ago someone presented a way to run a shell script on first Saturday of month and 3 days prior. The Crontab was: 03 08-17 * * 3-5 [ $(date -d "next Saturday" +\%d) -le 07 ] && /run/your/script
Seemed to work, but it doesn't. (date -d "next Saturday" +\%d) correctly returns the numeric value of next Saturday, but when testing if it's less or equal to 7 [ $(date -d "next Saturday" +\%d) -le 07 ].
When testing in bash shell, bash throws an error with the -le
Anyone can explain/point to why? and explain how to overcome?
It would be helpful if you posted the actual message text, and the shell name and version (being aware that cron runs /bin/sh by default, unless you use the SHELL=bash line in the crontab to run a specific shell).
Some shells treat numbers with leading zeros as Octal values. So comparisons with days 08 and 09 would throw an error something like 08: invalid octal number.
Formatting the date output with '+%e' or '+%_d' may solve the issue, but again this depends on your Distro and command versions. Both work on my /bin/sh (which runs dash), and on my GNU bash, version 5.1.16(1).
If you only trip the script on 3 days (Wed, Thursday, Friday), it is not going to run on four consecutive days (Saturday and the previous three days). You need another specific rule for Saturday which does not invoke "next Saturday".
Ok guys, here is more data. Shell is bash, the result from the date call is correct as next Saturday will be the 14th, but (14 the result ) it's being looked at/perceived as a function call
sry bout the markdown code tags, not obvious
So now the result from the date expression is no longer being perceived as a function, but it does not resolve to either true or false
(1) Your original script (from October 2023) can never run on a Saturday. You need a separate rule for the first Saturday in the month, because "next Saturday" will be later.
(2) Your shell at your current command line might well be Bash, but crontab does not know that. man -s 5 crontab says: "The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file." My crond runs /bin/dash, but others may run ksh88, ksh93, or plain Bourne shell.
(3) "It does not resolve to true or false". The result merely decides whether the echo runs or not. It could be echo Hello World for all the shell cares.
(4) You should use shellcheck on any script or command fragment. It checks for syntax issues more thoroughly than shell, and has better diagnostics. It is available online, and is also downloadable. It would have caught your syntax issue.
I don't have the whole range of shells to test. But your ls -ilL is incomplete.
paul: ~ $ ls -ilL /bin/sh /bin/bash /bin/dash
1184162 -rwxr-xr-x 1 root root 1396520 Mar 14 2024 /bin/bash
1179909 -rwxr-xr-x 1 root root 125688 Mar 23 2022 /bin/dash
1179909 -rwxr-xr-x 1 root root 125688 Mar 23 2022 /bin/sh
So sh and dash have the same inode, which is explained by:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Nov 16 17:08 /bin/sh -> dash
and man -s 5 crontab clearly states that crond by default will run /bin/sh, and therefore dash on my distro (which was news to me).
I don't see or recall any specific case of a test or [ command which rejects 08 or 09 as a decimal number, although I am sure I encountered the issue at some time.
However, there are constructs in both bash and dash which show an issue with numbers that exceed their base character set.
Let's get somethings straight to make the question flow:
Always post the entire and unchanged error message, not "an error" or some interpretation. That makes all readers know exactly the problem you're having.
Do you have an explicit crontab directive stating SHELL=bash? As @Paul_Pedant already pointed out, cron uses sh as default shell, unless you specifically set another one.
In fact, the shell tries to interpret (almost always) the first word as a command or shell builtin. [ is indeed a shell builtin.
type [
[ is a shell builtin
Since this part lacked the spaces @vgersh99 warned about, the first word was [14, and the shell tried to parse it as a command or builtin.
All that said, pls provide:
Your current crontab line (after all fixes you tried)
Any errors you get
Finally, pls follow hints from this answer, specially 1 and 4.
Thank you all for your valuable input, mostly to madeingermany for the original crontab entry.
To answer in order all the "which shell" questions it is bash in archlinux
My problem was with lack of understanding on how the code was parsed and hence all my difficulty. Reason I posted was to get some guidance ......
Link to markdown sheet is not working, so I may post this reply incorrectly, but if anyone else out there searching can benefit by my learning here is the final working crontab line for archlinux bash cron