1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
11
TITLE="Advanced Bash-Scripting Guide"
12
HREF="index.html"><LINK
14
TITLE="Advanced Topics"
15
HREF="part5.html"><LINK
17
TITLE="Scripting With Style"
18
HREF="scrstyle.html"><LINK
20
TITLE="Shell Wrappers"
21
HREF="wrapper.html"><META
22
HTTP-EQUIV="Content-Style-Type"
23
CONTENT="text/css"><LINK
25
HREF="common/kde-common.css"
27
HTTP-EQUIV="Content-Type"
28
CONTENT="text/html; charset=iso-8859-1"><META
29
HTTP-EQUIV="Content-Language"
32
HREF="common/kde-localised.css"
34
TITLE="KDE-English"><LINK
36
HREF="common/kde-default.css"
38
TITLE="KDE-Default"></HEAD
46
STYLE="font-family: sans-serif;"
50
SUMMARY="Header navigation table"
59
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
96
>Chapter 35. Miscellany</H1
114
>Nobody really knows what the Bourne shell's grammar is. Even
115
examination of the source code is little help.</I
132
>35.1. Interactive and non-interactive shells and scripts</H1
138
commands from user input on a <TT
142
other things, such a shell reads startup files on activation,
143
displays a prompt, and enables job control by default. The
149
>A shell running a script is always a non-interactive
150
shell. All the same, the script can still access its
154
>. It is even possible to emulate an
155
interactive shell in a script.
164
CLASS="PROGRAMLISTING"
166
2 MY_PROMPT='$ '
169
5 echo -n "$MY_PROMPT"
176
12 # This example script, and much of the above explanation supplied by
177
13 # St�phane Chazelas (thanks again).</PRE
183
>Let us consider an <I
187
script to be one that requires input from the user, usually
189
HREF="internal.html#READREF"
192
HREF="internal.html#EX36"
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
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
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
217
script or embedded <A
218
HREF="here-docs.html#HEREDOCREF"
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
229
>read variable <file</B
231
workarounds make possible general purpose scripts that run
232
in either interactive or non-interactive modes.</P
234
>If a script needs to test whether it is running in an
235
interactive shell, it is simply a matter of finding
240
HREF="variables2.html#PS1REF"
242
> is set. (If the user is being
243
prompted for input, then the script needs to display a
253
CLASS="PROGRAMLISTING"
254
> 1 if [ -z $PS1 ] # no prompt?
256
3 # non-interactive
259
6 # interactive
270
>Alternatively, the script can test
271
for the presence of option <SPAN
275
HREF="variables2.html#FLPREF"
286
CLASS="PROGRAMLISTING"
288
2 *i*) # interactive shell
290
4 *) # non-interactive shell
292
6 # (Courtesy of "UNIX F.A.Q.," 1993)</PRE
301
>However, John Lange describes
302
an alternative method, using the <A
303
HREF="fto.html#TERMTEST"
321
CLASS="PROGRAMLISTING"
322
> 1 # Test for a terminal!
326
5 # As we recall, the -t test option checks whether the stdin, [ -t 0 ],
327
6 #+ or stdout, [ -t 1 ], in a given script is running in a terminal.
328
7 if [ -t "$fd" ]
330
9 echo interactive
332
11 echo non-interactive
336
15 # But, as John points out:
337
16 # if [ -t 0 ] works ... when you're logged in locally
338
17 # but fails when you invoke the command remotely via ssh.
339
18 # So for a true test you also have to test for a socket.
341
20 if [[ -t "$fd" || -p /dev/stdin ]]
343
22 echo interactive
345
24 echo non-interactive
363
SRC="common/note.png"
370
>Scripts may be forced to run in interactive
380
> header. Be aware that
381
this can cause erratic script behavior or show error messages
382
even when no error is present.</P
394
SUMMARY="Footer navigation table"
433
>Scripting With Style</TD
b'\\ No newline at end of file'