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

📄 internal.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
   7&nbsp;                    # (Double quotes and spaces make it more readable.)   8&nbsp;echo "11 + 5 = $a"  # 16   9&nbsp;  10&nbsp;let "a &#60;&#60;= 3"       # Equivalent to  let "a = a &#60;&#60; 3"  11&nbsp;echo "\"\$a\" (=16) left-shifted 3 places = $a"  12&nbsp;                    # 128  13&nbsp;  14&nbsp;let "a /= 4"        # Equivalent to  let "a = a / 4"  15&nbsp;echo "128 / 4 = $a" # 32  16&nbsp;  17&nbsp;let "a -= 5"        # Equivalent to  let "a = a - 5"  18&nbsp;echo "32 - 5 = $a"  # 27  19&nbsp;  20&nbsp;let "a *=  10"      # Equivalent to  let "a = a * 10"  21&nbsp;echo "27 * 10 = $a" # 270  22&nbsp;  23&nbsp;let "a %= 8"        # Equivalent to  let "a = a % 8"  24&nbsp;echo "270 modulo 8 = $a  (270 / 8 = 33, remainder $a)"  25&nbsp;                    # 6  26&nbsp;  27&nbsp;echo  28&nbsp;  29&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="EVALREF"></A><BCLASS="COMMAND">eval</B></DT><DD><P><TTCLASS="USERINPUT"><B>eval arg1 [arg2] ... [argN]</B></TT></P><P>Combines the arguments in an expression or list of	      expressions and <ICLASS="FIRSTTERM">evaluates</I> them. Any	      variables contained within the expression are expanded. The	      result translates into a command. This can be useful for	      code generation from the command line or within a script.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>process=xterm</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>show_process="eval ps ax | grep $process"</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>$show_process</B></TT> <TTCLASS="COMPUTEROUTPUT">1867 tty1     S      0:02 xterm 2779 tty1     S      0:00 xterm 2886 pts/1    S      0:00 grep xterm</TT> 	      </PRE></TD></TR></TABLE>	      </P><P><ANAME="EVALFORCED"></A></P><P>Each invocation of <ICLASS="FIRSTTERM">eval</I> forces        a re-<SPANCLASS="emphasis"><ICLASS="EMPHASIS">evaluation</I></SPAN> of its arguments.	<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;a='$b'   2&nbsp;b='$c'   3&nbsp;c=d   4&nbsp;   5&nbsp;echo $a             # $b   6&nbsp;                    # First level.   7&nbsp;eval echo $a        # $c   8&nbsp;                    # Second level.   9&nbsp;eval eval echo $a   # d  10&nbsp;                    # Third level.  11&nbsp;  12&nbsp;# Thank you, E. Choroba.</PRE></TD></TR></TABLE></P><P><ANAME="EVALEFF"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX43"></A><P><B>Example 14-11. Showing the effect of <ICLASS="FIRSTTERM">eval</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Exercising "eval" ...   3&nbsp;   4&nbsp;y=`eval ls -l`  #  Similar to y=`ls -l`   5&nbsp;echo $y         #+ but linefeeds removed because "echoed" variable is unquoted.   6&nbsp;echo   7&nbsp;echo "$y"       #  Linefeeds preserved when variable is quoted.   8&nbsp;   9&nbsp;echo; echo  10&nbsp;  11&nbsp;y=`eval df`     #  Similar to y=`df`  12&nbsp;echo $y         #+ but linefeeds removed.  13&nbsp;  14&nbsp;#  When LF's not preserved, it may make it easier to parse output,  15&nbsp;#+ using utilities such as "awk".  16&nbsp;  17&nbsp;echo  18&nbsp;echo "==========================================================="  19&nbsp;echo  20&nbsp;  21&nbsp;  22&nbsp;# Now, showing how to do something useful with "eval" . . .  23&nbsp;# (Thank you, E. Choroba!)  24&nbsp;  25&nbsp;version=3.4     #  Can we split the version into major and minor  26&nbsp;                #+ part in one command?  27&nbsp;echo "version = $version"  28&nbsp;eval major=${version/./;minor=}     #  Replaces '.' in version by ';minor='  29&nbsp;                                    #  The substitution yields '3; minor=4'  30&nbsp;                                    #+ so eval does minor=4, major=3  31&nbsp;echo Major: $major, minor: $minor   #  Major: 3, minor: 4</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ECHOPARAMS"></A><P><B>Example 14-12. <ICLASS="FIRSTTERM">Echoing</I> the	        <ICLASS="FIRSTTERM">command-line parameters</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# echo-params.sh   3&nbsp;   4&nbsp;# Call this script with a few command line parameters.   5&nbsp;# For example:   6&nbsp;#     sh echo-params.sh first second third fourth fifth   7&nbsp;   8&nbsp;params=$#              # Number of command-line parameters.   9&nbsp;param=1                # Start at first command-line param.  10&nbsp;  11&nbsp;while [ "$param" -le "$params" ]  12&nbsp;do  13&nbsp;  echo -n "Command line parameter "  14&nbsp;  echo -n \$$param     #  Gives only the *name* of variable.  15&nbsp;#         ^^^          #  $1, $2, $3, etc.  16&nbsp;                       #  Why?  17&nbsp;                       #  \$ escapes the first "$"  18&nbsp;                       #+ so it echoes literally,  19&nbsp;                       #+ and $param dereferences "$param" . . .  20&nbsp;                       #+ . . . as expected.  21&nbsp;  echo -n " = "  22&nbsp;  eval echo \$$param   #  Gives the *value* of variable.  23&nbsp;# ^^^^      ^^^        #  The "eval" forces the *evaluation*  24&nbsp;                       #+ of \$$  25&nbsp;                       #+ as an indirect variable reference.  26&nbsp;  27&nbsp;(( param ++ ))         # On to the next.  28&nbsp;done  29&nbsp;  30&nbsp;exit $?  31&nbsp;  32&nbsp;# =================================================  33&nbsp;  34&nbsp;$ sh echo-params.sh first second third fourth fifth  35&nbsp;Command line parameter $1 = first  36&nbsp;Command line parameter $2 = second  37&nbsp;Command line parameter $3 = third  38&nbsp;Command line parameter $4 = fourth  39&nbsp;Command line parameter $5 = fifth</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX44"></A><P><B>Example 14-13. Forcing a log-off</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Killing ppp to force a log-off.   3&nbsp;   4&nbsp;# Script should be run as root user.   5&nbsp;   6&nbsp;killppp="eval kill -9 `ps ax | awk '/ppp/ { print $1 }'`"   7&nbsp;#                     -------- process ID of ppp -------     8&nbsp;   9&nbsp;$killppp                  # This variable is now a command.  10&nbsp;  11&nbsp;  12&nbsp;# The following operations must be done as root user.  13&nbsp;  14&nbsp;chmod 666 /dev/ttyS3      # Restore read+write permissions, or else what?  15&nbsp;#  Since doing a SIGKILL on ppp changed the permissions on the serial port,  16&nbsp;#+ we restore permissions to previous state.  17&nbsp;  18&nbsp;rm /var/lock/LCK..ttyS3   # Remove the serial port lock file. Why?  19&nbsp;  20&nbsp;#  Note:  21&nbsp;#  Depending on the hardware and even the kernel version,  22&nbsp;#+ the modem port on your machine may be different --  23&nbsp;#+ /dev/ttyS1 or /dev/ttyS2.  24&nbsp;  25&nbsp;exit 0  26&nbsp;  27&nbsp;# Exercises:  28&nbsp;# ---------  29&nbsp;# 1) Have script check whether root user is invoking it.  30&nbsp;# 2) Do a check on whether the process to be killed  31&nbsp;#+   is actually running before attempting to kill it.     32&nbsp;# 3) Write an alternate version of this script based on 'fuser':  33&nbsp;#+      if [ fuser -s /dev/modem ]; then . . .</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ROT14"></A><P><B>Example 14-14. A version of <ICLASS="FIRSTTERM">rot13</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# A version of "rot13" using 'eval'.   3&nbsp;# Compare to "rot13.sh" example.   4&nbsp;   5&nbsp;setvar_rot_13()              # "rot13" scrambling   6&nbsp;{   7&nbsp;  local varname=$1 varvalue=$2   8&nbsp;  eval $varname='$(echo "$varvalue" | tr a-z n-za-m)'   9&nbsp;}  10&nbsp;  11&nbsp;  12&nbsp;setvar_rot_13 var "foobar"   # Run "foobar" through rot13.  13&nbsp;echo $var                    # sbbone  14&nbsp;  15&nbsp;setvar_rot_13 var "$var"     # Run "sbbone" through rot13.  16&nbsp;                             # Back to original variable.  17&nbsp;echo $var                    # foobar  18&nbsp;  19&nbsp;# This example by Stephane Chazelas.  20&nbsp;# Modified by document author.  21&nbsp;  22&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Rory Winston contributed the following instance of how	      useful <ICLASS="FIRSTTERM">eval</I> can be.</P><P><ANAME="EVALX0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EVALEX"></A><P><B>Example 14-15. Using <ICLASS="FIRSTTERM">eval</I> to force variable	        substitution in a <ICLASS="FIRSTTERM">Perl</I> script</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;In the Perl script "test.pl":   2&nbsp;        ...		   3&nbsp;        my $WEBROOT = &#60;WEBROOT_PATH&#62;;   4&nbsp;        ...   5&nbsp;   6&nbsp;To force variable substitution try:   7&nbsp;        $export WEBROOT_PATH=/usr/local/webroot   8&nbsp;        $sed 's/&#60;WEBROOT_PATH&#62;/$WEBROOT_PATH/' &#60; test.pl &#62; out   9&nbsp;  10&nbsp;But this just gives:  11&nbsp;        my $WEBROOT = $WEBROOT_PATH;  12&nbsp;  13&nbsp;However:  14&nbsp;        $export WEBROOT_PATH=/usr/local/webroot  15&nbsp;        $eval sed 's%\&#60;WEBROOT_PATH\&#62;%$WEBROOT_PATH%' &#60; test.pl &#62; out  16&nbsp;#        ====  17&nbsp;  18&nbsp;That works fine, and gives the expected substitution:  19&nbsp;        my $WEBROOT = /usr/local/webroot;  20&nbsp;  21&nbsp;  22&nbsp;### Correction applied to original example by Paulo Marcel Coelho Aragao.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="EVALRISK"></A></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>The <BCLASS="COMMAND">eval</B> command can be	      risky, and normally should be avoided when there	      exists a reasonable alternative. An <TTCLASS="USERINPUT"><B>eval	      $COMMANDS</B></TT> executes the contents of	      <TTCLASS="REPLACEABLE"><I>COMMANDS</I></TT>, which may	      contain such unpleasant surprises as <BCLASS="COMMAND">rm -rf	      *</B>. Running an <BCLASS="COMMAND">eval</B> on	      unfamiliar code written by persons unknown is living	      dangerously.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="SETREF"></A><BCLASS="COMMAND">set</B></DT><DD><P>The <BCLASS="COMMAND">set</B> command changes	      the value of internal script variables/options. One use for	      this is to toggle <AHREF="options.html#OPTIONSREF">option	      flags</A> which help determine the behavior of the	      script. Another application for it is to reset the <AHREF="variables2.html#POSPARAMREF">positional parameters</A> that	      a script sees as the result of a command (<TTCLASS="USERINPUT"><B>set	      `command`</B></TT>). The script can then parse the	      fields of the command output.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX34"></A><P><B>Example 14-16. Using <ICLASS="FIRSTTERM">set</I> with positional	        parameters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# script "set-test"   4&nbsp;   5&nbsp;# Invoke this script with three command line parameters,   6&nbsp;# for example, "./set-test one two three".   7&nbsp;   8&nbsp;echo   9&nbsp;echo "Positional parameters before  set \`uname -a\` :"  10&nbsp;echo "Command-line argument #1 = $1"  11&nbsp;echo "Command-line argument #2 = $2"  12&nbsp;echo "Command-line argument #3 = $3"  13&nbsp;  14&nbsp;  15&nbsp;set `uname -a` # Sets the positional parameters to the output  16&nbsp;               # of the command `uname -a`  17&nbsp;  18&nbsp;echo $_        # unknown  19&nbsp;# Flags set in script.  20&nbsp;  21&nbsp;echo "Positional parameters after  set \`uname -a\` :"  22&nbsp;# $1, $2, $3, etc. reinitialized to result of `uname -a`  23&nbsp;echo "Field #1 of 'uname -a' = $1"  24&nbsp;echo "Field #2 of 'uname -a' = $2"  25&nbsp;echo "Field #3 of 'uname -a' = $3"  26&nbsp;echo ---  27&nbsp;echo $_        # ---  28&nbsp;echo  29&nbsp;  30&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>More fun with positional parameters.</P><DIVCLASS="EXAMPLE"><HR><ANAME="REVPOSPARAMS"></A><P><B>Example 14-17. Reversing the positional parameters</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# revposparams.sh: Reverse positional parameters.   3&nbsp;# Script by Dan Jacobson, with stylistic revisions by document author.   4&nbsp;   5&nbsp;   6&nbsp;set a\ b c d\ e;   7&nbsp;#     ^      ^     Spaces escaped    8&nbsp;#       ^ ^        Spaces not escaped   9&nbsp;OIFS=$IFS; IFS=:;  10&nbsp;#              ^   Saving old IFS and setting new one.  11&nbsp;  12&nbsp;echo  13&nbsp;  14&nbsp;until [ $# -eq 0 ]  15&nbsp;do          #      Step through positional parameters.  16&nbsp;  echo "### k0 = "$k""     # Before  17&nbsp;  k=$1:$k;  #      Append each pos param to loop variable.  18&nbsp;#     ^  19&nbsp;  echo "### k = "$k""      # After  20&nbsp;  echo  21&nbsp;  shift;  22&nbsp;done  23&nbsp;  24&nbsp;set $k  #  Set new positional parameters.  25&nbsp;echo -  26&nbsp;echo $# #  Count of positional parameters.  27&nbsp;echo -  28&nbsp;echo  29&nbsp;  30&nbsp;for i   #  Omitting the "in list" sets the variable -- i --  31&nbsp;        #+ to the positional parameters.  32&nbsp;do  33&nbsp;  echo $i  # Display new positional parameters.  34&nbsp;done  35&nbsp;  36&nbsp;IFS=$OIFS  # Restore IFS.  37&nbsp;  38&nbsp;#  Question:  39&nbsp;#  Is it necessary to set an new IFS, internal field separator,  40&nbsp;#+ in order for this script to work properly?  41&nbsp;#  What happens if you don't? Try it.  42&nbsp;#  And, why use the new IFS -- a colon -- in line 17,  43&nbsp;#+ to append to the loop variable?  44&nbsp;#  What is the purpose of this?  45&nbsp;  46&nbsp;exit 0  47&nbsp;  48&nbsp;$ ./revposparams.sh  49&nbsp;  50&nbsp;### k0 =   51&nbsp;### k = a b  52&nbsp;  53&nbsp;### k0 = a b  54&nbsp;### k = c a b  55&nbsp;  56&nbsp;### k0 = c a b  57&nbsp;### k = d e c a b  58&nbsp;  59&nbsp;-  60&nbsp;3  61&nbsp;-  62&nbsp;  63&nbsp;d e  64&nbsp;c  65&nbsp;a b</PRE></TD></TR></TABLE><HR></DIV><P>Invoking <BCLASS="COMMAND">set</B> without any options or	      arguments simply lists all the <AHREF="othertypesv.html#ENVREF">environmental</A> and other variables	      that have been initialized.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>set</B></TT> <TTCLASS="COMPUTEROUTPUT">AUTHORCOPY=/home/bozo/posts BASH=/bin/bash

⌨️ 快捷键说明

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