Debian Codegraph @

HTML/redirapps.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Applications</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="I/O Redirection"
15 HREF="io-redirection.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Redirecting Code Blocks"
18 HREF="redircb.html"><LINK
19 REL="NEXT"
20 TITLE="Subshells"
21 HREF="subshells.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="SECT1"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="redircb.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 20. I/O Redirection</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="subshells.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="REDIRAPPS"
96 ></A
97 >20.3. Applications</H1
98 ><P
99 >Clever use of I/O redirection permits parsing and stitching
100 together snippets of command output (see <A
101 HREF="internal.html#READREDIR"
102 >Example 15-7</A
103 >). This permits
104 generating report and log files.</P
105 ><DIV
106 CLASS="EXAMPLE"
107 ><HR><A
108 NAME="LOGEVENTS"
109 ></A
110 ><P
111 ><B
112 >Example 20-12. Logging events</B
113 ></P
114 ><TABLE
115 BORDER="0"
116 BGCOLOR="#E0E0E0"
117 WIDTH="100%"
118 ><TR
119 ><TD
120 ><PRE
121 CLASS="PROGRAMLISTING"
122 > 1&nbsp;#!/bin/bash
123 2&nbsp;# logevents.sh
124 3&nbsp;# Author: Stephane Chazelas.
125 4&nbsp;# Used in ABS Guide with permission.
126 5&nbsp;
127 6&nbsp;# Event logging to a file.
128 7&nbsp;# Must be run as root (for write access in /var/log).
129 8&nbsp;
130 9&nbsp;ROOT_UID=0 # Only users with $UID 0 have root privileges.
131 10&nbsp;E_NOTROOT=67 # Non-root exit error.
132 11&nbsp;
133 12&nbsp;
134 13&nbsp;if [ "$UID" -ne "$ROOT_UID" ]
135 14&nbsp;then
136 15&nbsp; echo "Must be root to run this script."
137 16&nbsp; exit $E_NOTROOT
138 17&nbsp;fi
139 18&nbsp;
140 19&nbsp;
141 20&nbsp;FD_DEBUG1=3
142 21&nbsp;FD_DEBUG2=4
143 22&nbsp;FD_DEBUG3=5
144 23&nbsp;
145 24&nbsp;# === Uncomment one of the two lines below to activate script. ===
146 25&nbsp;# LOG_EVENTS=1
147 26&nbsp;# LOG_VARS=1
148 27&nbsp;
149 28&nbsp;
150 29&nbsp;log() # Writes time and date to log file.
151 30&nbsp;{
152 31&nbsp;echo "$(date) $*" &#62;&#38;7 # This *appends* the date to the file.
153 32&nbsp;# ^^^^^^^ command substitution
154 33&nbsp; # See below.
155 34&nbsp;}
156 35&nbsp;
157 36&nbsp;
158 37&nbsp;
159 38&nbsp;case $LOG_LEVEL in
160 39&nbsp; 1) exec 3&#62;&#38;2 4&#62; /dev/null 5&#62; /dev/null;;
161 40&nbsp; 2) exec 3&#62;&#38;2 4&#62;&#38;2 5&#62; /dev/null;;
162 41&nbsp; 3) exec 3&#62;&#38;2 4&#62;&#38;2 5&#62;&#38;2;;
163 42&nbsp; *) exec 3&#62; /dev/null 4&#62; /dev/null 5&#62; /dev/null;;
164 43&nbsp;esac
165 44&nbsp;
166 45&nbsp;FD_LOGVARS=6
167 46&nbsp;if [[ $LOG_VARS ]]
168 47&nbsp;then exec 6&#62;&#62; /var/log/vars.log
169 48&nbsp;else exec 6&#62; /dev/null # Bury output.
170 49&nbsp;fi
171 50&nbsp;
172 51&nbsp;FD_LOGEVENTS=7
173 52&nbsp;if [[ $LOG_EVENTS ]]
174 53&nbsp;then
175 54&nbsp; # exec 7 &#62;(exec gawk '{print strftime(), $0}' &#62;&#62; /var/log/event.log)
176 55&nbsp; # Above line fails in versions of Bash more recent than 2.04. Why?
177 56&nbsp; exec 7&#62;&#62; /var/log/event.log # Append to "event.log".
178 57&nbsp; log # Write time and date.
179 58&nbsp;else exec 7&#62; /dev/null # Bury output.
180 59&nbsp;fi
181 60&nbsp;
182 61&nbsp;echo "DEBUG3: beginning" &#62;&#38;${FD_DEBUG3}
183 62&nbsp;
184 63&nbsp;ls -l &#62;&#38;5 2&#62;&#38;4 # command1 &#62;&#38;5 2&#62;&#38;4
185 64&nbsp;
186 65&nbsp;echo "Done" # command2
187 66&nbsp;
188 67&nbsp;echo "sending mail" &#62;&#38;${FD_LOGEVENTS}
189 68&nbsp;# Writes "sending mail" to file descriptor #7.
190 69&nbsp;
191 70&nbsp;
192 71&nbsp;exit 0</PRE
193 ></TD
194 ></TR
195 ></TABLE
196 ><HR></DIV
197 ></DIV
198 ><DIV
199 CLASS="NAVFOOTER"
200 ><HR
201 ALIGN="LEFT"
202 WIDTH="100%"><TABLE
203 SUMMARY="Footer navigation table"
204 WIDTH="100%"
205 BORDER="0"
206 CELLPADDING="0"
207 CELLSPACING="0"
208 ><TR
209 ><TD
210 WIDTH="33%"
211 ALIGN="left"
212 VALIGN="top"
213 ><A
214 HREF="redircb.html"
215 ACCESSKEY="P"
216 >Prev</A
217 ></TD
218 ><TD
219 WIDTH="34%"
220 ALIGN="center"
221 VALIGN="top"
222 ><A
223 HREF="index.html"
224 ACCESSKEY="H"
225 >Home</A
226 ></TD
227 ><TD
228 WIDTH="33%"
229 ALIGN="right"
230 VALIGN="top"
231 ><A
232 HREF="subshells.html"
233 ACCESSKEY="N"
234 >Next</A
235 ></TD
236 ></TR
237 ><TR
238 ><TD
239 WIDTH="33%"
240 ALIGN="left"
241 VALIGN="top"
242 >Redirecting Code Blocks</TD
243 ><TD
244 WIDTH="34%"
245 ALIGN="center"
246 VALIGN="top"
247 ><A
248 HREF="io-redirection.html"
249 ACCESSKEY="U"
250 >Up</A
251 ></TD
252 ><TD
253 WIDTH="33%"
254 ALIGN="right"
255 VALIGN="top"
256 >Subshells</TD
257 ></TR
258 ></TABLE
259 ></DIV
260 ></BODY
261 ></HTML
262 >