-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter2_Shell_Programming
More file actions
75 lines (65 loc) · 3.66 KB
/
Chapter2_Shell_Programming
File metadata and controls
75 lines (65 loc) · 3.66 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
68
69
#!/bin/sh
# page 60 Beginning Linux Programming , expanded and corrected by me version of programm
trap 'rm -f /tmp/my_tmp_file_$$' INT #SIGINT is not correct to use. All signal names are to be without SIG...
#trap 'rm -f /tmp/my_tmp_file_$$' 2 #that would be an equivalent way to look for INT signal
echo "creating file /tmp/my_tmp_file_$$"
date > /tmp/my_tmp_file_$$
echo "press interrupt (CTRL-C) to interrupt ....(and rm -f command will be executed!)"
while [ -f /tmp/my_tmp_file_$$ ] #as long as the user doesn't press Ctrl-C ,causing the trap to be caught and the rm -f ,do...
do
echo " File exists and it contains :"
cat /tmp/my_tmp_file_$$
sleep 3
done
echo " The file no longer exists, we deleted it, as a reaction to catching the Ctrl+C "
trap '' 2 #Corrected in the public Errata as: trap - INT.Warning : trap '' INT ignores the signal, nothing happens!
# and the process will have to be : kill -9 PID#
trap '' KILL #but this can still not make the running script unstopable after sending it the signal :
#kill -9 PID#
echo "Creating again some file /tmp/my_tmp_file_$$"
echo "Do not forget to remove them after the script finishes!"
date > /tmp/my_tmp_file_$$
echo "press interrupt (control-C) to interrupt ....(and no commands will be executed!)"
echo "Remember to remove by hand the files in /tmp/my_tmp_fileXX "
echo "and also to check that the exit code of the script is not 0, check it with : echo \$?"
while [ -f /tmp/my_tmp_file_$$ ]
do
echo " File exists again and it contains :"
cat /tmp/my_tmp_file_$$
sleep 4
done
echo "we never get here, so the next exit 0 is never reached."
exit 0
#We can delete the created /tmp/my_tmp... files with either one of the following ways
#rm /tmp/my_tmp_*
#find /tmp -name my_tmp_* -exec rm {} \;
#find /tmp -name my_tmp_* -delete
# page 68 Beginning Linux Programming
In example 4 for regular expressions , they say :
"Finally, use the extended grep mode to search for lowercase words that are exactly 10 characters
long. Do this by specifying a range of characters to match a to z, and a repetition of 10 matches:"
And in then give as a solution
$ grep -E [a-z]\{10\} words2.txt
In the errata they even correct it as :
$ grep -E [A-Za-z]\{10\} words2.txt
But there is a subtle case : if there are lines with words more than 10 characters , they will also match.
So the solutions given are good only for matching lines that have words with 10 or more characters. For
matching only lines that have words exactly 10 characters a solution would be
#$ grep -E [a-z]\{10\}[[:space:]]+ words2.txt #I tested it and still matches words bigger than 10 characters
#$ grep -E \([a-z]\{10\}\)[[:space:]]+ words2.txt #I tested it and still matches words bigger than 10 characters
#!/bin/sh
# page 72 of Beginning Linux Programming 2007
# putting the output (as corrected by the public errata) near the commands for ease of reference
unset foo
echo ${foo:-bar} # gives : bar
foo=fud
echo ${foo:-bar} # gives : fud
foo=/usr/bin/X11/startx
echo ${foo#*/} # gives : usr/bin/X11/startx (removes only first / , left to right )
echo ${foo##*/} # gives : startx (removes everything up to last .../ , so it removes /usr/bin/X11/ ,left-to-right)
bar=/usr/local/etc/local/networks
echo ${bar%local*} # gives : /usr/local/etc/ (works right-to-left , catches only local/networks and removes it
echo ${bar%%local*} # gives : /usr/ (works right-to-left , catches whole local/etc/local/networks and removes it
echo ${bar%/local*} # gives : /usr/local/etc (works right-to-left , catches only /local/networks and removes it
echo ${bar%%/local*} # gives : /usr (works right-to-left , catches whole /local/etc/local/networks and removes it
exit 0