0

I am writing a bash shell script for practice, but can't seem to fix its behavior. The script copies an array of files and directories into a given backup directory, $path, and compresses it to a .tar.gz file.

The iteration I'm working on should check if said .tar.gz file already exists using the naming pattern for backup directory ($date_value.tar.gz), and has specific cases depending on the answer. The script is expected to be ran from main project directory for now.

The following code is the part that goes wrong.

if [ -f "$path/$date_value.tar.gz" ] ; then

  echo "A $date_value.tar.gz directory already exists."

  read -p "Do you wish to update directory ? (Y/N)"

  echo # Moves to new line; for user experience purpose only

  case $REPLY in

        # Case when user wrote "Y" or "y" as in Yes
        " [[ $REPLY =~ ^[Yy]$ ]] " )

            # Extract archive
            tar xf "$path/$date_value.tar.gz"

            # Synchronise directories
            rsync --update -raz --progress \ 
                  --include="$current_path${files_array[@]}" "$path/$date_value" \
                  --exclude="*"

   exit 0;
;;

        # Case when user wrote anything else
        " [[ $REPLY =~ ^[*]$ ]] " )

            echo "Script didn't make any change and stopped itself."

    exit 1;
esac

# Else if backup directory doesn't exist yet
else

    # Make a directory using the date
    mkdir "$path/$date_value"

    # Loops over the whole array and copies files/directories
    # recursively to given directory with current rights
    for i in ${files_array[@]}; do
            cp -ar ${i} /home/robin/backup/by_date/"$date_value"/
    done

    # Goes to backup directory
    cd "$path"

    # Compress backup directory into tarball AND(=&&) removes it if successfull
    tar cfM "$date_value.tar.gz" "$date_value" && rm -Rf "$date_value"

fi

When run in bash -x mode, I see that it proceeds as expected until the case $REPLY in line, then suddenly stops without running through the case. I put an exit 1 as a test after the last closing fi and can confirm the script simply jumps past case and stops, since there are no further instructions.

What is going wrong in there, and why is it ditching the case?

Documentation used to write this code:

Run on Debian, with bash as terminal.

7
  • 4
    ... Because the cases are supposed to be globs. Commented Apr 3, 2017 at 5:35
  • 1
    Paste your script in shellcheck.net to fix the syntax errors! Commented Apr 3, 2017 at 6:01
  • Look at the 2nd gray box of the first link you posted, about flow control. That example compares a variable with 1, 2, 3 (digits). Subsequent examples are even more complete, and they never use your intricated [[ $REPLY ...]]. You wrote your question really well, good job, but you should read the links you posted yourself! Commented Apr 3, 2017 at 6:01
  • Aside: The ABS is a very problematic resource (often outdated, showcasing bad practices in examples, etc). Consider wiki.bash-hackers.org/syntax/ccmd/case and wiki.bash-hackers.org/syntax/ccmd/conditional_expression instead. The Wooledge wiki is also reliably well-maintained; see for instance BashFAQ #31 on [[. Commented Apr 3, 2017 at 23:41
  • Thanks for the advices. I put the code through shellcheck and also changed my [[ $REPLY ...]]) toY|y). Working great now. Commented Apr 4, 2017 at 0:19

1 Answer 1

1

Incorrect:

" [[ $REPLY =~ ^[Yy]$ ]] " )

Correct:

[Yy])

There are other errors in the OP code, but start with that...

Sign up to request clarification or add additional context in comments.

3 Comments

In the glob ^ will match a literal ^, not the beginning of line. Try Y|y)
@WilliamPursell, Fixed & thanks for the needed recorrection.
As @WilliamPursell noted, ^ matched the literal ^ and my case was still overlooked. Changing to Y|y) and *) did work however. Thank you both !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.