-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpfile.sh
143 lines (130 loc) · 2.51 KB
/
cmpfile.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
#!/bin/bash
# -d check if it is a directory
if [ -d $HOME ]
then
echo "Your home directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME directory"
fi
# -e check if it exists (either a file or a directory)
if [ -e $HOME ]
then
echo "OK on the directory, now to check the file"
if [ -e $HOME/test ]
then
echo "Appending date to existing file"
date >> $HOME/test
else
echo "Creating new file"
date > $HOME/test
fi
else
echo "Sorry, you do not have a HOME directory"
fi
cat $HOME/test
rm $HOME/test
# -f check if it is a file
if [ -e $HOME ]
then
echo "The object exists, is it a file?"
if [ -f $HOME ]
then
echo "Yes, it is a file!"
else
echo "No, it is not a file!"
if [ -f $HOME/.bash_history ]
then
echo "But this is a file!"
fi
fi
else
echo "Sorry, the object does not exist"
fi
# -r check if it is readable
pwfile=/etc/shadow
if [ -f $pwfile ]
then
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry, I am unable to read the $pwfile file"
fi
else
echo "Sorry, the file $file does not exist"
fi
# -s check if it exists and not empty
file=test4s
if [ -s $file ]
then
echo "The $file exists and has data in it"
else
echo "The $file does not exist or it is empty"
touch $file
if [ -s $file ]
then
echo "The $file file has data in it"
else
echo "The $file does not exist or it is empty"
date > $file
if [ -s $file ]
then
echo "The $file file has data in it"
else
echo "WHY?"
fi
fi
fi
cat $file
rm $file
# -w check if it is writable
logfile=$HOME/test
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`
if [ -w $logfile ]
then
echo "The program ran at: $now" >$logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed"
fi
chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at: $now" >$logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi
cat $logfile
rm $logfile
# -x check if it is executable
if [ -f $HOME/a.out ]
then
if [ -x $HOME/a.out ]
then
echo "You can run the file:"
$HOME/a.out
else
echo "Sorry, you are unable to execute it."
fi
fi
# -O check if it is of your own
if [ -O /etc/passwd ]
then
echo "/etc/passwd is yours"
else
echo "Sorry, you are not the owner of /etc/passwd"
fi
# -G check if it is owned by your group (default group)
if [ -G /etc/passwd ]
then
echo "You are in the same group as the file"
else
echo "The file is not owned by your group"
fi
# file1 -nt file2 ---> file1 is newer than file2
# file1 -ot file2 ---> file1 is older than file2