0

I'm writing a script to back up a directory by user and for a specific number of days. I'm new to bash so the fix could be very simple but I can't see it. When I run it return "syntax error near unexpected token ')'"

#
#!/bin/bash
SCRDIR = ${1? Error: No directory given}
DATE =5
echo "Backing up $SCRDIR for $DATE days"

TODAY = "$(date +%Y_%m_%d)"
FILENAME = $SCRDIR"_"$TODAY".tar"
tar -cvfz $FILENAME $SCRDIR
find $PWD -mtime +$DATE -type f -delete
echo "Done"
#

1 Answer 1

0

So much is wrong that I had to use shellcheck:

walt@bat:~(1)$ cat >foo.sh
#!/bin/bash
SCRDIR = ${1? Error: No directory given}
DATE =5
echo "Backing up $SCRDIR for $DATE days"

TODAY = "$(date +%Y_%m_%d)"
FILENAME = $SCRDIR"_"$TODAY".tar"
tar -cvfz $FILENAME $SCRDIR
find $PWD -mtime +$DATE -type f -delete
echo "Done"
walt@bat:~(0)$ shellcheck foo.sh

In foo.sh line 2:
SCRDIR = ${1? Error: No directory given}
       ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 3:
DATE =5
     ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 6:
TODAY = "$(date +%Y_%m_%d)"
      ^-- SC1068: Don't put spaces around the = in assignments.


In foo.sh line 7:
FILENAME = $SCRDIR"_"$TODAY".tar"
         ^-- SC1068: Don't put spaces around the = in assignments.
                     ^-- SC2027: The surrounding quotes actually unquote this. Remove or escape them.


In foo.sh line 8:
tar -cvfz $FILENAME $SCRDIR
          ^-- SC2086: Double quote to prevent globbing and word splitting.
                    ^-- SC2086: Double quote to prevent globbing and word splitting.


In foo.sh line 9:
find $PWD -mtime +$DATE -type f -delete
     ^-- SC2086: Double quote to prevent globbing and word splitting.
1
  • Thank for your detail answer waltinator Feb 5, 2020 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .