File: miscellany.html

package info (click to toggle)
abs-guide 10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,952 kB
  • sloc: sh: 14,129; makefile: 81
file content (454 lines) | stat: -rw-r--r-- 8,136 bytes parent folder | download | duplicates (5)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Miscellany</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Scripting With Style"
HREF="scrstyle.html"><LINK
REL="NEXT"
TITLE="Shell Wrappers"
HREF="wrapper.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="scrstyle.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="wrapper.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="MISCELLANY"
></A
>Chapter 36. Miscellany</H1
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
>&nbsp;</TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>Nobody really knows what the Bourne shell's grammar is. Even
        examination of the source code is little help.</I
></P
><P
><I
>--Tom Duff</I
></P
></I
></TD
></TR
></TABLE
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="INTANDNONINT"
></A
>36.1. Interactive and non-interactive shells and scripts</H1
><P
>An <I
CLASS="FIRSTTERM"
>interactive</I
> shell reads
	   commands from user input on a <TT
CLASS="FILENAME"
>tty</TT
>. Among
	   other things, such a shell reads startup files on activation,
	   displays a prompt, and enables job control by default. The
	   user can <I
CLASS="FIRSTTERM"
>interact</I
> with the shell.</P
><P
>A shell running a script is always a non-interactive
	   shell. All the same, the script can still access its
	   <TT
CLASS="FILENAME"
>tty</TT
>. It is even possible to emulate an
	   interactive shell in a script.

	   <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;MY_PROMPT='$ '
   3&nbsp;while :
   4&nbsp;do
   5&nbsp;  echo -n "$MY_PROMPT"
   6&nbsp;  read line
   7&nbsp;  eval "$line"
   8&nbsp;  done
   9&nbsp;
  10&nbsp;exit 0
  11&nbsp;
  12&nbsp;# This example script, and much of the above explanation supplied by
  13&nbsp;# Stphane Chazelas (thanks again).</PRE
></TD
></TR
></TABLE
></P
><P
>Let us consider an <I
CLASS="FIRSTTERM"
>interactive</I
>
	   script to be one that requires input from the user, usually
	   with <A
HREF="internal.html#READREF"
>read</A
> statements (see <A
HREF="internal.html#EX36"
>Example 15-3</A
>). <SPAN
CLASS="QUOTE"
>"Real life"</SPAN
> is actually a
	   bit messier than that. For now, assume an interactive script
	   is bound to a tty, a script that a user has invoked from the
	   console or an <I
CLASS="FIRSTTERM"
>xterm</I
>.</P
><P
>Init and startup scripts are necessarily non-interactive,
	   since they must run without human intervention. Many
	   administrative and system maintenance scripts are likewise
	   non-interactive.  Unvarying repetitive tasks cry out for
	   automation by non-interactive scripts.</P
><P
>Non-interactive scripts can run in the background, but
	   interactive ones hang, waiting for input that never comes.
	   Handle that difficulty by having an <B
CLASS="COMMAND"
>expect</B
>
	   script or embedded <A
HREF="here-docs.html#HEREDOCREF"
>here
	   document</A
> feed input to an interactive script running
	   as a background job.  In the simplest case, redirect a
	   file to supply input to a <B
CLASS="COMMAND"
>read</B
> statement
	   (<B
CLASS="COMMAND"
>read variable &#60;file</B
>).  These particular
	   workarounds make possible general purpose scripts that run
	   in either interactive or non-interactive modes.</P
><P
>If a script needs to test whether it is running in an
	   interactive shell, it is simply a matter of finding
	   whether the <I
CLASS="FIRSTTERM"
>prompt</I
> variable, <A
HREF="variables2.html#PS1REF"
>$PS1</A
> is set. (If the user is being
	   prompted for input, then the script needs to display a
	   prompt.)</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;if [ -z $PS1 ] # no prompt?
   2&nbsp;### if [ -v PS1 ]   # On Bash 4.2+ ...
   3&nbsp;then
   4&nbsp;  # non-interactive
   5&nbsp;  ...
   6&nbsp;else
   7&nbsp;  # interactive
   8&nbsp;  ...
   9&nbsp;fi</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="IITEST"
></A
>Alternatively, the script can test
	   for the presence of option <SPAN
CLASS="QUOTE"
>"i"</SPAN
> in the <A
HREF="variables2.html#FLPREF"
>$-</A
> flag.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;case $- in
   2&nbsp;*i*)    # interactive shell
   3&nbsp;;;
   4&nbsp;*)      # non-interactive shell
   5&nbsp;;;
   6&nbsp;# (Courtesy of "UNIX F.A.Q.," 1993)</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="II2TEST"
></A
>However, John Lange describes
             an alternative method, using the <A
HREF="fto.html#TERMTEST"
><SPAN
CLASS="TOKEN"
>-t</SPAN
>
	     <I
CLASS="FIRSTTERM"
>test</I
> operator</A
>.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;# Test for a terminal!
   2&nbsp;
   3&nbsp;fd=0   # stdin
   4&nbsp;
   5&nbsp;#  As we recall, the -t test option checks whether the stdin, [ -t 0 ],
   6&nbsp;#+ or stdout, [ -t 1 ], in a given script is running in a terminal.
   7&nbsp;if [ -t "$fd" ]
   8&nbsp;then
   9&nbsp;  echo interactive
  10&nbsp;else
  11&nbsp;  echo non-interactive
  12&nbsp;fi
  13&nbsp;
  14&nbsp;
  15&nbsp;#  But, as John points out:
  16&nbsp;#    if [ -t 0 ] works ... when you're logged in locally
  17&nbsp;#    but fails when you invoke the command remotely via ssh.
  18&nbsp;#    So for a true test you also have to test for a socket.
  19&nbsp;
  20&nbsp;if [[ -t "$fd" || -p /dev/stdin ]]
  21&nbsp;then
  22&nbsp;  echo interactive
  23&nbsp;else
  24&nbsp;  echo non-interactive
  25&nbsp;fi</PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Scripts may be forced to run in interactive
	   mode with the <SPAN
CLASS="TOKEN"
>-i</SPAN
> option or with a
	   <TT
CLASS="USERINPUT"
><B
>#!/bin/bash -i</B
></TT
> header. Be aware that
	   this can cause erratic script behavior or show error messages
	   even when no error is present.</P
></TD
></TR
></TABLE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="scrstyle.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="wrapper.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Scripting With Style</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Shell Wrappers</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>