Implemented options and arguments to Övning 5
This commit is contained in:
parent
04b076b790
commit
df6cf31242
@ -7,27 +7,58 @@
|
|||||||
|
|
||||||
# Set some variabels
|
# Set some variabels
|
||||||
BackupTo="$HOME/backups/"
|
BackupTo="$HOME/backups/"
|
||||||
|
declare Verbose
|
||||||
# Set our binarys
|
|
||||||
Tar="/bin/tar"
|
Tar="/bin/tar"
|
||||||
|
|
||||||
|
# Help screens
|
||||||
|
short_usage()
|
||||||
|
{
|
||||||
|
echo "Usage: `basename $0` -b <dir> -f <filename.tar> -o <dir> -v -h"
|
||||||
|
}
|
||||||
|
|
||||||
|
long_usage()
|
||||||
|
{
|
||||||
|
short_usage
|
||||||
|
cat <<- EOF
|
||||||
|
-b Directory to make a backup of
|
||||||
|
-f Optional tar output filename (default is the name of the directory)
|
||||||
|
-o Optional directory to put the backup in (default is \$HOME/backups)
|
||||||
|
-v Turn on tar -v (Verbose)
|
||||||
|
-h This help
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse arguments and options
|
||||||
|
while getopts b:f:o:vh Opt; do
|
||||||
|
case "$Opt" in
|
||||||
|
b) BackupThis=$OPTARG
|
||||||
|
;;
|
||||||
|
f) File=$OPTARG
|
||||||
|
;;
|
||||||
|
o) BackupTo=$OPTARG
|
||||||
|
;;
|
||||||
|
v) Verbose=v
|
||||||
|
;;
|
||||||
|
h) long_usage
|
||||||
|
;;
|
||||||
|
*) short_usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
# Sanity checks
|
# Sanity checks
|
||||||
if [ $# -ne 1 ]; then
|
|
||||||
echo "Usage: $0 <directory>"
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ! -w $BackupTo ] || [ ! -d $BackupTo ]; then
|
if [ ! -w $BackupTo ] || [ ! -d $BackupTo ]; then
|
||||||
echo "Can't write to $BackupTo or doesn't exist" > /dev/stderr
|
echo "Can't write to $BackupTo or $BackupTo doesn't exist" > /dev/stderr
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -r "$1" ]; then
|
if [ ! -r $BackupThis ]; then
|
||||||
echo "Can't read $1"
|
echo "Can't read $BackupThis"
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Main
|
# Main (do the actual backup)
|
||||||
$Tar cvf ${BackupTo}`basename $1`.tar $1
|
|
||||||
|
$Tar cf$Verbose ${BackupTo}/`basename $BackupThis`.tar $BackupThis
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
2
Labb5/ovning5_todo.txt
Normal file
2
Labb5/ovning5_todo.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[ ] Check if backup.tar already exist (we don't want to overwrite it)
|
||||||
|
[ ]
|
Loading…
x
Reference in New Issue
Block a user