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

📄 internal.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
><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 command is a shell builtin, and not the same as	      <TTCLASS="FILENAME">/bin/echo</TT>, although its behavior is	      similar.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>type -a echo</B></TT> <TTCLASS="COMPUTEROUTPUT">echo is a shell builtin echo is /bin/echo</TT> 	      </PRE></TD></TR></TABLE>	  </P></TD></TR></TABLE></DIV></DD><DT><ANAME="PRINTFREF"></A><BCLASS="COMMAND">printf</B></DT><DD><P>The <BCLASS="COMMAND">printf</B>, formatted print, command is an	      enhanced <BCLASS="COMMAND">echo</B>.  It is a limited variant	      of the C language <TTCLASS="FUNCTION">printf()</TT> library	      function, and its syntax is somewhat different.</P><P><BCLASS="COMMAND">printf</B>   <TTCLASS="REPLACEABLE"><I>format-string</I></TT>...   <TTCLASS="REPLACEABLE"><I>parameter</I></TT>... </P><P>This is the Bash builtin version	      of the <TTCLASS="FILENAME">/bin/printf</TT> or	      <TTCLASS="FILENAME">/usr/bin/printf</TT> command. See the	      <BCLASS="COMMAND">printf</B> <AHREF="external.html#MANREF">manpage</A> (of the system command)	      for in-depth coverage.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Older versions of Bash may not support	      <BCLASS="COMMAND">printf</B>.</P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX47"></A><P><B>Example 14-2. <ICLASS="FIRSTTERM">printf</I> in action</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# printf demo   3&nbsp;   4&nbsp;PI=3.14159265358979   5&nbsp;DecimalConstant=31373   6&nbsp;Message1="Greetings,"   7&nbsp;Message2="Earthling."   8&nbsp;   9&nbsp;echo  10&nbsp;  11&nbsp;printf "Pi to 2 decimal places = %1.2f" $PI  12&nbsp;echo  13&nbsp;printf "Pi to 9 decimal places = %1.9f" $PI  # It even rounds off correctly.  14&nbsp;  15&nbsp;printf "\n"                                  # Prints a line feed,  16&nbsp;                                             # Equivalent to 'echo' . . .  17&nbsp;  18&nbsp;printf "Constant = \t%d\n" $DecimalConstant  # Inserts tab (\t).  19&nbsp;  20&nbsp;printf "%s %s \n" $Message1 $Message2  21&nbsp;  22&nbsp;echo  23&nbsp;  24&nbsp;# ==========================================#  25&nbsp;# Simulation of C function, sprintf().  26&nbsp;# Loading a variable with a formatted string.  27&nbsp;  28&nbsp;echo   29&nbsp;  30&nbsp;Pi12=$(printf "%1.12f" $PI)  31&nbsp;echo "Pi to 12 decimal places = $Pi12"      # Roundoff error!  32&nbsp;  33&nbsp;Msg=`printf "%s %s \n" $Message1 $Message2`  34&nbsp;echo $Msg; echo $Msg  35&nbsp;  36&nbsp;#  As it happens, the 'sprintf' function can now be accessed  37&nbsp;#+ as a loadable module to Bash,  38&nbsp;#+ but this is not portable.  39&nbsp;  40&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Formatting error messages is a useful application of	      <BCLASS="COMMAND">printf</B></P><P>	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;E_BADDIR=65   2&nbsp;   3&nbsp;var=nonexistent_directory   4&nbsp;   5&nbsp;error()   6&nbsp;{   7&nbsp;  printf "$@" &#62;&#38;2   8&nbsp;  # Formats positional params passed, and sends them to stderr.   9&nbsp;  echo  10&nbsp;  exit $E_BADDIR  11&nbsp;}  12&nbsp;  13&nbsp;cd $var || error $"Can't cd to %s." "$var"  14&nbsp;  15&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE>            </P></DD><DT><ANAME="READREF"></A><BCLASS="COMMAND">read</B></DT><DD><P><SPANCLASS="QUOTE">"Reads"</SPAN> the value	    of a variable from <TTCLASS="FILENAME">stdin</TT>, that	    is, interactively fetches input from the keyboard. The	    <TTCLASS="OPTION">-a</TT> option lets <BCLASS="COMMAND">read</B>	    get array variables (see <AHREF="arrays.html#EX67">Example 26-6</A>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX36"></A><P><B>Example 14-3. Variable assignment, using <ICLASS="FIRSTTERM">read</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# "Reading" variables.   3&nbsp;   4&nbsp;echo -n "Enter the value of variable 'var1': "   5&nbsp;# The -n option to echo suppresses newline.   6&nbsp;   7&nbsp;read var1   8&nbsp;# Note no '$' in front of var1, since it is being set.   9&nbsp;  10&nbsp;echo "var1 = $var1"  11&nbsp;  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;# A single 'read' statement can set multiple variables.  16&nbsp;echo -n "Enter the values of variables 'var2' and 'var3' "  17&nbsp;echo =n "(separated by a space or tab): "  18&nbsp;read var2 var3  19&nbsp;echo "var2 = $var2      var3 = $var3"  20&nbsp;#  If you input only one value,  21&nbsp;#+ the other variable(s) will remain unset (null).  22&nbsp;  23&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>A <BCLASS="COMMAND">read</B> without an associated variable	      assigns its input to the dedicated variable <AHREF="variables2.html#REPLYREF">$REPLY</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="READNOVAR"></A><P><B>Example 14-4. What happens when <ICLASS="FIRSTTERM">read</I> has no	        variable</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# read-novar.sh   3&nbsp;   4&nbsp;echo   5&nbsp;   6&nbsp;# -------------------------- #   7&nbsp;echo -n "Enter a value: "   8&nbsp;read var   9&nbsp;echo "\"var\" = "$var""  10&nbsp;# Everything as expected here.  11&nbsp;# -------------------------- #  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;# ------------------------------------------------------------------- #  16&nbsp;echo -n "Enter another value: "  17&nbsp;read           #  No variable supplied for 'read', therefore...  18&nbsp;               #+ Input to 'read' assigned to default variable, $REPLY.  19&nbsp;var="$REPLY"  20&nbsp;echo "\"var\" = "$var""  21&nbsp;# This is equivalent to the first code block.  22&nbsp;# ------------------------------------------------------------------- #  23&nbsp;  24&nbsp;echo  25&nbsp;echo "========================="  26&nbsp;echo  27&nbsp;  28&nbsp;  29&nbsp;#  This example is similar to the "reply.sh" script.  30&nbsp;#  However, this one shows that $REPLY is available  31&nbsp;#+ even after a 'read' to a variable in the conventional way.  32&nbsp;  33&nbsp;  34&nbsp;# ================================================================= #  35&nbsp;  36&nbsp;#  In some instances, you might wish to discard the first value read.  37&nbsp;#  In such cases, simply ignore the $REPLY variable.  38&nbsp;  39&nbsp;{ # Code block.  40&nbsp;read            # Line 1, to be discarded.  41&nbsp;read line2      # Line 2, saved in variable.  42&nbsp;  } &#60;$0  43&nbsp;echo "Line 2 of this script is:"  44&nbsp;echo "$line2"   #   # read-novar.sh  45&nbsp;echo            #   #!/bin/bash  line discarded.  46&nbsp;  47&nbsp;# See also the soundcard-on.sh script.  48&nbsp;  49&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Normally, inputting a <TTCLASS="USERINPUT"><B>\</B></TT>	      suppresses a newline during input to	      a <BCLASS="COMMAND">read</B>. The <TTCLASS="OPTION">-r</TT>	      option causes an inputted <TTCLASS="USERINPUT"><B>\</B></TT> to be	      interpreted literally.</P><DIVCLASS="EXAMPLE"><HR><ANAME="READR"></A><P><B>Example 14-5. Multi-line input to <ICLASS="FIRSTTERM">read</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo   4&nbsp;   5&nbsp;echo "Enter a string terminated by a \\, then press &#60;ENTER&#62;."   6&nbsp;echo "Then, enter a second string (no \\ this time), and again press &#60;ENTER&#62;."   7&nbsp;   8&nbsp;read var1     # The "\" suppresses the newline, when reading $var1.   9&nbsp;              #     first line \  10&nbsp;              #     second line  11&nbsp;  12&nbsp;echo "var1 = $var1"  13&nbsp;#     var1 = first line second line  14&nbsp;  15&nbsp;#  For each line terminated by a "\"  16&nbsp;#+ you get a prompt on the next line to continue feeding characters into var1.  17&nbsp;  18&nbsp;echo; echo  19&nbsp;  20&nbsp;echo "Enter another string terminated by a \\ , then press &#60;ENTER&#62;."  21&nbsp;read -r var2  # The -r option causes the "\" to be read literally.  22&nbsp;              #     first line \  23&nbsp;  24&nbsp;echo "var2 = $var2"  25&nbsp;#     var2 = first line \  26&nbsp;  27&nbsp;# Data entry terminates with the first &#60;ENTER&#62;.  28&nbsp;  29&nbsp;echo   30&nbsp;  31&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="READOPTIONS"></A></P><P>The <BCLASS="COMMAND">read</B> command has some interesting	      options that permit echoing a prompt and even reading keystrokes	      without hitting <BCLASS="KEYCAP">ENTER</B>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Read a keypress without hitting ENTER.   2&nbsp;   3&nbsp;read -s -n1 -p "Hit a key " keypress   4&nbsp;echo; echo "Keypress was "\"$keypress\""."   5&nbsp;   6&nbsp;# -s option means do not echo input.   7&nbsp;# -n N option means accept only N characters of input.   8&nbsp;# -p option means echo the following prompt before reading input.   9&nbsp;  10&nbsp;# Using these options is tricky, since they need to be in the correct order.</PRE></TD></TR></TABLE></P><P><ANAME="READARROW"></A></P><P>The <TTCLASS="OPTION">-n</TT> option to <BCLASS="COMMAND">read</B>	      also allows detection of the <BCLASS="KEYCAP">arrow keys</B>	      and certain of the other unusual keys.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARROWDETECT"></A><P><B>Example 14-6. Detecting the arrow keys</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# arrow-detect.sh: Detects the arrow keys, and a few more.   3&nbsp;# Thank you, Sandro Magi, for showing me how.   4&nbsp;   5&nbsp;# --------------------------------------------   6&nbsp;# Character codes generated by the keypresses.   7&nbsp;arrowup='\[A'   8&nbsp;arrowdown='\[B'   9&nbsp;arrowrt='\[C'  10&nbsp;arrowleft='\[D'  11&nbsp;insert='\[2'  12&nbsp;delete='\[3'  13&nbsp;# --------------------------------------------  14&nbsp;  15&nbsp;SUCCESS=0  16&nbsp;OTHER=65  17&nbsp;  18&nbsp;echo -n "Press a key...  "  19&nbsp;# May need to also press ENTER if a key not listed above pressed.  20&nbsp;read -n3 key                      # Read 3 characters.  21&nbsp;  22&nbsp;echo -n "$key" | grep "$arrowup"  #Check if character code detected.  23&nbsp;if [ "$?" -eq $SUCCESS ]  24&nbsp;then  25&nbsp;  echo "Up-arrow key pressed."  26&nbsp;  exit $SUCCESS  27&nbsp;fi  28&nbsp;  29&nbsp;echo -n "$key" | grep "$arrowdown"  30&nbsp;if [ "$?" -eq $SUCCESS ]  31&nbsp;then  32&nbsp;  echo "Down-arrow key pressed."  33&nbsp;  exit $SUCCESS  34&nbsp;fi  35&nbsp;  36&nbsp;echo -n "$key" | grep "$arrowrt"  37&nbsp;if [ "$?" -eq $SUCCESS ]  38&nbsp;then  39&nbsp;  echo "Right-arrow key pressed."  40&nbsp;  exit $SUCCESS  41&nbsp;fi  42&nbsp;  43&nbsp;echo -n "$key" | grep "$arrowleft"  44&nbsp;if [ "$?" -eq $SUCCESS ]  45&nbsp;then  46&nbsp;  echo "Left-arrow key pressed."  47&nbsp;  exit $SUCCESS  48&nbsp;fi  49&nbsp;  50&nbsp;echo -n "$key" | grep "$insert"  51&nbsp;if [ "$?" -eq $SUCCESS ]  52&nbsp;then  53&nbsp;  echo "\"Insert\" key pressed."  54&nbsp;  exit $SUCCESS  55&nbsp;fi  56&nbsp;  57&nbsp;echo -n "$key" | grep "$delete"  58&nbsp;if [ "$?" -eq $SUCCESS ]  59&nbsp;then  60&nbsp;  echo "\"Delete\" key pressed."  61&nbsp;  exit $SUCCESS  62&nbsp;fi  63&nbsp;  64&nbsp;  65&nbsp;echo " Some other key pressed."  66&nbsp;  67&nbsp;exit $OTHER  68&nbsp;  69&nbsp;# ========================================= #  70&nbsp;  71&nbsp;#  Mark Alexander came up with a simplified  72&nbsp;#+ version of the above script (Thank you!).  73&nbsp;#  It eliminates the need for grep.  74&nbsp;  75&nbsp;#!/bin/bash  76&nbsp;  77&nbsp;  uparrow=$'\x1b[A'  78&nbsp;  downarrow=$'\x1b[B'  79&nbsp;  leftarrow=$'\x1b[D'  80&nbsp;  rightarrow=$'\x1b[C'  81&nbsp;  82&nbsp;  read -s -n3 -p "Hit an arrow key: " x  83&nbsp;  84&nbsp;  case "$x" in  85&nbsp;  $uparrow)  86&nbsp;     echo "You pressed up-arrow"  87&nbsp;     ;;  88&nbsp;  $downarrow)  89&nbsp;     echo "You pressed down-arrow"  90&nbsp;     ;;  91&nbsp;  $leftarrow)  92&nbsp;     echo "You pressed left-arrow"  93&nbsp;     ;;  94&nbsp;  $rightarrow)  95&nbsp;     echo "You pressed right-arrow"  96&nbsp;     ;;  97&nbsp;  esac  98&nbsp;  99&nbsp;# ========================================= # 100&nbsp; 101&nbsp;#  Exercise: 102&nbsp;#  -------- 103&nbsp;#  1) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.</PRE></TD></TR></TABLE><HR></DIV><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 <TTCLASS="OPTION">-n</TT> option to <BCLASS="COMMAND">read</B>	      will not detect the <BCLASS="KEYCAP">ENTER</B> (newline)	      key.</P></TD></TR></TABLE></DIV><P

⌨️ 快捷键说明

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