📄 variables2.html
字号:
><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 E_WRONG_DIRECTORY=73 4 5 clear # Clear screen. 6 7 TargetDirectory=/home/bozo/projects/GreatAmericanNovel 8 9 cd $TargetDirectory 10 echo "Deleting stale files in $TargetDirectory." 11 12 if [ "$PWD" != "$TargetDirectory" ] 13 then # Keep from wiping out wrong directory by accident. 14 echo "Wrong directory!" 15 echo "In $PWD, rather than $TargetDirectory!" 16 echo "Bailing out!" 17 exit $E_WRONG_DIRECTORY 18 fi 19 20 rm -rf * 21 rm .[A-Za-z0-9]* # Delete dotfiles. 22 # rm -f .[^.]* ..?* to remove filenames beginning with multiple dots. 23 # (shopt -s dotglob; rm -f *) will also work. 24 # Thanks, S.C. for pointing this out. 25 26 # Filenames may contain all characters in the 0 - 255 range, except "/". 27 # Deleting files beginning with weird characters is left as an exercise. 28 29 # Various other operations here, as necessary. 30 31 echo 32 echo "Done." 33 echo "Old files deleted in $TargetDirectory." 34 echo 35 36 37 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 #!/bin/bash 2 # reply.sh 3 4 # REPLY is the default value for a 'read' command. 5 6 echo 7 echo -n "What is your favorite vegetable? " 8 read 9 10 echo "Your favorite vegetable is $REPLY." 11 # REPLY holds the value of last "read" if and only if 12 #+ no variable supplied. 13 14 echo 15 echo -n "What is your favorite fruit? " 16 read fruit 17 echo "Your favorite fruit is $fruit." 18 echo "but..." 19 echo "Value of \$REPLY is still $REPLY." 20 # $REPLY is still set to its previous value because 21 #+ the variable $fruit absorbed the new "read" value. 22 23 echo 24 25 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 #!/bin/bash 2 3 TIME_LIMIT=10 4 INTERVAL=1 5 6 echo 7 echo "Hit Control-C to exit before $TIME_LIMIT seconds." 8 echo 9 10 while [ "$SECONDS" -le "$TIME_LIMIT" ] 11 do 12 if [ "$SECONDS" -eq 1 ] 13 then 14 units=second 15 else 16 units=seconds 17 fi 18 19 echo "This script has been running $SECONDS $units." 20 # On a slow or overburdened machine, the script may skip a count 21 #+ every once in a while. 22 sleep $INTERVAL 23 done 24 25 echo -e "\a" # Beep! 26 27 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 # Works in scripts for Bash, versions 2.05b and later. 2 3 TMOUT=3 # Prompt times out at three seconds. 4 5 echo "What is your favorite song?" 6 echo "Quickly now, you only have $TMOUT seconds to answer!" 7 read song 8 9 if [ -z "$song" ] 10 then 11 song="(no answer)" 12 # Default response. 13 fi 14 15 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 #!/bin/bash 2 # timed-input.sh 3 4 # TMOUT=3 Also works, as of newer versions of Bash. 5 6 7 TIMELIMIT=3 # Three seconds in this instance. May be set to different value. 8 9 PrintAnswer() 10 { 11 if [ "$answer" = TIMEOUT ] 12 then 13 echo $answer 14 else # Don't want to mix up the two instances. 15 echo "Your favorite veggie is $answer" 16 kill $! # Kills no longer needed TimerOn function running in background. 17 # $! is PID of last job running in background. 18 fi 19 20 } 21 22 23 24 TimerOn() 25 { 26 sleep $TIMELIMIT && kill -s 14 $$ & 27 # Waits 3 seconds, then sends sigalarm to script. 28 } 29 30 Int14Vector() 31 { 32 answer="TIMEOUT" 33 PrintAnswer 34 exit 14 35 } 36 37 trap Int14Vector 14 # Timer interrupt (14) subverted for our purposes. 38 39 echo "What is your favorite vegetable " 40 TimerOn 41 read answer 42 PrintAnswer 43 44 45 # Admittedly, this is a kludgy implementation of timed input, 46 #+ however the "-t" option to "read" simplifies this task. 47 # See "t-out.sh", below. 48 49 # If you need something really elegant... 50 #+ consider writing the application in C or C++, 51 #+ using appropriate library functions, such as 'alarm' and 'setitimer'. 52 53 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 #!/bin/bash 2 # timeout.sh 3 4 # Written by Stephane Chazelas, 5 #+ and modified by the document author. 6 7 INTERVAL=5 # timeout interval 8 9 timedout_read() { 10 timeout=$1 11 varname=$2 12 old_tty_settings=`stty -g` 13 stty -icanon min 0 time ${timeout}0 14 eval read $varname # or just read $varname 15 stty "$old_tty_settings" 16 # See man page for "stty". 17 } 18 19 echo; echo -n "What's your name? Quick! " 20 timedout_read $INTERVAL your_name 21 22 # This may not work on every terminal type. 23 # The maximum timeout depends on the terminal. 24 #+ (it is often 25.5 seconds). 25 26 echo 27 28 if [ ! -z "$your_name" ] # If name input before timeout... 29 then 30 echo "Your name is $your_name." 31 else 32 echo "Timed out." 33 fi 34 35 echo 36 37 # The behavior of this script differs somewhat from "timed-input.sh". 38 # At each keystroke, the counter resets. 39 40 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 #!/bin/bash 2 # t-out.sh 3 # Inspired by a suggestion from "syngin seven" (thanks). 4 5 6 TIMELIMIT=4 # 4 seconds 7 8 read -t $TIMELIMIT variable <&1 9 # ^^^ 10 # In this instance, "<&1" is needed for Bash 1.x and 2.x, 11 # but unnecessary for Bash 3.x. 12 13 echo 14 15 if [ -z "$variable" ] # Is null? 16 then 17 echo "Timed out, variable still unset." 18 else 19 echo "variable = $variable" 20 fi 21 22 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 #!/bin/bash 2 # am-i-root.sh: Am I root or not? 3 4 ROOT_UID=0 # Root has $UID 0. 5 6 if [ "$UID" -eq "$ROOT_UID" ] # Will the real "root" please stand up? 7 then 8 echo "You are root." 9 else 10 echo "You are just an ordinary user (but mom loves you just the same)." 11 fi 12 13 exit 0 14 15 16 # ============================================================= # 17 # Code below will not execute, because the script already exited. 18 19 # An alternate method of getting to the root of matters: 20 21 ROOTUSER_NAME=root 22 23 username=`id -nu` # Or... username=`whoami` 24 if [ "$username" = "$ROOTUSER_NAME" ] 25 then 26 echo "Rooty, toot, toot. You are root." 27 else 28 echo "You are just a regular fella." 29 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 + -