getting changes from cakelampvm
[feisty_meow.git] / infobase / examples / bashisms / qs_handy_unix_examples.sh
1 #!/bin/bash
2
3 #
4 # these great examples of handy unix tidbits were donated by "q. black".
5 #
6
7 # list a directory tree
8 ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
9
10 # list directory sizes (biggest first)
11 du -h $(du -s * |sort -nr|awk '{print $2}')
12
13 # this will sort a date field of the form: DD-MON-YYYY HH:MM:SS
14 sort +0.7 -1 +0.3M -0.6 +0 -0.2 +1
15
16 # this will sort a date field of the form: MON DD HH:MM:SS YYYY
17 sort +3 -4 +0M +1n
18
19 # this will sort a date field of the form: MON DD HH:MM:SS
20 sort +0M +1n
21  
22 # this will sort a date field of the form: Date: Tue Feb  3 09:17:58 EST 2004
23 sort +6 -7 +2M +3n +4
24
25 # display all lines from a certain line onward
26 start_line=132
27 |awk "{if (NR >= ${start_line}){print \$0}}"
28
29 # display all lines after a token
30 sed '1,/CUT HERE/d'
31
32 # print the first and last lines
33 sed -n '1,1p;$,$p'
34
35 # signal bash about a window size change
36 kill -winch $$
37
38 # show the date 1 year, 2 months and 3 days ago
39 date -v -1y -v -2m -v -3d
40
41 # set the date back 1 year
42 sudo date $(date -v -1y +%Y%m%d%H%M)
43
44 # output the standard date format for setting the time
45 # get the date
46 date -u +%Y%m%d%H%M.%S
47 # set the date
48 date -u (cut and paste from above)
49
50 # convert one date format to another (output is in the current time zone)
51 old_date="Aug 27 15:24:33 2005 GMT"
52 new_date=$(date -j -f "%b %e %T %Y %Z" "${old_date}" +%D)
53 echo ${new_date}
54 # returns "08/27/05"
55
56 # output the modification time of a file in different format
57 file=
58 date -j -f "%b %e %T %Y" "$(ls -lT ${file} |awk '{print $6,$7,$8,$9}')"
59
60 # output the number of days until a certain date
61 target_date="Sep  2 15:20:20 2005 GMT"
62 target_seconds=$(date -j -f "%b %e %T %Y" +%s "${target_date}" 2>/dev/null)
63 diff_seconds=$(expr ${target_seconds} - $(date +%s))
64 diff_days=$(expr ${diff_seconds} / 86400)
65 echo "${diff_days} day(s)"
66
67 # these commands can be used to fill in missing times in a "uniq -c" count
68 # of times.
69 # output 24 hours in one minute increments
70 for h in $(jot -w %02d - 0 23 1); do
71     for m in $(jot -w %02d - 0 59 1); do
72         echo "   0 ${h}:${m}"
73     done
74 done
75 # sort them together, and remove any 0 counts if an count already exists
76 sort +1 +0rn out1 out2 |uniq -f 1
77
78 # output with w3m to get basic html word wrap
79 w3m -T "text/html" -dump -cols 72 <<EOF
80     <p>
81     This test verifies basic networking and that the ${product_short_name}
82     can reach it's default gateway.
83 EOF
84
85 # another way to format text for output
86 fmt 72 <<EOF
87 This test verifies basic networking and that the ${product_short_name}
88 can reach it's default gateway.
89 EOF
90
91 # smtpcrypt "printf"
92 {
93 char *jkwbuf = NULL;
94 asprintf(&jkwbuf, "JKW: msg->used = %ld\n", msg->used);
95 BIO_write(sc->log, jkwbuf, strlen(jkwbuf)+1);
96 free(jkwbuf);
97 }
98
99 # rolling diff of a list of files (diff a & b, then b & c,...)
100 last=
101 for i in $(ls -1rt); do
102     if [ ! -z "${last}" ]; then
103         diff -u ${last} ${i}
104     fi
105     last=${i}
106 done
107
108 # clearing and restoring chflags
109 file=
110 old_chflags=$(ls -lo ${file}|awk '{print $5}')
111 chflags 0 ${file}
112 # do whatever
113 if [ ."${old_chflags}" != ."-" ]; then
114     chflags ${old_chflags} ${file}
115 fi
116
117 # way to do standard edits to files
118 file=
119 {
120     # append line(s) after a line, "i" to insert before
121     echo '/www_recovery/a'
122     echo 'mithril ALL = (root) NOPASSWD: /usr/local/libexec/destroyer'
123     echo '.'
124     # modify a line
125     echo 'g/^xntpd_program=/s,^xntpd_program=.*$,xntpd_program="ntpd",'
126     # delete a line
127     echo 'g/^controls key secret =/d'
128     echo 'x!'
129 } | ex - ${file}
130
131 # how to search for errors in the last 24 hours
132 # note that this command does not work quite right.  The sort is off early
133 # in the year because the dates do not have the year.
134 # Also sed never sees the /CUT HERE/ when it is the first line.
135 (echo "$(date -v-24H "+%b %e %H:%M:%S") --CUT HERE--"; \
136     zgrep -h "cookie" /var/log/messages*)|sort +0M| \
137     sed '1,/CUT HERE/d'
138 # This version fixes those problems.  It adds the file year to the date
139 # and puts a marker at the start of the list.
140 (echo "$(date -j -f "%s" 0 "+%Y %b %e %H:%M:%S") --ALWAYS FIRST--"; \
141     echo "$(date -v-24H "+%Y %b %e %H:%M:%S") --CUT HERE--"; \
142     for i in /var/log/messages*; do
143         year=$(ls -lT ${i}|awk '{print $9}')
144         zgrep -h "cookie" ${i}|while read line; do
145             echo "${year} ${line}"
146         done
147     done)|sort +0n +1M| sed '1,/CUT HERE/d'
148
149 # process a list of quoted values
150 {
151     # It tends to be easiest to use a 'here-document' to feed in the list.
152     # I prefer to have the list at the start instead of the end
153     cat <<EOF
154         'general' 'network node0' 'private address'
155         'general' 'options node2' 'kern securelevel'
156 EOF
157 }| while read line; do
158     eval set -- ${line}
159     config=$1; shift
160     section=$1; shift
161     key=$1; shift
162
163     echo "confutil value \"${config}\" \"${section}\" \"${key}\""
164 done
165
166 # Method to read lines with a "for" loop, without spawning a subshell
167 NEWLINE='
168 '
169 OIFS="${IFS}"
170 IFS="${NEWLINE}"
171 for line in $(cat /etc/passwd | sort -r); do
172     IFS="${OIFS}"
173
174     # do whatever you want here
175     echo "line = ${line}"
176
177     IFS="${NEWLINE}"
178 done
179 IFS="${OIFS}"
180
181 # generate a histogram of characters in a file
182 cat file|
183     awk '{for (i=1; i <= length($0); i++) {printf("%s\n",substr($0,i,1))}}'|
184     sort|uniq -c
185
186 # show line lengths for a file
187 cat file| awk '{print length($0)}'| sort -n
188
189
190
191
192 # get the modification time of a directory or file and then reset the time.
193 target=/usr/local/etc/pkdb
194 save_date=$(ls -ldT ${target}|awk '{print $6,$7,$8,$9}')
195 save_date=$(date -j -f "%b %e %T %Y" "${save_date}" +"%Y%m%d%H%M.%S")
196 # later
197 touch -t ${save_date} ${target}
198
199
200 # detect NULL bytes in a file
201 file=
202 hexdump -e '"%_u\n"' ${file}|grep -q '^nul$'
203 if [ $? -eq 0 ]; then
204 else
205 fi
206
207
208 # calculate average
209 cd /tmp
210 uudecode
211 begin 644 bc.average
212 M<V-A;&4],PIT;W1A;#TP"F-O=6YT/3`*=VAI;&4@*#$I('L*("`@(&YU;2`]
213 M(')E860H*0H@("`@:68@*&YU;2`]/2`P*2!B<F5A:SL*("`@(&-O=6YT*RL*
214 M("`@('1O=&%L("L](&YU;0I]"B)T;W1A;"`]("([('1O=&%L"B)C;W5N="`]
215 K("([(&-O=6YT"B)A=F5R86=E(#T@(CL@=&]T86P@+R!C;W5N=`IQ=6ET"@``
216 `
217 end
218 (cat data; echo "0") |bc -q bc.average
219
220