0

I am having difficulties figuring out what is wrong with my script.

#!/bin/bash
echo "Good Day $USER"

if [ -f "$1" ]
then 
    tar -cvf home-10-07-2017.tar --files-from /dev/null
    echo "You are about to back up the following files… $*"
for i in "$@"
do
    if [ -f "$i" ]
    then
        PROCEED='YES'
    else
         PROCEED='NO'
    fi  

    if [ $PROCEED='YES' ]
    then
        tar -rvf home-10-07-2017.tar "$i"
    fi
done
        tar -vzf home-10-07-2017.tar.gz
else
      echo 'You did not enter any files to backup'
fi

When I run script I get the following error

./backups.sh first.txt second.txt 
Good Day fed
You are about to back up the following files… first.txt second.txt
first.txt
second.txt
tar: You must specify one of the '-Acdtrux', '--delete' or '--test-label'      options

closed as off-topic by Wildcard, jasonwryan, Stephen Rauch, Anthon, Archemar Jul 11 '17 at 10:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Wildcard, jasonwryan, Stephen Rauch, Anthon, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.

  • [ is a command, not a special token. It needs space before and after. – Wildcard Jul 11 '17 at 2:25
  • Also see list of "shell scripting basics" questions on my profile page; there are other common mistakes present such as unquoted variables and unquoted $@. – Wildcard Jul 11 '17 at 2:26
  • Have you tried shellcheck. Please clean your code with the help of that site. – Arrow Jul 11 '17 at 2:26
  • 1
    @Arrow, or just for i do – Wildcard Jul 11 '17 at 2:35
  • 1
    tar -vzf lacks one of -Acdtrux so probably instead should be tar -tvzf ... to list the contents, assuming that's what that line is for. – thrig Jul 11 '17 at 3:08
3

The actual error from tar on the last line (which I guess was your primary question) is because you haven't specified an actual action. The f specifies that a filename follows. The v says to do something verbosely. The z says the file is compressed (and with most versions of tar is redundant).

I guess the action you wanted was t - to list the files. In which case, you'd need:

tar -tvzf home-10-07-2017.tar.gz