问题
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?
回答1:
Specify your interpreter explicitly in the crontab entry. Use
bash /path/zero_check.sh
rather than
/path/zero_check.sh
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/37004196/error-syntax-error-unexpected-when-declaring-arrays-in-bash