📄 internal.html
字号:
><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 #!/bin/bash 2 # printf demo 3 4 PI=3.14159265358979 5 DecimalConstant=31373 6 Message1="Greetings," 7 Message2="Earthling." 8 9 echo 10 11 printf "Pi to 2 decimal places = %1.2f" $PI 12 echo 13 printf "Pi to 9 decimal places = %1.9f" $PI # It even rounds off correctly. 14 15 printf "\n" # Prints a line feed, 16 # Equivalent to 'echo' . . . 17 18 printf "Constant = \t%d\n" $DecimalConstant # Inserts tab (\t). 19 20 printf "%s %s \n" $Message1 $Message2 21 22 echo 23 24 # ==========================================# 25 # Simulation of C function, sprintf(). 26 # Loading a variable with a formatted string. 27 28 echo 29 30 Pi12=$(printf "%1.12f" $PI) 31 echo "Pi to 12 decimal places = $Pi12" # Roundoff error! 32 33 Msg=`printf "%s %s \n" $Message1 $Message2` 34 echo $Msg; echo $Msg 35 36 # As it happens, the 'sprintf' function can now be accessed 37 #+ as a loadable module to Bash, 38 #+ but this is not portable. 39 40 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 E_BADDIR=65 2 3 var=nonexistent_directory 4 5 error() 6 { 7 printf "$@" >&2 8 # Formats positional params passed, and sends them to stderr. 9 echo 10 exit $E_BADDIR 11 } 12 13 cd $var || error $"Can't cd to %s." "$var" 14 15 # 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 #!/bin/bash 2 # "Reading" variables. 3 4 echo -n "Enter the value of variable 'var1': " 5 # The -n option to echo suppresses newline. 6 7 read var1 8 # Note no '$' in front of var1, since it is being set. 9 10 echo "var1 = $var1" 11 12 13 echo 14 15 # A single 'read' statement can set multiple variables. 16 echo -n "Enter the values of variables 'var2' and 'var3' " 17 echo =n "(separated by a space or tab): " 18 read var2 var3 19 echo "var2 = $var2 var3 = $var3" 20 # If you input only one value, 21 #+ the other variable(s) will remain unset (null). 22 23 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 #!/bin/bash 2 # read-novar.sh 3 4 echo 5 6 # -------------------------- # 7 echo -n "Enter a value: " 8 read var 9 echo "\"var\" = "$var"" 10 # Everything as expected here. 11 # -------------------------- # 12 13 echo 14 15 # ------------------------------------------------------------------- # 16 echo -n "Enter another value: " 17 read # No variable supplied for 'read', therefore... 18 #+ Input to 'read' assigned to default variable, $REPLY. 19 var="$REPLY" 20 echo "\"var\" = "$var"" 21 # This is equivalent to the first code block. 22 # ------------------------------------------------------------------- # 23 24 echo 25 echo "=========================" 26 echo 27 28 29 # This example is similar to the "reply.sh" script. 30 # However, this one shows that $REPLY is available 31 #+ even after a 'read' to a variable in the conventional way. 32 33 34 # ================================================================= # 35 36 # In some instances, you might wish to discard the first value read. 37 # In such cases, simply ignore the $REPLY variable. 38 39 { # Code block. 40 read # Line 1, to be discarded. 41 read line2 # Line 2, saved in variable. 42 } <$0 43 echo "Line 2 of this script is:" 44 echo "$line2" # # read-novar.sh 45 echo # #!/bin/bash line discarded. 46 47 # See also the soundcard-on.sh script. 48 49 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 #!/bin/bash 2 3 echo 4 5 echo "Enter a string terminated by a \\, then press <ENTER>." 6 echo "Then, enter a second string (no \\ this time), and again press <ENTER>." 7 8 read var1 # The "\" suppresses the newline, when reading $var1. 9 # first line \ 10 # second line 11 12 echo "var1 = $var1" 13 # var1 = first line second line 14 15 # For each line terminated by a "\" 16 #+ you get a prompt on the next line to continue feeding characters into var1. 17 18 echo; echo 19 20 echo "Enter another string terminated by a \\ , then press <ENTER>." 21 read -r var2 # The -r option causes the "\" to be read literally. 22 # first line \ 23 24 echo "var2 = $var2" 25 # var2 = first line \ 26 27 # Data entry terminates with the first <ENTER>. 28 29 echo 30 31 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 # Read a keypress without hitting ENTER. 2 3 read -s -n1 -p "Hit a key " keypress 4 echo; echo "Keypress was "\"$keypress\""." 5 6 # -s option means do not echo input. 7 # -n N option means accept only N characters of input. 8 # -p option means echo the following prompt before reading input. 9 10 # 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 #!/bin/bash 2 # arrow-detect.sh: Detects the arrow keys, and a few more. 3 # Thank you, Sandro Magi, for showing me how. 4 5 # -------------------------------------------- 6 # Character codes generated by the keypresses. 7 arrowup='\[A' 8 arrowdown='\[B' 9 arrowrt='\[C' 10 arrowleft='\[D' 11 insert='\[2' 12 delete='\[3' 13 # -------------------------------------------- 14 15 SUCCESS=0 16 OTHER=65 17 18 echo -n "Press a key... " 19 # May need to also press ENTER if a key not listed above pressed. 20 read -n3 key # Read 3 characters. 21 22 echo -n "$key" | grep "$arrowup" #Check if character code detected. 23 if [ "$?" -eq $SUCCESS ] 24 then 25 echo "Up-arrow key pressed." 26 exit $SUCCESS 27 fi 28 29 echo -n "$key" | grep "$arrowdown" 30 if [ "$?" -eq $SUCCESS ] 31 then 32 echo "Down-arrow key pressed." 33 exit $SUCCESS 34 fi 35 36 echo -n "$key" | grep "$arrowrt" 37 if [ "$?" -eq $SUCCESS ] 38 then 39 echo "Right-arrow key pressed." 40 exit $SUCCESS 41 fi 42 43 echo -n "$key" | grep "$arrowleft" 44 if [ "$?" -eq $SUCCESS ] 45 then 46 echo "Left-arrow key pressed." 47 exit $SUCCESS 48 fi 49 50 echo -n "$key" | grep "$insert" 51 if [ "$?" -eq $SUCCESS ] 52 then 53 echo "\"Insert\" key pressed." 54 exit $SUCCESS 55 fi 56 57 echo -n "$key" | grep "$delete" 58 if [ "$?" -eq $SUCCESS ] 59 then 60 echo "\"Delete\" key pressed." 61 exit $SUCCESS 62 fi 63 64 65 echo " Some other key pressed." 66 67 exit $OTHER 68 69 # ========================================= # 70 71 # Mark Alexander came up with a simplified 72 #+ version of the above script (Thank you!). 73 # It eliminates the need for grep. 74 75 #!/bin/bash 76 77 uparrow=$'\x1b[A' 78 downarrow=$'\x1b[B' 79 leftarrow=$'\x1b[D' 80 rightarrow=$'\x1b[C' 81 82 read -s -n3 -p "Hit an arrow key: " x 83 84 case "$x" in 85 $uparrow) 86 echo "You pressed up-arrow" 87 ;; 88 $downarrow) 89 echo "You pressed down-arrow" 90 ;; 91 $leftarrow) 92 echo "You pressed left-arrow" 93 ;; 94 $rightarrow) 95 echo "You pressed right-arrow" 96 ;; 97 esac 98 99 # ========================================= # 100 101 # Exercise: 102 # -------- 103 # 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 + -