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

📄 variables2.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 4 页
字号:
><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;E_WRONG_DIRECTORY=73   4&nbsp;   5&nbsp;clear # Clear screen.   6&nbsp;   7&nbsp;TargetDirectory=/home/bozo/projects/GreatAmericanNovel   8&nbsp;   9&nbsp;cd $TargetDirectory  10&nbsp;echo "Deleting stale files in $TargetDirectory."  11&nbsp;  12&nbsp;if [ "$PWD" != "$TargetDirectory" ]  13&nbsp;then    # Keep from wiping out wrong directory by accident.  14&nbsp;  echo "Wrong directory!"  15&nbsp;  echo "In $PWD, rather than $TargetDirectory!"  16&nbsp;  echo "Bailing out!"  17&nbsp;  exit $E_WRONG_DIRECTORY  18&nbsp;fi    19&nbsp;  20&nbsp;rm -rf *  21&nbsp;rm .[A-Za-z0-9]*    # Delete dotfiles.  22&nbsp;# rm -f .[^.]* ..?*   to remove filenames beginning with multiple dots.  23&nbsp;# (shopt -s dotglob; rm -f *)   will also work.  24&nbsp;# Thanks, S.C. for pointing this out.  25&nbsp;  26&nbsp;# Filenames may contain all characters in the 0 - 255 range, except "/".  27&nbsp;# Deleting files beginning with weird characters is left as an exercise.  28&nbsp;  29&nbsp;# Various other operations here, as necessary.  30&nbsp;  31&nbsp;echo  32&nbsp;echo "Done."  33&nbsp;echo "Old files deleted in $TargetDirectory."  34&nbsp;echo  35&nbsp;  36&nbsp;  37&nbsp;exit 0</PRE></TD></TR></TABLE></P></DD><DT><ANAME="REPLYREF"></A><TTCLASS="VARNAME">$REPLY</TT></DT><DD><P>The default value when a variable is not	    supplied to <AHREF="internal.html#READREF">read</A>. Also	    applicable to <AHREF="testbranch.html#SELECTREF">select</A> menus,	    but only supplies the item number of the variable chosen,	    not the value of the variable itself.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# reply.sh   3&nbsp;   4&nbsp;# REPLY is the default value for a 'read' command.   5&nbsp;   6&nbsp;echo   7&nbsp;echo -n "What is your favorite vegetable? "   8&nbsp;read   9&nbsp;  10&nbsp;echo "Your favorite vegetable is $REPLY."  11&nbsp;#  REPLY holds the value of last "read" if and only if  12&nbsp;#+ no variable supplied.  13&nbsp;  14&nbsp;echo  15&nbsp;echo -n "What is your favorite fruit? "  16&nbsp;read fruit  17&nbsp;echo "Your favorite fruit is $fruit."  18&nbsp;echo "but..."  19&nbsp;echo "Value of \$REPLY is still $REPLY."  20&nbsp;#  $REPLY is still set to its previous value because  21&nbsp;#+ the variable $fruit absorbed the new "read" value.  22&nbsp;  23&nbsp;echo  24&nbsp;  25&nbsp;exit 0</PRE></TD></TR></TABLE></P></DD><DT><TTCLASS="VARNAME">$SECONDS</TT></DT><DD><P>The number of seconds the script has been running.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;TIME_LIMIT=10   4&nbsp;INTERVAL=1   5&nbsp;   6&nbsp;echo   7&nbsp;echo "Hit Control-C to exit before $TIME_LIMIT seconds."   8&nbsp;echo   9&nbsp;  10&nbsp;while [ "$SECONDS" -le "$TIME_LIMIT" ]  11&nbsp;do  12&nbsp;  if [ "$SECONDS" -eq 1 ]  13&nbsp;  then  14&nbsp;    units=second  15&nbsp;  else    16&nbsp;    units=seconds  17&nbsp;  fi  18&nbsp;  19&nbsp;  echo "This script has been running $SECONDS $units."  20&nbsp;  #  On a slow or overburdened machine, the script may skip a count  21&nbsp;  #+ every once in a while.  22&nbsp;  sleep $INTERVAL  23&nbsp;done  24&nbsp;  25&nbsp;echo -e "\a"  # Beep!  26&nbsp;  27&nbsp;exit 0</PRE></TD></TR></TABLE></P></DD><DT><TTCLASS="VARNAME">$SHELLOPTS</TT></DT><DD><P>the list of enabled shell <AHREF="options.html#OPTIONSREF">options</A>, a readonly variable	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $SHELLOPTS</B></TT> <TTCLASS="COMPUTEROUTPUT">braceexpand:hashall:histexpand:monitor:history:interactive-comments:emacs</TT> 	      </PRE></TD></TR></TABLE>	    </P></DD><DT><ANAME="SHLVLREF"></A><TTCLASS="VARNAME">$SHLVL</TT></DT><DD><P>Shell level, how deeply Bash is nested.	      <ANAME="AEN4581"HREF="#FTN.AEN4581">[2]</A>	    If, at the command line, $SHLVL is 1, then in a script it	    will increment to 2.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>This variable is <AHREF="subshells.html#SUBSHNLEVREF">	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> affected by	      subshells</A>. Use <AHREF="variables2.html#BASHSUBSHELLREF">$BASH_SUBSHELL</A> when you	      need an indication of subshell nesting.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="TMOUTREF"></A><TTCLASS="VARNAME">$TMOUT</TT></DT><DD><P>If the <TTCLASS="REPLACEABLE"><I>$TMOUT</I></TT>	  environmental variable is set to a non-zero value	  <TTCLASS="VARNAME">time</TT>, then the shell prompt will time out	  after <TTCLASS="VARNAME">$time</TT> seconds. This will cause a	  logout.</P><P>As of version 2.05b of Bash, it is now possible to use	    <TTCLASS="REPLACEABLE"><I>$TMOUT</I></TT> in a script in combination	    with <AHREF="internal.html#READREF">read</A>.</P><P>	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Works in scripts for Bash, versions 2.05b and later.   2&nbsp;   3&nbsp;TMOUT=3    # Prompt times out at three seconds.   4&nbsp;   5&nbsp;echo "What is your favorite song?"   6&nbsp;echo "Quickly now, you only have $TMOUT seconds to answer!"   7&nbsp;read song   8&nbsp;   9&nbsp;if [ -z "$song" ]  10&nbsp;then  11&nbsp;  song="(no answer)"  12&nbsp;  # Default response.  13&nbsp;fi  14&nbsp;  15&nbsp;echo "Your favorite song is $song."</PRE></TD></TR></TABLE>	  </P><P><ANAME="TIMINGLOOP"></A></P><P>There are other, more complex, ways of implementing	    timed input in a script. One alternative is to set up	    a timing loop to signal the script when it times out.	    This also requires a signal handling routine to trap (see	    <AHREF="debugging.html#EX76">Example 29-5</A>) the interrupt generated by the timing	    loop (whew!).</P><DIVCLASS="EXAMPLE"><HR><ANAME="TMDIN"></A><P><B>Example 9-2. Timed Input</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# timed-input.sh   3&nbsp;   4&nbsp;# TMOUT=3    Also works, as of newer versions of Bash.   5&nbsp;   6&nbsp;   7&nbsp;TIMELIMIT=3  # Three seconds in this instance. May be set to different value.   8&nbsp;   9&nbsp;PrintAnswer()  10&nbsp;{  11&nbsp;  if [ "$answer" = TIMEOUT ]  12&nbsp;  then  13&nbsp;    echo $answer  14&nbsp;  else       # Don't want to mix up the two instances.   15&nbsp;    echo "Your favorite veggie is $answer"  16&nbsp;    kill $!  # Kills no longer needed TimerOn function running in background.  17&nbsp;             # $! is PID of last job running in background.  18&nbsp;  fi  19&nbsp;  20&nbsp;}    21&nbsp;  22&nbsp;  23&nbsp;  24&nbsp;TimerOn()  25&nbsp;{  26&nbsp;  sleep $TIMELIMIT &#38;&#38; kill -s 14 $$ &#38;  27&nbsp;  # Waits 3 seconds, then sends sigalarm to script.  28&nbsp;}    29&nbsp;  30&nbsp;Int14Vector()  31&nbsp;{  32&nbsp;  answer="TIMEOUT"  33&nbsp;  PrintAnswer  34&nbsp;  exit 14  35&nbsp;}    36&nbsp;  37&nbsp;trap Int14Vector 14   # Timer interrupt (14) subverted for our purposes.  38&nbsp;  39&nbsp;echo "What is your favorite vegetable "  40&nbsp;TimerOn  41&nbsp;read answer  42&nbsp;PrintAnswer  43&nbsp;  44&nbsp;  45&nbsp;#  Admittedly, this is a kludgy implementation of timed input,  46&nbsp;#+ however the "-t" option to "read" simplifies this task.  47&nbsp;#  See "t-out.sh", below.  48&nbsp;  49&nbsp;#  If you need something really elegant...  50&nbsp;#+ consider writing the application in C or C++,  51&nbsp;#+ using appropriate library functions, such as 'alarm' and 'setitimer'.  52&nbsp;  53&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="STTYTO"></A></P><P>An alternative is using <AHREF="system.html#STTYREF">stty</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="TIMEOUT"></A><P><B>Example 9-3. Once more, timed input</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# timeout.sh   3&nbsp;   4&nbsp;#  Written by Stephane Chazelas,   5&nbsp;#+ and modified by the document author.   6&nbsp;   7&nbsp;INTERVAL=5                # timeout interval   8&nbsp;   9&nbsp;timedout_read() {  10&nbsp;  timeout=$1  11&nbsp;  varname=$2  12&nbsp;  old_tty_settings=`stty -g`  13&nbsp;  stty -icanon min 0 time ${timeout}0  14&nbsp;  eval read $varname      # or just  read $varname  15&nbsp;  stty "$old_tty_settings"  16&nbsp;  # See man page for "stty".  17&nbsp;}  18&nbsp;  19&nbsp;echo; echo -n "What's your name? Quick! "  20&nbsp;timedout_read $INTERVAL your_name  21&nbsp;  22&nbsp;#  This may not work on every terminal type.  23&nbsp;#  The maximum timeout depends on the terminal.  24&nbsp;#+ (it is often 25.5 seconds).  25&nbsp;  26&nbsp;echo  27&nbsp;  28&nbsp;if [ ! -z "$your_name" ]  # If name input before timeout...  29&nbsp;then  30&nbsp;  echo "Your name is $your_name."  31&nbsp;else  32&nbsp;  echo "Timed out."  33&nbsp;fi  34&nbsp;  35&nbsp;echo  36&nbsp;  37&nbsp;# The behavior of this script differs somewhat from "timed-input.sh".  38&nbsp;# At each keystroke, the counter resets.  39&nbsp;  40&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Perhaps the simplest method is using the	    <TTCLASS="OPTION">-t</TT> option to <AHREF="internal.html#READREF">read</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="TOUT"></A><P><B>Example 9-4. Timed <ICLASS="FIRSTTERM">read</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# t-out.sh   3&nbsp;# Inspired by a suggestion from "syngin seven" (thanks).   4&nbsp;   5&nbsp;   6&nbsp;TIMELIMIT=4         # 4 seconds   7&nbsp;   8&nbsp;read -t $TIMELIMIT variable &#60;&#38;1   9&nbsp;#                           ^^^  10&nbsp;#  In this instance, "&#60;&#38;1" is needed for Bash 1.x and 2.x,  11&nbsp;#  but unnecessary for Bash 3.x.  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;if [ -z "$variable" ]  # Is null?  16&nbsp;then  17&nbsp;  echo "Timed out, variable still unset."  18&nbsp;else    19&nbsp;  echo "variable = $variable"  20&nbsp;fi    21&nbsp;  22&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="UIDREF"></A><TTCLASS="VARNAME">$UID</TT></DT><DD><P>user ID number</P><P>current user's user identification number, as	      recorded in <TTCLASS="FILENAME">/etc/passwd</TT>	    </P><P>This is the current user's real id, even if she has	      temporarily assumed another identity through <AHREF="system.html#SUREF">su</A>. <TTCLASS="VARNAME">$UID</TT> is a	      readonly variable, not subject to change from the command	      line or within a script, and is the counterpart to the	      <AHREF="system.html#IDREF">id</A> builtin.</P><DIVCLASS="EXAMPLE"><HR><ANAME="AMIROOT"></A><P><B>Example 9-5. Am I root?</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# am-i-root.sh:   Am I root or not?   3&nbsp;   4&nbsp;ROOT_UID=0   # Root has $UID 0.   5&nbsp;   6&nbsp;if [ "$UID" -eq "$ROOT_UID" ]  # Will the real "root" please stand up?   7&nbsp;then   8&nbsp;  echo "You are root."   9&nbsp;else  10&nbsp;  echo "You are just an ordinary user (but mom loves you just the same)."  11&nbsp;fi  12&nbsp;  13&nbsp;exit 0  14&nbsp;  15&nbsp;  16&nbsp;# ============================================================= #  17&nbsp;# Code below will not execute, because the script already exited.  18&nbsp;  19&nbsp;# An alternate method of getting to the root of matters:  20&nbsp;  21&nbsp;ROOTUSER_NAME=root  22&nbsp;  23&nbsp;username=`id -nu`              # Or...   username=`whoami`  24&nbsp;if [ "$username" = "$ROOTUSER_NAME" ]  25&nbsp;then  26&nbsp;  echo "Rooty, toot, toot. You are root."  27&nbsp;else  28&nbsp;  echo "You are just a regular fella."  29&nbsp;fi</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="sha-bang.html#EX2">Example 2-3</A>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The variables <TTCLASS="VARNAME">$ENV</TT>,	  <TTCLASS="VARNAME">$LOGNAME</TT>, <TTCLASS="VARNAME">$MAIL</TT>,	  <TTCLASS="VARNAME">$TERM</TT>, <TTCLASS="VARNAME">$USER</TT>, and	  <TTCLASS="VARNAME">$USERNAME</TT> are <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN>	  Bash <AHREF="internal.html#BUILTINREF">builtins</A>. These are,	  however, often set as <AHREF="othertypesv.html#ENVREF">environmental	  variables</A> in one of the Bash <AHREF="files.html#FILESREF1">startup files</A>.  <ANAME="SHELLVARREF"></A><TTCLASS="VARNAME">$SHELL</TT>,	  the name of the user's login shell, may be set from	  <TTCLASS="FILENAME">/etc/passwd</TT> or in an <SPANCLASS="QUOTE">"init"</SPAN>	  script, and it is likewise not a Bash builtin.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">tcsh% </TT><TTCLASS="USERINPUT"><B>echo $LOGNAME</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT> <TTCLASS="PROMPT">tcsh% </TT><TTCLASS="USERINPUT"><B>echo $SHELL</B></TT> <TTCLASS="COMPUTEROUTPUT">/bin/tcsh</TT> <TTCLASS="PROMPT">tcsh% </TT><TTCLASS="USERINPUT"><B>echo $TERM</B></TT> <TTCLASS="COMPUTEROUTPUT">rxvt</TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $LOGNAME</B></TT> <TTCLASS="COMPUTEROUTPUT">bozo</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $SHELL</B></TT> <TTCLASS="COMPUTEROUTPUT">/bin/tcsh</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $TERM</B></TT> <TTCLASS="COMPUTEROUTPUT">rxvt</TT> 	      </PRE></TD></TR></TABLE>	      </P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B>Positional Parameters</B></P><DL><DT><ANAME="POSPARAMREF"></A><TTCLASS="VARNAME">$0</TT>, <TTCLASS="VARNAME">$1</TT>,	  <TTCLASS="VARNAME">$2</TT>, etc.</DT><DD><P>positional parameters, passed from command	      line to script, passed to a function, or <AHREF="internal.html#SETREF">set</A> to a variable (see <AHREF="othertypesv.html#EX17">Example 4-5</A> and <AHREF="internal.html#EX34">Example 14-16</A>)</P></DD><DT><ANAME="CLACOUNTREF"></A><TTCLASS="VARNAME">$#</TT></DT><DD><P>number of command line arguments		<ANAME="AEN4737"HREF="#FTN.AEN4737">[3]</A>	    or positional parameters (see <AHREF="wrapper.html#EX4">Example 33-2</A>)</P></DD><DT><ANAME="APPREF"></A><TTCLASS="VARNAME">$*</TT></DT><DD><P>All of the positional parameters, seen as a single word</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><SPANCLASS="QUOTE">"<TTCLASS="VARNAME">$*</TT>"</SPAN> must be	    quoted.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="APPREF2"></A><TTCLASS="VARNAME">$@</TT></DT><DD><P>Same as <SPANCLASS="TOKEN">$*</SPAN>, but each parameter is a	      quoted string, that is, the parameters are passed on	      intact, without interpretation or expansion. This means,

⌨️ 快捷键说明

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