📄 gotchas.html
字号:
6 chmod 755 $0 # Change back to execute permission. 7 # The 'unix2dos' command removes execute permission. 8 9 ./$0 # Script tries to run itself again. 10 # But it won't work as a DOS file. 11 12 echo "There" 13 14 exit 0</PRE></TD></TR></TABLE> </P><P>A shell script headed by <TTCLASS="USERINPUT"><B>#!/bin/sh</B></TT> will not run in full Bash-compatibility mode. Some Bash-specific functions might be disabled. Scripts that need complete access to all the Bash-specific extensions should start with <TTCLASS="USERINPUT"><B>#!/bin/bash</B></TT>.</P><P><AHREF="here-docs.html#INDENTEDLS">Putting whitespace in front of the terminating limit string</A> of a <AHREF="here-docs.html#HEREDOCREF">here document</A> will cause unexpected behavior in a script.</P><P><ANAME="PARCHILDPROBREF"></A></P><P>A script may not <BCLASS="COMMAND">export</B> variables back to its <AHREF="internal.html#FORKREF">parent process</A>, the shell, or to the environment. Just as we learned in biology, a child process can inherit from a parent, but not vice versa. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 WHATEVER=/home/bozo 2 export WHATEVER 3 exit 0</PRE></TD></TR></TABLE> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><BCLASS="COMMAND">echo $WHATEVER</B> <TTCLASS="COMPUTEROUTPUT"></TT> <TTCLASS="PROMPT">bash$ </TT></PRE></TD></TR></TABLE> Sure enough, back at the command prompt, $WHATEVER remains unset. </P><P>Setting and manipulating variables in a <AHREF="subshells.html#SUBSHELLSREF">subshell</A>, then attempting to use those same variables outside the scope of the subshell will result an unpleasant surprise.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SUBPIT"></A><P><B>Example 31-2. Subshell Pitfalls</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Pitfalls of variables in a subshell. 3 4 outer_variable=outer 5 echo 6 echo "outer_variable = $outer_variable" 7 echo 8 9 ( 10 # Begin subshell 11 12 echo "outer_variable inside subshell = $outer_variable" 13 inner_variable=inner # Set 14 echo "inner_variable inside subshell = $inner_variable" 15 outer_variable=inner # Will value change globally? 16 echo "outer_variable inside subshell = $outer_variable" 17 18 # Will 'exporting' make a difference? 19 # export inner_variable 20 # export outer_variable 21 # Try it and see. 22 23 # End subshell 24 ) 25 26 echo 27 echo "inner_variable outside subshell = $inner_variable" # Unset. 28 echo "outer_variable outside subshell = $outer_variable" # Unchanged. 29 echo 30 31 exit 0 32 33 # What happens if you uncomment lines 19 and 20? 34 # Does it make a difference?</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="BADREAD0"></A></P><P><AHREF="special-chars.html#PIPEREF">Piping</A> <BCLASS="COMMAND">echo</B> output to a <AHREF="internal.html#READREF">read</A> may produce unexpected results. In this scenario, the <BCLASS="COMMAND">read</B> acts as if it were running in a subshell. Instead, use the <AHREF="internal.html#SETREF">set</A> command (as in <AHREF="internal.html#SETPOS">Example 11-16</A>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="BADREAD"></A><P><B>Example 31-3. Piping the output of <BCLASS="COMMAND">echo</B> to a <BCLASS="COMMAND">read</B></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # badread.sh: 3 # Attempting to use 'echo and 'read' 4 #+ to assign variables non-interactively. 5 6 a=aaa 7 b=bbb 8 c=ccc 9 10 echo "one two three" | read a b c 11 # Try to reassign a, b, and c. 12 13 echo 14 echo "a = $a" # a = aaa 15 echo "b = $b" # b = bbb 16 echo "c = $c" # c = ccc 17 # Reassignment failed. 18 19 # ------------------------------ 20 21 # Try the following alternative. 22 23 var=`echo "one two three"` 24 set -- $var 25 a=$1; b=$2; c=$3 26 27 echo "-------" 28 echo "a = $a" # a = one 29 echo "b = $b" # b = two 30 echo "c = $c" # c = three 31 # Reassignment succeeded. 32 33 # ------------------------------ 34 35 # Note also that an echo to a 'read' works within a subshell. 36 # However, the value of the variable changes *only* within the subshell. 37 38 a=aaa # Starting all over again. 39 b=bbb 40 c=ccc 41 42 echo; echo 43 echo "one two three" | ( read a b c; 44 echo "Inside subshell: "; echo "a = $a"; echo "b = $b"; echo "c = $c" ) 45 # a = one 46 # b = two 47 # c = three 48 echo "-----------------" 49 echo "Outside subshell: " 50 echo "a = $a" # a = aaa 51 echo "b = $b" # b = bbb 52 echo "c = $c" # c = ccc 53 echo 54 55 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>In fact, as Anthony Richardson points out, piping to <ICLASS="EMPHASIS">any</I> loop can cause a similar problem.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Loop piping troubles. 2 # This example by Anthony Richardson, 3 #+ with addendum by Wilbert Berendsen. 4 5 6 foundone=false 7 find $HOME -type f -atime +30 -size 100k | 8 while true 9 do 10 read f 11 echo "$f is over 100KB and has not been accessed in over 30 days" 12 echo "Consider moving the file to archives." 13 foundone=true 14 # ------------------------------------ 15 echo "Subshell level = $BASH_SUBSHELL" 16 # Subshell level = 1 17 # Yes, we're inside a subshell. 18 # ------------------------------------ 19 done 20 21 # foundone will always be false here since it is 22 #+ set to true inside a subshell 23 if [ $foundone = false ] 24 then 25 echo "No files need archiving." 26 fi 27 28 # =====================Now, here is the correct way:================= 29 30 foundone=false 31 for f in $(find $HOME -type f -atime +30 -size 100k) # No pipe here. 32 do 33 echo "$f is over 100KB and has not been accessed in over 30 days" 34 echo "Consider moving the file to archives." 35 foundone=true 36 done 37 38 if [ $foundone = false ] 39 then 40 echo "No files need archiving." 41 fi 42 43 # ==================And here is another alternative================== 44 45 # Places the part of the script that reads the variables 46 #+ within a code block, so they share the same subshell. 47 # Thank you, W.B. 48 49 find $HOME -type f -atime +30 -size 100k | { 50 foundone=false 51 while read f 52 do 53 echo "$f is over 100KB and has not been accessed in over 30 days" 54 echo "Consider moving the file to archives." 55 foundone=true 56 done 57 58 if ! $foundone 59 then 60 echo "No files need archiving." 61 fi 62 }</PRE></TD></TR></TABLE> </P><P> A related problem occurs when trying to write the <TTCLASS="FILENAME">stdout</TT> of a <BCLASS="COMMAND">tail -f</B> piped to <AHREF="textproc.html#GREPREF">grep</A>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 tail -f /var/log/messages | grep "$ERROR_MSG" >> error.log 2 # The "error.log" file will not have anything written to it.</PRE></TD></TR></TABLE> </P><P>--</P><P>Using <SPANCLASS="QUOTE">"suid"</SPAN> commands within scripts is risky, as it may compromise system security. <ANAME="AEN15214"HREF="#FTN.AEN15214">[1]</A> </P><P>Using shell scripts for CGI programming may be problematic. Shell script variables are not <SPANCLASS="QUOTE">"typesafe"</SPAN>, and this can cause undesirable behavior as far as CGI is concerned. Moreover, it is difficult to <SPANCLASS="QUOTE">"cracker-proof"</SPAN> shell scripts.</P><P>Bash does not handle the <AHREF="internal.html#DOUBLESLASHREF">double slash (<SPANCLASS="TOKEN">//</SPAN>) string</A> correctly.</P><P>Bash scripts written for Linux or BSD systems may need fixups to run on a commercial UNIX (or Apple OSX) machine. Such scripts often employ GNU commands and filters which have greater functionality than their generic UNIX counterparts. This is particularly true of such text processing utilites as <AHREF="textproc.html#TRREF">tr</A>.</P><TABLEBORDER="0"WIDTH="100%"CELLSPACING="0"CELLPADDING="0"CLASS="EPIGRAPH"><TR><TDWIDTH="45%"> </TD><TDWIDTH="45%"ALIGN="LEFT"VALIGN="TOP"><I><P><I>Danger is near thee --</I></P><P><I>Beware, beware, beware, beware.</I></P><P><I>Many brave hearts are asleep in the deep.</I></P><P><I>So beware --</I></P><P><I>Beware.</I></P></I></TD></TR><TR><TDWIDTH="45%"> </TD><TDWIDTH="45%"ALIGN="RIGHT"VALIGN="TOP"><I><SPANCLASS="ATTRIBUTION">A.J. Lamb and H.W. Petrie</SPAN></I></TD></TR></TABLE></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN15214"HREF="gotchas.html#AEN15214">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Setting the <ICLASS="EMPHASIS">suid</I> permission on the script itself has no effect.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="options.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="scrstyle.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Options</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part4.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Scripting With Style</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -