crontab bash tests

Ralf Mardorf kde.lists at yahoo.com
Tue Sep 26 13:13:39 UTC 2023


On Tue, 2023-09-26 at 14:01 +0200, Oliver Grawert wrote:
> "grep -q <searchterm>"

Just running "grep" without an option or without redirecting stdout
would probably do the same.

• rocketmouse at archlinux ~ 
$ echo "Hello Ernie!" | grep "Bert"; echo $?
1
• rocketmouse at archlinux ~ 
$ echo "Hello Ernie!" | grep "Ernie"; echo $?
Hello Ernie!
0
• rocketmouse at archlinux ~ 
$ echo "Hello Ernie!" | grep --quiet "Ernie"; echo $?
0

If cron would run it, where is "Hello Ernie!" shown? By a log file or
nowhere? At least no window does open and display it.

Behind the following sarcasm is a serious message.

The message:

IMO it's to complicated to ensure that a one-liner is free from
mistakes.

The sarcasm:

Maybe "> /dev/null" is a placeholder for a file that is used later. When
the single line script becomes longer, it might parse the file and
interact by more broken "if then else" syntax. It's a pity that shell
scripts don't support jump commands such as the BASIC "goto", to allow
spaghetti code in a single endless unreadable line.

I suspect the pitfall of the OP's cron one-liner is the sequence of &&
and ||. I might be mistaken, since I don't know what difference the
"{ ; }" makes.

On Arch Linux using "dash -i" doesn't explain the difference that the OP
does experience when running the one-liner from command line or by cron.

   • rocketmouse at archlinux ~ 
   $ bash -i
   • rocketmouse at archlinux ~ 
   $ { [[ $(true)$? -eq 0 ]] && false || echo "Hello Kitty"; } 
   Hello Kitty
   • rocketmouse at archlinux ~ 
   $ exit
   exit
   • rocketmouse at archlinux ~ 
   $ dash -i
   $ { [[ $(true)$? -eq 0 ]] && false || echo "Hello Kitty"; }
   dash: 1: [[: not found
   Hello Kitty
   $ exit
   • rocketmouse at archlinux ~ 
   $


   • rocketmouse at archlinux ~ 
   $ bash -i
   • rocketmouse at archlinux ~ 
   $ { [[ $(true)$? -eq 0 ]] && true || echo "Hello Kitty"; }
   • rocketmouse at archlinux ~ 
   $ exit
   exit
   • rocketmouse at archlinux ~ 
   $ dash -i
   $ $ { [[ $(true)$? -eq 0 ]] && true || echo "Hello Kitty"; }
   dash: 1: Syntax error: "}" unexpected
   $ exit
   

   • rocketmouse at archlinux ~ 
   $ dash -i
   $ { [ $(true)$? -eq 0 ] && false || echo "Hello Kitty"; }
   Hello Kitty
   $ exit
   
   
   • rocketmouse at archlinux ~ 
   $ dash -i
   $ $ { [ $(true)$? -eq 0 ] && true || echo "Hello Kitty"; }
   dash: 1: Syntax error: "}" unexpected
   
Maybe I made a mistake or maybe I need to read "man dash" from Arch Linux
and/or Ubuntu" and or man cron

Regards,
Ralf



More information about the ubuntu-users mailing list