4

Same problem as this OP, but must be a seperate cause.

The following script:

#!/bin/sh
arr=("cat" "dog" "bird")

Works interactively (debian) but fails when called by crontab with:

/bin/sh: 2: /path/zero_check.sh: Syntax error: "(" unexpected

I've tried with #!/bin/bash shebang, and declaring array with declare -a arr=("cat" "dog" "bird"), to no effect.

Any idea why?

geotheory
  • 22,624
  • 29
  • 119
  • 196

4 Answers4

6

The problem here is that you are using this shebang:

#!/bin/sh

Whereas arrays are something Bash specific that shell does not allow.

So to make it work, change the shebang of your script to Bash:

#!/bin/bash
fedorqui
  • 275,237
  • 103
  • 548
  • 598
5

Specify your interpreter explicitly in the crontab entry. Use

bash /path/zero_check.sh

rather than

/path/zero_check.sh
Seth Difley
  • 1,330
  • 1
  • 23
  • 33
  • 2
    this shouldn't be the issue if the script already contains the `#!/bin/bash` header. – fedorqui May 03 '16 at 12:45
  • I think I just copied the working script at the wrong point. The solution works. – geotheory May 03 '16 at 12:52
  • 1
    If this is the answer, then there's another problem in your script you are ignoring. :( – SaintHax May 03 '16 at 12:52
  • @SaintHax Agreed; the problem is the incorrect shebang line (uses `/bin/sh` instead of '/bin/bash`); overriding that by running it with `bash` really just hides the problem, it'd be better to fix the shebang (see fedorqui's answer). – Gordon Davisson May 03 '16 at 21:33
1

Just for the documentation, i had an old script to run which had an Syntax Error in the Shebang:

#/bin/bash

instead of

#!/bin/bash

Also check the Script is executable of course.

ysf
  • 4,634
  • 3
  • 27
  • 29
Jens Franik
  • 131
  • 1
  • 3
  • YES! I have been going insane the past two days because I forgot the exclamation point. This saves what little sanity I have remaining. – David Apr 29 '20 at 21:00
0

Very similar problem with incorrect bash function declarations. This works OK from the command line, but it causes cron to fail...

function test () { ... }

Cron should save the errors in /var/mail

I also recommend linting with "shellcheck" because it found another error I didn't notice.

PJ Brunet
  • 3,615
  • 40
  • 37