⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miscellany.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Miscellany</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="Scripting With Style"HREF="scrstyle.html"><LINKREL="NEXT"TITLE="Operator Precedence"HREF="opprecedence.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="scrstyle.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="opprecedence.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="MISCELLANY"></A>Chapter 33. Miscellany</H1><TABLEBORDER="0"WIDTH="100%"CELLSPACING="0"CELLPADDING="0"CLASS="EPIGRAPH"><TR><TDWIDTH="45%">&nbsp;</TD><TDWIDTH="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><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="INTANDNONINT"></A>33.1. Interactive and non-interactive shells and scripts</H1><P>An <ICLASS="FIRSTTERM">interactive</I> shell reads	   commands from user input on a <TTCLASS="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 <ICLASS="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	   <TTCLASS="FILENAME">tty</TT>. It is even possible to emulate an	   interactive shell in a script.	   <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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;# St閜hane Chazelas (thanks again).</PRE></TD></TR></TABLE></P><P>Let us consider an <ICLASS="FIRSTTERM">interactive</I>	   script to be one that requires input from the user, usually	   with <AHREF="internal.html#READREF">read</A> statements (see <AHREF="internal.html#EX36">Example 14-3</A>). <SPANCLASS="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 <ICLASS="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 <BCLASS="COMMAND">expect</B>	   script or embedded <AHREF="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 <BCLASS="COMMAND">read</B> statement	   (<BCLASS="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 <ICLASS="FIRSTTERM">prompt</I> variable, <AHREF="variables2.html#PS1REF">$PS1</A> is set. (If the user is being	   prompted for input, then the script needs to display a prompt.)	   <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ -z $PS1 ] # no prompt?   2&nbsp;then   3&nbsp;  # non-interactive   4&nbsp;  ...   5&nbsp;else   6&nbsp;  # interactive   7&nbsp;  ...   8&nbsp;fi</PRE></TD></TR></TABLE>	   <ANAME="IITEST"></A>Alternatively, the script can test	   for the presence of option <SPANCLASS="QUOTE">"i"</SPAN> in the <AHREF="variables2.html#FLPREF">$-</A> flag.           <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Scripts may be forced to run in interactive	   mode with the <SPANCLASS="TOKEN">-i</SPAN> option or with a	   <TTCLASS="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><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="scrstyle.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="opprecedence.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Scripting With Style</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Operator Precedence</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -