-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassessment2BackupScript.sh
157 lines (131 loc) · 3.17 KB
/
assessment2BackupScript.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /bin/bash
#Matthew Wilson EC1841586
#
#
#
#Loop until input is Q
while [ "$MENU_INPUT" != "Q" ]
do
#PRINT MENU
echo
echo '======================'
echo '1. Backup script file'
echo '2. Info'
echo '3. Copy'
echo '4. Move'
echo 'Q. Quit'
echo '======================'
echo 'Please input selection'
#READ INPUT
read MENU_INPUT
#ACT ON INPUT
case $MENU_INPUT in
#Create backup copy of this script
1)
#Assign variables for script backup
scriptfile='Ass2_MatthewWilson.sh'
scriptbackupfile=$scriptfile'_backup_'$(date +"%d_%m_%Y")'.sh'
#Test to see if scriptfile exists. Print error if not.
echo "sourcefile= " $scriptfile
echo "destination file= " $scriptbackupfile
if test -f $scriptfile
then
cp $scriptfile $scriptbackupfile
else
echo 'Error, file does not exist or is not a normal file.'
fi
#Test to see if created file exists. Print error if not
if test -f $scriptbackupfile
then
echo $scriptbackupfile 'has been created and is a normal file.'
else
echo 'Has not been created or is not a normal file.'
fi
;;
#Info Menu, saves to log_dir. Generic system/user info
2)
#Gather information and store in variables
who=$(who)
host=$(hostname)
path=$(echo ~)
user=$(whoami)
date=$(date)
logfile='log_dir/log_file_'$(date +"%d_%m_%Y")'.txt'
#Print info to file
echo 'Login: ' $who > $logfile
echo 'Host: ' $host >> $logfile
echo 'Directory: ' $path >> $logfile
echo 'Shell: ' $SHELL >> $logfile
#Check if file created and print content. Else print error.
if test -e $logfile
then
echo $logfile 'has been created, contents below'
cat $logfile
else
echo 'Error creating file'
fi
;;
#Create copy of a file
3)
#Ask for file name
echo 'Name of file to copy'
read sourcefile
#Ask for destination directory
echo 'Destination directory to copy to'
read destinationdirectory
#Check if source file exists
if test -f $sourcefile
then
#Check if destination directory exists
if test -d $destinationdirectory
then
#If both exist then copy file
cp $sourcefile $destinationdirectory
else
echo 'Directory does not exist'
fi
else
echo 'Error, file does not exist or is not a normal file.'
fi
#Checking to confirm copy of the file and show file has been created
if test -e $destinationdirectory/$sourcefile
then
echo $destinationdirectory/$sourcefile ' A copy has been created.'
else
echo 'Error has not been copied successfully.'
fi
;;
#Move file
4)
#Ask for file name
echo 'Name of file to move'
read sourcefile
#Ask for destination directory
echo 'Destination directory to move file to'
read destinationdirectory
#Check if source file exists
if test -f $sourcefile
then
#Check if destination directory exists
if test -d $destinationdirectory
then
#Move source file to destination directory
mv $sourcefile $destinationdirectory
else
echo 'Directory does not exist'
fi
else
echo 'Error, file does not exist or is not a normal file.'
fi
#Confirm move of the file and show file has been created
if test -e $destinationdirectory/$sourcefile
then
echo $destinationdirectory/$sourcefile ' File successfully moved.'
else
echo 'Error has not been moved successfully.'
fi
;;
#Print quit
Q) echo 'Bye!';;
esac
done