2

I'm trying to add a new entry into crontab, the issue I have is that all other entries are written like so:

0 0 * * * [ -x /home/test/test_reports/nightly ] && /home/test/test_reports/nightly

What is the purpose of [ -x /home/test/test_reports/nightly ]?

I tried to Google it but with no luck..

SpaceCowboy
  • 543
  • 6
  • 16

1 Answers1

5

The options within [ ] can be found in man test:

-x FILE

FILE exists and execute (or search) permission is granted

So what these lines are doing is to check if the file is executable (and of course if it exists) and, if true, execute it.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Yes! It was a bit tricky :) Also, remember you can accept the answer if your problem is solved. – fedorqui Aug 27 '14 at 16:05
  • 1
    @fedorqui What about double square brackets ? I just had to put my test in a .sh because my cron failed with `[[: not found` when trying to use them directly. Is it a bash specifity, (i.e. prefixing my cron command with "bash" would have done the trick, and replaced my script's "shebang") ? – Balmipour Apr 04 '17 at 23:28
  • @Balmipour `[[` is Bash-specific, so if you are in Shell it will not work because it is not POSIX defined. See [Is [[ ]] preferable over [ ] in bash scripts?](http://stackoverflow.com/a/669486/1983854). – fedorqui Apr 05 '17 at 09:06
  • the url is broken, try https://ss64.com/bash/test.html – George Birbilis Aug 21 '20 at 18:15
  • 1
    @GeorgeBirbilis thanks! I have updated the link to the one you provided – fedorqui Aug 24 '20 at 06:28