sudo bashfile makes syntax error
Pregunta
i have got a bash file which i want to toggle via the sudo crontab list. Problem is, that it does not work, because when i run the script with sudo, there is a syntax error message on this line:
size=(`du -h $backupDir --summarize`)
If i run the same script without, i have to type the sudo pw, but it works without any problems. I allready tried a few variations with brackets, with or without backticks, with or without spaces, etc but nothing helped. The error message is:
Syntax error: "(" unexpected (expecting ";;")
Any help?
Solución
The problem here is that you use bash-syntax, and the script (when it is executed from cron) is interpreted by /bin/sh
(that known nothing about arrays and the ()
construction.).
You must either specify bash
as an interpreter of the script using she-bang notation:
#!/bin/bash
or run the script explicitly with bash
from cron:
bash /path/to/script
or rewrite script so, that it could run without arrays.
Otros consejos
Fro readability use the $()
form
size=$(du -h $backupDir --summarize)
If you want to stick to back quotes, then
size=`du -h $backupDir --summarize`