From 362e3ed060e5d408abcfed0eca3941e0cd9335d5 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Tue, 17 Dec 2013 10:55:13 +0100 Subject: [PATCH] =?UTF-8?q?Done=20with=20=C3=96vning=205=20(backup=20scrip?= =?UTF-8?q?t)=20on=20Lab=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Labb5/ovning5.sh | 59 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/Labb5/ovning5.sh b/Labb5/ovning5.sh index 9c51973..5f117fb 100755 --- a/Labb5/ovning5.sh +++ b/Labb5/ovning5.sh @@ -1,6 +1,6 @@ #!/bin/bash -#Jack-Benny Persson +# Jack-Benny Persson # LX13 # Övning 5, labb 5 # Backup-script @@ -8,14 +8,35 @@ # Set some variabels BackupTo="$HOME/backups/" declare Verbose +File="False" Tar="/bin/tar" -# Help screens -short_usage() +# Question for overwriting existing file +overwrite() { - echo "Usage: `basename $0` -b -f -o -v -h" + select Overwrite in "Yes" "No"; do + case $Overwrite in + "Yes") printf "Overwriting previous file, continuing...\n" + break + ;; + "No") printf "Not overwriting, halting script\n" + exit 0 + ;; + *) printf "Please answer 1 (yes) or 2 (no)\n" + ;; + esac + done } + +# Print short help +short_usage() +{ + printf "Usage: `basename $0` -b -f -o " + printf " -v -h\n" +} + +# Print long help long_usage() { short_usage @@ -28,7 +49,7 @@ long_usage() EOF } -# Parse arguments and options +# Parse command line arguments and options while getopts b:f:o:vh Opt; do case "$Opt" in b) BackupThis=$OPTARG @@ -40,25 +61,45 @@ while getopts b:f:o:vh Opt; do v) Verbose=v ;; h) long_usage + exit 1 ;; *) short_usage + exit 2 ;; esac done # Sanity checks if [ ! -w $BackupTo ] || [ ! -d $BackupTo ]; then - echo "Can't write to $BackupTo or $BackupTo doesn't exist" > /dev/stderr + printf "Can't write to $BackupTo or $BackupTo doesn't exist\n" \ + > /dev/stderr exit 2 fi if [ ! -r $BackupThis ]; then - echo "Can't read $BackupThis" + printf "Can't read ${BackupThis}\n" > /dev/stderr exit 2 fi -# Main (do the actual backup) +# Check if the backup file already exists +if [ "$File" != "False" ]; then + if [ -f ${BackupTo}/${File} ]; then + printf "${BackupTo}/${File} already exist, overwrite?\n" + overwrite + fi +else + if [ -f ${BackupTo}/`basename $BackupThis`.tar ]; then + printf "${BackupTo}/`basename $BackupThis`.tar already exist, " + printf "overwrite?\n" + overwrite + fi +fi -$Tar cf$Verbose ${BackupTo}/`basename $BackupThis`.tar $BackupThis +# Main (do the actual backup) +if [ "$File" != "False" ]; then + $Tar cf$Verbose ${BackupTo}/${File} $BackupThis +else + $Tar cf$Verbose ${BackupTo}/`basename $BackupThis`.tar $BackupThis +fi exit 0