Angus
Angus

Reputation: 517

Loop to ask user via keyboard for filename to be used or q to exit in bash

I'm having issues with a script I'm writing in bash with regards to backing up or restoring. What I'm trying to do is check for parameters and then if none are presented, loop until a name is provided or they quit. I can check for parameters and loop to quit but the problem I am having is getting the user input and then using that for the backup file name. Here's my script so far, can someone advise on how to loop for filename/q and how to get said filename input to work with the rest of the script?

#!/bin/bash
# Purpose - Backup world directory
# Author - Angus 
# Version - 1.0

FILENAME=$1.tar.gz
SRCDIR=World
DESDIR=backupfolder

if [ $# -eq 0 ]
  then
    echo "No filename detected.  To use this utility a filename is
required.  Usage:tarscript filename"
  else
    echo "The filename to be used will be $filename"
fi


while [ $# -eq 0 ]
do
  echo "Please provide a filename or press q to exit."
  read response
  if [ $response == 'q' ]
    then
      exit
    else [ $response == '$FILENAME' ]
     echo -n 'Would you like to backup or restore? (B/R)'
     read response
       if [ $response == 'B' ]
         then
            tar cvpzf $DESDIR/$FILENAME $SRCDIR
            echo 'Backup completed'
            exit
       fi
  fi
done

Upvotes: 1

Views: 68

Answers (1)

Angus
Angus

Reputation: 517

I finally managed to get it working in the end. I realised what my mistakes were thanks to Jens and changed things enough that it now responds to input and supplied parameters. Of course the code is nearly twice as big now with all my changes but hey ho.

Upvotes: 1

Related Questions