0

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?

modmoto
  • 2,901
  • 7
  • 29
  • 54
  • does the script have a bash shebang ? `#!/usr/bin/env bash` or at least `#!/bin/bash` ? `(` is valid in bash, and should create an array from the command's output. – c00kiemon5ter Aug 07 '12 at 10:28
  • also it seems like you're not closing a `case` statement correctly, thus `expecting ;;`. posting your script would help. – c00kiemon5ter Aug 07 '12 at 10:29
  • the #!/bin/bash was the problem ;) All case Statements where closed correctly :/ – modmoto Aug 07 '12 at 11:45

2 Answers2

4

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.

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0

Fro readability use the $() form

size=$(du -h $backupDir --summarize)

If you want to stick to back quotes, then

size=`du -h $backupDir --summarize`
Stephane Rouberol
  • 4,286
  • 19
  • 18