The immediate issue - likely the one triggering the syntax error - is:
You're using shebang #!/bin/sh, which is NOT guaranteed to be bash (and even if it is, it behaves differently), yet you're using bash-specific syntax ([[ ... ]]).
- Use
#!/bin/bash as the shebang instead.
- When explicitly starting a script with an executable, use
bash, not sh; in your example: bash /usr/local/src/backupcheck.sh
Alternatively, with the shebang as is and if you want to be able to invoke your script explicitly with sh:
- Rewrite your script to use POSIX features only, which in the case at hand requires replacing
[[ ... ]] with [ ... ] (but in the general case typically requires more changes).
You can only get away without rewriting if you know for sure that sh is actually bash on your system and you do not need the script to be portable (run on other platforms).
However, there are also issues with your conditionals:
It looks like your first 2 conditionals are meant to only test whether the enclosed command succeeds or not.
Generally, you do not need [[ ... ]] for such tests at all, and instead simply use the command directly with if, possibly negated with !, and with output suppressed as needed with >/dev/null or 2>/dev/null:
Thus, instead of your command:
if [[ "$( grep $BACKUP_DRIVE /etc/fstab | awk '{print $2}')" -ne "0" ]]; then
you should use:
if grep $BACKUP_DRIVE /etc/fstab >/dev/null; then
grep will indicate having found (at least) a match with exit code 0, which is (also) success from if's perspective. Thus, effectively, the if command's body will be executed if the backup drive exists.
Note that I've removed the awk command, as it (a) isn't necessary for the test and (b) actually defeats the test in that it will cause the overall result to be 0 (success), even if the grep command failed.
Similarly, your 2nd conditional should read:
if df -h | grep $BACKUP_DRIVE >/dev/null; then
Finally, your 3rd conditional is correct in principle: it captures stdout output from the pipeline and compares it to a percentage number (though double-quoting the number is not necessary and potentially confusing).
However, you accidentally hard-coded the drive name, so instead it should be:
if [[ "$(df -h | grep $BACKUP_DRIVE | awk '{print $5}' | cut -d "%" -f1)" -ge 95 ]]; then
Finally:
- You should output your error messages to
stderr by redirecting the echo commands with 2>/dev/null; similarly, you should use exit 1 (or any other nonzero exit code) to exit in case of error, so as to properly signal an error condition.
- As @Charles Duffy points out in comments on the OP, there is potential for making your commands more efficient.
grep | awk,df | awk,... | awk | cut, and similar usage in here is inefficient, hard-to-read, and generally silly. Using the shell'sreadbuiltin to parse these contents a whole line at a time would be considerably simpler and more readable.[[ ]]with-geis silly as well -- any shell supporting[[ ]]will also support(( )), in which the far-more-readable>=can be used for numeric comparisons.