~ubuntu-branches/ubuntu/natty/abs-guide/natty

« back to all changes in this revision

Viewing changes to abs/HTML/miscellany.html

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-05-09 17:49:52 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100509174952-1s583w0zbh7rl1po
Tags: 6.2-1
* New upstream release; Closes: #441278
* debian/control
  - adopting package; Closes: #577181
  - removed bzip2 from b-d-i, no more needed
  - debhelper has to be in b-d
  - bump Standards-Version to 3.8.4 (no changes needed)
  - added Homepage field
  - added misc:Depends to Depends
  - added Vcs-{Browser, Git} field
* debian/rules
  - removed the unpacking stuff, we now use an alrqady unpacked source
  - added more example scripts to fix
* debian/{compat, control, rules}
  - switched to debhelper 7
* debian/watch
  - updated to new upstream location; thanks to Raphael Geissert for the
    report; Closes: #453596
* debian/copyright
  - updated with new location and copyright/license notices
* debian/source/format
  - set source package format to 1.0 explicitly
* debian/abs-guide.doc-base
  - set Section field correctly
* debian/{copyright, abs-guide.doc-base}
  - updated upstream email
* debian/abs-guide.lintian-overrides
  - added override for "known syntax-errored" scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Miscellany</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="Advanced Topics"
 
15
HREF="part5.html"><LINK
 
16
REL="PREVIOUS"
 
17
TITLE="Scripting With Style"
 
18
HREF="scrstyle.html"><LINK
 
19
REL="NEXT"
 
20
TITLE="Shell Wrappers"
 
21
HREF="wrapper.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="CHAPTER"
 
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="scrstyle.html"
 
68
ACCESSKEY="P"
 
69
>Prev</A
 
70
></TD
 
71
><TD
 
72
WIDTH="80%"
 
73
ALIGN="center"
 
74
VALIGN="bottom"
 
75
></TD
 
76
><TD
 
77
WIDTH="10%"
 
78
ALIGN="right"
 
79
VALIGN="bottom"
 
80
><A
 
81
HREF="wrapper.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="CHAPTER"
 
92
><H1
 
93
><A
 
94
NAME="MISCELLANY"
 
95
></A
 
96
>Chapter 35. Miscellany</H1
 
97
><TABLE
 
98
BORDER="0"
 
99
WIDTH="100%"
 
100
CELLSPACING="0"
 
101
CELLPADDING="0"
 
102
CLASS="EPIGRAPH"
 
103
><TR
 
104
><TD
 
105
WIDTH="45%"
 
106
>&nbsp;</TD
 
107
><TD
 
108
WIDTH="45%"
 
109
ALIGN="LEFT"
 
110
VALIGN="TOP"
 
111
><I
 
112
><P
 
113
><I
 
114
>Nobody really knows what the Bourne shell's grammar is. Even
 
115
        examination of the source code is little help.</I
 
116
></P
 
117
><P
 
118
><I
 
119
>--Tom Duff</I
 
120
></P
 
121
></I
 
122
></TD
 
123
></TR
 
124
></TABLE
 
125
><DIV
 
126
CLASS="SECT1"
 
127
><H1
 
128
CLASS="SECT1"
 
129
><A
 
130
NAME="INTANDNONINT"
 
131
></A
 
132
>35.1. Interactive and non-interactive shells and scripts</H1
 
133
><P
 
134
>An <I
 
135
CLASS="FIRSTTERM"
 
136
>interactive</I
 
137
> shell reads
 
138
           commands from user input on a <TT
 
139
CLASS="FILENAME"
 
140
>tty</TT
 
141
>. Among
 
142
           other things, such a shell reads startup files on activation,
 
143
           displays a prompt, and enables job control by default. The
 
144
           user can <I
 
145
CLASS="FIRSTTERM"
 
146
>interact</I
 
147
> with the shell.</P
 
148
><P
 
149
>A shell running a script is always a non-interactive
 
150
           shell. All the same, the script can still access its
 
151
           <TT
 
152
CLASS="FILENAME"
 
153
>tty</TT
 
154
>. It is even possible to emulate an
 
155
           interactive shell in a script.
 
156
 
 
157
           <TABLE
 
158
BORDER="0"
 
159
BGCOLOR="#E0E0E0"
 
160
WIDTH="100%"
 
161
><TR
 
162
><TD
 
163
><PRE
 
164
CLASS="PROGRAMLISTING"
 
165
>   1&nbsp;#!/bin/bash
 
166
   2&nbsp;MY_PROMPT='$ '
 
167
   3&nbsp;while :
 
168
   4&nbsp;do
 
169
   5&nbsp;  echo -n "$MY_PROMPT"
 
170
   6&nbsp;  read line
 
171
   7&nbsp;  eval "$line"
 
172
   8&nbsp;  done
 
173
   9&nbsp;
 
174
  10&nbsp;exit 0
 
175
  11&nbsp;
 
176
  12&nbsp;# This example script, and much of the above explanation supplied by
 
177
  13&nbsp;# St�phane Chazelas (thanks again).</PRE
 
178
></TD
 
179
></TR
 
180
></TABLE
 
181
></P
 
182
><P
 
183
>Let us consider an <I
 
184
CLASS="FIRSTTERM"
 
185
>interactive</I
 
186
>
 
187
           script to be one that requires input from the user, usually
 
188
           with <A
 
189
HREF="internal.html#READREF"
 
190
>read</A
 
191
> statements (see <A
 
192
HREF="internal.html#EX36"
 
193
>Example 15-3</A
 
194
>). <SPAN
 
195
CLASS="QUOTE"
 
196
>"Real life"</SPAN
 
197
> is actually a
 
198
           bit messier than that. For now, assume an interactive script
 
199
           is bound to a tty, a script that a user has invoked from the
 
200
           console or an <I
 
201
CLASS="FIRSTTERM"
 
202
>xterm</I
 
203
>.</P
 
204
><P
 
205
>Init and startup scripts are necessarily non-interactive,
 
206
           since they must run without human intervention. Many
 
207
           administrative and system maintenance scripts are likewise
 
208
           non-interactive.  Unvarying repetitive tasks cry out for
 
209
           automation by non-interactive scripts.</P
 
210
><P
 
211
>Non-interactive scripts can run in the background, but
 
212
           interactive ones hang, waiting for input that never comes.
 
213
           Handle that difficulty by having an <B
 
214
CLASS="COMMAND"
 
215
>expect</B
 
216
>
 
217
           script or embedded <A
 
218
HREF="here-docs.html#HEREDOCREF"
 
219
>here
 
220
           document</A
 
221
> feed input to an interactive script running
 
222
           as a background job.  In the simplest case, redirect a
 
223
           file to supply input to a <B
 
224
CLASS="COMMAND"
 
225
>read</B
 
226
> statement
 
227
           (<B
 
228
CLASS="COMMAND"
 
229
>read variable &#60;file</B
 
230
>).  These particular
 
231
           workarounds make possible general purpose scripts that run
 
232
           in either interactive or non-interactive modes.</P
 
233
><P
 
234
>If a script needs to test whether it is running in an
 
235
           interactive shell, it is simply a matter of finding
 
236
           whether the <I
 
237
CLASS="FIRSTTERM"
 
238
>prompt</I
 
239
> variable, <A
 
240
HREF="variables2.html#PS1REF"
 
241
>$PS1</A
 
242
> is set. (If the user is being
 
243
           prompted for input, then the script needs to display a
 
244
           prompt.)</P
 
245
><P
 
246
><TABLE
 
247
BORDER="0"
 
248
BGCOLOR="#E0E0E0"
 
249
WIDTH="100%"
 
250
><TR
 
251
><TD
 
252
><PRE
 
253
CLASS="PROGRAMLISTING"
 
254
>   1&nbsp;if [ -z $PS1 ] # no prompt?
 
255
   2&nbsp;then
 
256
   3&nbsp;  # non-interactive
 
257
   4&nbsp;  ...
 
258
   5&nbsp;else
 
259
   6&nbsp;  # interactive
 
260
   7&nbsp;  ...
 
261
   8&nbsp;fi</PRE
 
262
></TD
 
263
></TR
 
264
></TABLE
 
265
></P
 
266
><P
 
267
><A
 
268
NAME="IITEST"
 
269
></A
 
270
>Alternatively, the script can test
 
271
           for the presence of option <SPAN
 
272
CLASS="QUOTE"
 
273
>"i"</SPAN
 
274
> in the <A
 
275
HREF="variables2.html#FLPREF"
 
276
>$-</A
 
277
> flag.</P
 
278
><P
 
279
><TABLE
 
280
BORDER="0"
 
281
BGCOLOR="#E0E0E0"
 
282
WIDTH="100%"
 
283
><TR
 
284
><TD
 
285
><PRE
 
286
CLASS="PROGRAMLISTING"
 
287
>   1&nbsp;case $- in
 
288
   2&nbsp;*i*)    # interactive shell
 
289
   3&nbsp;;;
 
290
   4&nbsp;*)      # non-interactive shell
 
291
   5&nbsp;;;
 
292
   6&nbsp;# (Courtesy of "UNIX F.A.Q.," 1993)</PRE
 
293
></TD
 
294
></TR
 
295
></TABLE
 
296
></P
 
297
><P
 
298
><A
 
299
NAME="II2TEST"
 
300
></A
 
301
>However, John Lange describes
 
302
             an alternative method, using the <A
 
303
HREF="fto.html#TERMTEST"
 
304
><SPAN
 
305
CLASS="TOKEN"
 
306
>-t</SPAN
 
307
>
 
308
             <I
 
309
CLASS="FIRSTTERM"
 
310
>test</I
 
311
> operator</A
 
312
>.</P
 
313
><P
 
314
><TABLE
 
315
BORDER="0"
 
316
BGCOLOR="#E0E0E0"
 
317
WIDTH="100%"
 
318
><TR
 
319
><TD
 
320
><PRE
 
321
CLASS="PROGRAMLISTING"
 
322
>   1&nbsp;# Test for a terminal!
 
323
   2&nbsp;
 
324
   3&nbsp;fd=0   # stdin
 
325
   4&nbsp;
 
326
   5&nbsp;#  As we recall, the -t test option checks whether the stdin, [ -t 0 ],
 
327
   6&nbsp;#+ or stdout, [ -t 1 ], in a given script is running in a terminal.
 
328
   7&nbsp;if [ -t "$fd" ]
 
329
   8&nbsp;then
 
330
   9&nbsp;  echo interactive
 
331
  10&nbsp;else
 
332
  11&nbsp;  echo non-interactive
 
333
  12&nbsp;fi
 
334
  13&nbsp;
 
335
  14&nbsp;
 
336
  15&nbsp;#  But, as John points out:
 
337
  16&nbsp;#    if [ -t 0 ] works ... when you're logged in locally
 
338
  17&nbsp;#    but fails when you invoke the command remotely via ssh.
 
339
  18&nbsp;#    So for a true test you also have to test for a socket.
 
340
  19&nbsp;
 
341
  20&nbsp;if [[ -t "$fd" || -p /dev/stdin ]]
 
342
  21&nbsp;then
 
343
  22&nbsp;  echo interactive
 
344
  23&nbsp;else
 
345
  24&nbsp;  echo non-interactive
 
346
  25&nbsp;fi</PRE
 
347
></TD
 
348
></TR
 
349
></TABLE
 
350
></P
 
351
><DIV
 
352
CLASS="NOTE"
 
353
><TABLE
 
354
CLASS="NOTE"
 
355
WIDTH="100%"
 
356
BORDER="0"
 
357
><TR
 
358
><TD
 
359
WIDTH="25"
 
360
ALIGN="CENTER"
 
361
VALIGN="TOP"
 
362
><IMG
 
363
SRC="common/note.png"
 
364
HSPACE="5"
 
365
ALT="Note"></TD
 
366
><TD
 
367
ALIGN="LEFT"
 
368
VALIGN="TOP"
 
369
><P
 
370
>Scripts may be forced to run in interactive
 
371
           mode with the <SPAN
 
372
CLASS="TOKEN"
 
373
>-i</SPAN
 
374
> option or with a
 
375
           <TT
 
376
CLASS="USERINPUT"
 
377
><B
 
378
>#!/bin/bash -i</B
 
379
></TT
 
380
> header. Be aware that
 
381
           this can cause erratic script behavior or show error messages
 
382
           even when no error is present.</P
 
383
></TD
 
384
></TR
 
385
></TABLE
 
386
></DIV
 
387
></DIV
 
388
></DIV
 
389
><DIV
 
390
CLASS="NAVFOOTER"
 
391
><HR
 
392
ALIGN="LEFT"
 
393
WIDTH="100%"><TABLE
 
394
SUMMARY="Footer navigation table"
 
395
WIDTH="100%"
 
396
BORDER="0"
 
397
CELLPADDING="0"
 
398
CELLSPACING="0"
 
399
><TR
 
400
><TD
 
401
WIDTH="33%"
 
402
ALIGN="left"
 
403
VALIGN="top"
 
404
><A
 
405
HREF="scrstyle.html"
 
406
ACCESSKEY="P"
 
407
>Prev</A
 
408
></TD
 
409
><TD
 
410
WIDTH="34%"
 
411
ALIGN="center"
 
412
VALIGN="top"
 
413
><A
 
414
HREF="index.html"
 
415
ACCESSKEY="H"
 
416
>Home</A
 
417
></TD
 
418
><TD
 
419
WIDTH="33%"
 
420
ALIGN="right"
 
421
VALIGN="top"
 
422
><A
 
423
HREF="wrapper.html"
 
424
ACCESSKEY="N"
 
425
>Next</A
 
426
></TD
 
427
></TR
 
428
><TR
 
429
><TD
 
430
WIDTH="33%"
 
431
ALIGN="left"
 
432
VALIGN="top"
 
433
>Scripting With Style</TD
 
434
><TD
 
435
WIDTH="34%"
 
436
ALIGN="center"
 
437
VALIGN="top"
 
438
><A
 
439
HREF="part5.html"
 
440
ACCESSKEY="U"
 
441
>Up</A
 
442
></TD
 
443
><TD
 
444
WIDTH="33%"
 
445
ALIGN="right"
 
446
VALIGN="top"
 
447
>Shell Wrappers</TD
 
448
></TR
 
449
></TABLE
 
450
></DIV
 
451
></BODY
 
452
></HTML
 
453
>
 
 
b'\\ No newline at end of file'