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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Apweleleh
  • 23
  • 1
  • 4
  • 4
    ... Because the cases are supposed to be globs. – Ignacio Vazquez-Abrams Apr 03 '17 at 05:35
  • 1
    Paste your script in http://www.shellcheck.net/ to fix the syntax errors! – Inian Apr 03 '17 at 06: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! – linuxfan says Reinstate Monica Apr 03 '17 at 06:01
  • Aside: The ABS is a very problematic resource (often outdated, showcasing bad practices in examples, etc). Consider http://wiki.bash-hackers.org/syntax/ccmd/case and http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression instead. The Wooledge wiki is also reliably well-maintained; see for instance [BashFAQ #31](http://mywiki.wooledge.org/BashFAQ/031) on `[[`. – Charles Duffy Apr 03 '17 at 23:41
  • Thanks for the advices. I put the code through shellcheck and also changed my `[[ $REPLY ...]])` to`Y|y)`. Working great now. – Apweleleh Apr 04 '17 at 00:19
  • `http://linuxcommand.org/wss0120.php` is no longer available. Is there any mirror or copy for this page? –  Feb 06 '20 at 12:17
  • @Sabrina I don't know of any mirrors but the page is still available on archive.org (I tried the snapshot from the 8th of July 2017 which worked fine) : [here](https://web.archive.org/web/20170708090643/http://linuxcommand.org/wss0120.php) – Apweleleh Feb 10 '20 at 13:33

1 Answers1

1

Incorrect:

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

Correct:

[Yy])

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

agc
  • 7,973
  • 2
  • 29
  • 50