📄 internal.html
字号:
<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> manpage (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 11-2. <BCLASS="COMMAND">printf</B> 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" 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 11-3. Variable assignment, using <BCLASS="COMMAND">read</B></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' (separated by a space or tab): " 17 read var2 var3 18 echo "var2 = $var2 var3 = $var3" 19 # If you input only one value, the other variable(s) will remain unset (null). 20 21 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 11-4. What happens when <BCLASS="COMMAND">read</B> 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 26 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 11-5. Multi-line input to <BCLASS="COMMAND">read</B></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, and again press <ENTER>." 7 read var1 # The "\" suppresses the newline, when reading $var1. 8 # first line \ 9 # second line 10 11 echo "var1 = $var1" 12 # var1 = first line second line 13 14 # For each line terminated by a "\" 15 #+ you get a prompt on the next line to continue feeding characters into var1. 16 17 echo; echo 18 19 echo "Enter another string terminated by a \\ , then press <ENTER>." 20 read -r var2 # The -r option causes the "\" to be read literally. 21 # first line \ 22 23 echo "var2 = $var2" 24 # var2 = first line \ 25 26 # Data entry terminates with the first <ENTER>. 27 28 echo 29 30 exit 0</PRE></TD></TR></TABLE><HR></DIV><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>The <TTCLASS="OPTION">-n</TT> option to <BCLASS="COMMAND">read</B> also allows detection of the <ICLASS="EMPHASIS">arrow keys</I> and certain of the other unusual keys.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARROWDETECT"></A><P><B>Example 11-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 # Exercises: 70 # --------- 71 # 1) Simplify this script by rewriting the multiple "if" tests 72 #+ as a 'case' construct. 73 # 2) 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>The <TTCLASS="OPTION">-t</TT> option to <BCLASS="COMMAND">read</B> permits timed input (see <AHREF="variables2.html#TOUT">Example 9-4</A>).</P><P>The <BCLASS="COMMAND">read</B> command may also <SPANCLASS="QUOTE">"read"</SPAN> its variable value from a file <AHREF="io-redirection.html#IOREDIRREF">redirected</A> to <TTCLASS="FILENAME">stdin</TT>. If the file contains more than one line, only the first line is assigned to the variable. If <BCLASS="COMMAND">read</B> has more than one parameter, then each of these variables gets assigned a successive <AHREF="special-chars.html#WHITESPACEREF">whitespace-delineated</A> string. Caution!</P><DIVCLASS="EXAMPLE"><HR><ANAME="READREDIR"></A><P><B>Example 11-7. Using <BCLASS="COMMAND">read</B> with <AHREF="io-redirection.html#IOREDIRREF">file redirection</A></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 read var1 <data-file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -