I have a shell script to automate a git commit and push every night:
auto_git_push.sh
#!/bin/sh
function automate(){
git add .
git commit -am "automated push $(date +"%Y-%m-%d")"
git push -u
}
cd ~/htdocs
automate
If I run this command, the script works as expected: . ~/bin/auto_git_push.sh
However, with this crontab line (set to every minute for testing)
* * * * * sh /home/hookedonwinter/bin/auto_git_push.sh
I get the following error:
/home/hookedonwinter/bin/auto_git_push.sh: 3: Syntax error: "(" unexpected
What is causing this syntax error?
Thanks!
Edit based on accepted answer:
Changed the script to:
#!/bin/bash
automate() {
git add .
git commit -am "automated push $(date +"%Y-%m-%d")"
git push -u
}
cd ~/htdocs
automate
And the crontab line to:
* * * * * /bin/bash /home/hookedonwinter/bin/auto_git_push.sh
Best Answer
As John mentioned, it is a matter of your script being interpreted differently in the two environments (using
/bin/sh
undercron
, and using your existing shell, which is probably/bin/bash
when you source it in directly). Actually,/bin/sh
is usually just a symlink to/bin/bash
, and the bash executable behaves differently depending on the name under which it was invoked, but that's just an aside.Here, the easiest way to fix your issue is probably just to specify
as the command to run under cron.