-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2.bash
More file actions
67 lines (59 loc) · 1.41 KB
/
Lab2.bash
File metadata and controls
67 lines (59 loc) · 1.41 KB
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
#!/bin/bash
# counter of interruptions
counter=0
# file name
filename=''
# prints prompt for user. Mostly helpful in SIGINT handler
printPrompt(){
echo 'Enter filename: '
}
# checks if file <filename> exists, doing until user do not write correct filename
getCorrectFileName(){
while true
do
# prints prompt for user
printPrompt
# gets user input
read filename
# checks if file exists
if [[ ! -f $filename ]]; then
echo "File $filename not found"
# null filename for SIGINT handler
filename=''
else
# in <filename> now correct file name, exit from inf cycle
break
fi
done
}
# SIGINT handler
sigintHandler(){
# beautifier
echo ''
# increase interrupt counter
(( counter++ ))
# every odd interruption
if [[ $((counter % 2)) = 1 ]]; then
# prints all non-binary files in current directory
find . -maxdepth 1 -type f -exec grep -Iq . {} \; -print;
fi
# if user on input filename stage, we should give him new prompt
if [[ $filename = '' ]]; then
printPrompt
fi
}
# rewrite SIGINT handler
trap 'sigintHandler' SIGINT
# get filename from user input
getCorrectFileName
# get words count, print info to STDOUT and file info.txt
wc $filename | awk {'print "Words count: " $2'} | tee info.txt
# command cycle (for interruptions)
while true
do
echo 'Enter "exit" to stop or Ctrl+C to show the list of non-binary files in current dir'
read command
if [[ $command = "exit" ]]; then
exit 0
fi
done