| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Gotchas</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="Advanced Topics" |
| 15 | HREF="part5.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Options" |
| 18 | HREF="options.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Scripting With Style" |
| 21 | HREF="scrstyle.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="CHAPTER" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="options.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="scrstyle.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="GOTCHAS" |
| 95 | ></A |
| 96 | >Chapter 34. Gotchas</H1 |
| 97 | ><TABLE |
| 98 | BORDER="0" |
| 99 | WIDTH="100%" |
| 100 | CELLSPACING="0" |
| 101 | CELLPADDING="0" |
| 102 | CLASS="EPIGRAPH" |
| 103 | ><TR |
| 104 | ><TD |
| 105 | WIDTH="45%" |
| 106 | > </TD |
| 107 | ><TD |
| 108 | WIDTH="45%" |
| 109 | ALIGN="LEFT" |
| 110 | VALIGN="TOP" |
| 111 | ><I |
| 112 | ><P |
| 113 | ><I |
| 114 | >Turandot: <I |
| 115 | CLASS="FOREIGNPHRASE" |
| 116 | >Gli enigmi sono tre, la morte |
| 117 | una!</I |
| 118 | ></I |
| 119 | ></P |
| 120 | ><P |
| 121 | ><I |
| 122 | >Caleph: <I |
| 123 | CLASS="FOREIGNPHRASE" |
| 124 | >No, no! Gli enigmi sono tre, una la |
| 125 | vita!</I |
| 126 | ></I |
| 127 | ></P |
| 128 | ><P |
| 129 | ><I |
| 130 | >--Puccini</I |
| 131 | ></P |
| 132 | ></I |
| 133 | ></TD |
| 134 | ></TR |
| 135 | ></TABLE |
| 136 | ><P |
| 137 | ><A |
| 138 | NAME="BASH3GOTCHA" |
| 139 | ></A |
| 140 | ></P |
| 141 | ><P |
| 142 | >Here are some (non-recommended!) scripting practices that |
| 143 | will bring excitement into an otherwise dull life.</P |
| 144 | ><UL |
| 145 | ><LI |
| 146 | ><P |
| 147 | ><A |
| 148 | NAME="INAPPVN" |
| 149 | ></A |
| 150 | ></P |
| 151 | ><P |
| 152 | >Assigning reserved words or characters to variable names.</P |
| 153 | ><P |
| 154 | > <TABLE |
| 155 | BORDER="0" |
| 156 | BGCOLOR="#E0E0E0" |
| 157 | WIDTH="90%" |
| 158 | ><TR |
| 159 | ><TD |
| 160 | ><PRE |
| 161 | CLASS="PROGRAMLISTING" |
| 162 | > 1 case=value0 # Causes problems. |
| 163 | 2 23skidoo=value1 # Also problems. |
| 164 | 3 # Variable names starting with a digit are reserved by the shell. |
| 165 | 4 # Try _23skidoo=value1. Starting variables with an underscore is okay. |
| 166 | 5 |
| 167 | 6 # However . . . using just an underscore will not work. |
| 168 | 7 _=25 |
| 169 | 8 echo $_ # $_ is a special variable set to last arg of last command. |
| 170 | 9 # But . . . _ is a valid function name! |
| 171 | 10 |
| 172 | 11 xyz((!*=value2 # Causes severe problems. |
| 173 | 12 # As of version 3 of Bash, periods are not allowed within variable names.</PRE |
| 174 | ></TD |
| 175 | ></TR |
| 176 | ></TABLE |
| 177 | > |
| 178 | </P |
| 179 | ></LI |
| 180 | ><LI |
| 181 | ><P |
| 182 | >Using a hyphen or other reserved characters in a variable name (or |
| 183 | function name).</P |
| 184 | ><P |
| 185 | > <TABLE |
| 186 | BORDER="0" |
| 187 | BGCOLOR="#E0E0E0" |
| 188 | WIDTH="90%" |
| 189 | ><TR |
| 190 | ><TD |
| 191 | ><PRE |
| 192 | CLASS="PROGRAMLISTING" |
| 193 | > 1 var-1=23 |
| 194 | 2 # Use 'var_1' instead. |
| 195 | 3 |
| 196 | 4 function-whatever () # Error |
| 197 | 5 # Use 'function_whatever ()' instead. |
| 198 | 6 |
| 199 | 7 |
| 200 | 8 # As of version 3 of Bash, periods are not allowed within function names. |
| 201 | 9 function.whatever () # Error |
| 202 | 10 # Use 'functionWhatever ()' instead.</PRE |
| 203 | ></TD |
| 204 | ></TR |
| 205 | ></TABLE |
| 206 | > |
| 207 | </P |
| 208 | ></LI |
| 209 | ><LI |
| 210 | ><P |
| 211 | >Using the same name for a variable and a function. This can make a |
| 212 | script difficult to understand.</P |
| 213 | ><P |
| 214 | > <TABLE |
| 215 | BORDER="0" |
| 216 | BGCOLOR="#E0E0E0" |
| 217 | WIDTH="90%" |
| 218 | ><TR |
| 219 | ><TD |
| 220 | ><PRE |
| 221 | CLASS="PROGRAMLISTING" |
| 222 | > 1 do_something () |
| 223 | 2 { |
| 224 | 3 echo "This function does something with \"$1\"." |
| 225 | 4 } |
| 226 | 5 |
| 227 | 6 do_something=do_something |
| 228 | 7 |
| 229 | 8 do_something do_something |
| 230 | 9 |
| 231 | 10 # All this is legal, but highly confusing.</PRE |
| 232 | ></TD |
| 233 | ></TR |
| 234 | ></TABLE |
| 235 | > |
| 236 | </P |
| 237 | ></LI |
| 238 | ><LI |
| 239 | ><P |
| 240 | ><A |
| 241 | NAME="WSBAD" |
| 242 | ></A |
| 243 | >Using <A |
| 244 | HREF="special-chars.html#WHITESPACEREF" |
| 245 | >whitespace</A |
| 246 | > inappropriately. |
| 247 | In contrast to other programming languages, Bash can be quite |
| 248 | finicky about whitespace.</P |
| 249 | ><P |
| 250 | > <TABLE |
| 251 | BORDER="0" |
| 252 | BGCOLOR="#E0E0E0" |
| 253 | WIDTH="90%" |
| 254 | ><TR |
| 255 | ><TD |
| 256 | ><PRE |
| 257 | CLASS="PROGRAMLISTING" |
| 258 | > 1 var1 = 23 # 'var1=23' is correct. |
| 259 | 2 # On line above, Bash attempts to execute command "var1" |
| 260 | 3 # with the arguments "=" and "23". |
| 261 | 4 |
| 262 | 5 let c = $a - $b # Instead: let c=$a-$b or let "c = $a - $b" |
| 263 | 6 |
| 264 | 7 if [ $a -le 5] # if [ $a -le 5 ] is correct. |
| 265 | 8 # ^^ if [ "$a" -le 5 ] is even better. |
| 266 | 9 # [[ $a -le 5 ]] also works.</PRE |
| 267 | ></TD |
| 268 | ></TR |
| 269 | ></TABLE |
| 270 | > |
| 271 | </P |
| 272 | ></LI |
| 273 | ><LI |
| 274 | ><P |
| 275 | ><A |
| 276 | NAME="OMITSEMICOLON" |
| 277 | ></A |
| 278 | ></P |
| 279 | ><P |
| 280 | >Not terminating with a <A |
| 281 | HREF="special-chars.html#SEMICOLONREF" |
| 282 | >semicolon</A |
| 283 | > the final command |
| 284 | in a <A |
| 285 | HREF="special-chars.html#CODEBLOCKREF" |
| 286 | >code block within curly |
| 287 | brackets</A |
| 288 | >.</P |
| 289 | ><P |
| 290 | > <TABLE |
| 291 | BORDER="0" |
| 292 | BGCOLOR="#E0E0E0" |
| 293 | WIDTH="90%" |
| 294 | ><TR |
| 295 | ><TD |
| 296 | ><PRE |
| 297 | CLASS="PROGRAMLISTING" |
| 298 | > 1 { ls -l; df; echo "Done." } |
| 299 | 2 # bash: syntax error: unexpected end of file |
| 300 | 3 |
| 301 | 4 { ls -l; df; echo "Done."; } |
| 302 | 5 # ^ ### Final command needs semicolon.</PRE |
| 303 | ></TD |
| 304 | ></TR |
| 305 | ></TABLE |
| 306 | > |
| 307 | </P |
| 308 | ></LI |
| 309 | ><LI |
| 310 | ><P |
| 311 | ><A |
| 312 | NAME="UNINITVAR" |
| 313 | ></A |
| 314 | ></P |
| 315 | ><P |
| 316 | > Assuming uninitialized variables (variables before a value is |
| 317 | assigned to them) are <SPAN |
| 318 | CLASS="QUOTE" |
| 319 | >"zeroed out"</SPAN |
| 320 | >. An |
| 321 | uninitialized variable has a value of <I |
| 322 | CLASS="FIRSTTERM" |
| 323 | >null</I |
| 324 | >, |
| 325 | <SPAN |
| 326 | CLASS="emphasis" |
| 327 | ><I |
| 328 | CLASS="EMPHASIS" |
| 329 | >not</I |
| 330 | ></SPAN |
| 331 | > zero.</P |
| 332 | ><P |
| 333 | ><A |
| 334 | NAME="BASH4.2-UNINITIALIZED" |
| 335 | ></A |
| 336 | ></P |
| 337 | ><P |
| 338 | > <TABLE |
| 339 | BORDER="0" |
| 340 | BGCOLOR="#E0E0E0" |
| 341 | WIDTH="90%" |
| 342 | ><TR |
| 343 | ><TD |
| 344 | ><PRE |
| 345 | CLASS="PROGRAMLISTING" |
| 346 | > 1 #!/bin/bash |
| 347 | 2 |
| 348 | 3 echo "uninitialized_var = $uninitialized_var" |
| 349 | 4 # uninitialized_var = |
| 350 | 5 |
| 351 | 6 # However . . . |
| 352 | 7 # if $BASH_VERSION ≥ 4.2; then |
| 353 | 8 |
| 354 | 9 if [[ ! -v uninitialized_var ]] |
| 355 | 10 then |
| 356 | 11 uninitialized_var=0 # Initialize it to zero! |
| 357 | 12 fi |
| 358 | 13 |
| 359 | 14 </PRE |
| 360 | ></TD |
| 361 | ></TR |
| 362 | ></TABLE |
| 363 | > |
| 364 | |
| 365 | </P |
| 366 | ></LI |
| 367 | ><LI |
| 368 | ><P |
| 369 | ><A |
| 370 | NAME="EQDIF" |
| 371 | ></A |
| 372 | ></P |
| 373 | ><P |
| 374 | >Mixing up <I |
| 375 | CLASS="FIRSTTERM" |
| 376 | >=</I |
| 377 | > and |
| 378 | <I |
| 379 | CLASS="FIRSTTERM" |
| 380 | >-eq</I |
| 381 | > in a test. Remember, |
| 382 | <I |
| 383 | CLASS="FIRSTTERM" |
| 384 | >=</I |
| 385 | > is for comparing literal variables |
| 386 | and <I |
| 387 | CLASS="FIRSTTERM" |
| 388 | >-eq</I |
| 389 | > for integers.</P |
| 390 | ><P |
| 391 | > <TABLE |
| 392 | BORDER="0" |
| 393 | BGCOLOR="#E0E0E0" |
| 394 | WIDTH="90%" |
| 395 | ><TR |
| 396 | ><TD |
| 397 | ><PRE |
| 398 | CLASS="PROGRAMLISTING" |
| 399 | > 1 if [ "$a" = 273 ] # Is $a an integer or string? |
| 400 | 2 if [ "$a" -eq 273 ] # If $a is an integer. |
| 401 | 3 |
| 402 | 4 # Sometimes you can interchange -eq and = without adverse consequences. |
| 403 | 5 # However . . . |
| 404 | 6 |
| 405 | 7 |
| 406 | 8 a=273.0 # Not an integer. |
| 407 | 9 |
| 408 | 10 if [ "$a" = 273 ] |
| 409 | 11 then |
| 410 | 12 echo "Comparison works." |
| 411 | 13 else |
| 412 | 14 echo "Comparison does not work." |
| 413 | 15 fi # Comparison does not work. |
| 414 | 16 |
| 415 | 17 # Same with a=" 273" and a="0273". |
| 416 | 18 |
| 417 | 19 |
| 418 | 20 # Likewise, problems trying to use "-eq" with non-integer values. |
| 419 | 21 |
| 420 | 22 if [ "$a" -eq 273.0 ] |
| 421 | 23 then |
| 422 | 24 echo "a = $a" |
| 423 | 25 fi # Aborts with an error message. |
| 424 | 26 # test.sh: [: 273.0: integer expression expected</PRE |
| 425 | ></TD |
| 426 | ></TR |
| 427 | ></TABLE |
| 428 | > |
| 429 | </P |
| 430 | ></LI |
| 431 | ><LI |
| 432 | ><P |
| 433 | ><A |
| 434 | NAME="NUMSTRCOMPNE" |
| 435 | ></A |
| 436 | ></P |
| 437 | ><P |
| 438 | >Misusing <A |
| 439 | HREF="comparison-ops.html#SCOMPARISON1" |
| 440 | >string comparison</A |
| 441 | > |
| 442 | operators.</P |
| 443 | ><DIV |
| 444 | CLASS="EXAMPLE" |
| 445 | ><HR><A |
| 446 | NAME="BADOP" |
| 447 | ></A |
| 448 | ><P |
| 449 | ><B |
| 450 | >Example 34-1. Numerical and string comparison are not equivalent</B |
| 451 | ></P |
| 452 | ><TABLE |
| 453 | BORDER="0" |
| 454 | BGCOLOR="#E0E0E0" |
| 455 | WIDTH="90%" |
| 456 | ><TR |
| 457 | ><TD |
| 458 | ><PRE |
| 459 | CLASS="PROGRAMLISTING" |
| 460 | > 1 #!/bin/bash |
| 461 | 2 # bad-op.sh: Trying to use a string comparison on integers. |
| 462 | 3 |
| 463 | 4 echo |
| 464 | 5 number=1 |
| 465 | 6 |
| 466 | 7 # The following while-loop has two errors: |
| 467 | 8 #+ one blatant, and the other subtle. |
| 468 | 9 |
| 469 | 10 while [ "$number" < 5 ] # Wrong! Should be: while [ "$number" -lt 5 ] |
| 470 | 11 do |
| 471 | 12 echo -n "$number " |
| 472 | 13 let "number += 1" |
| 473 | 14 done |
| 474 | 15 # Attempt to run this bombs with the error message: |
| 475 | 16 #+ bad-op.sh: line 10: 5: No such file or directory |
| 476 | 17 # Within single brackets, "<" must be escaped, |
| 477 | 18 #+ and even then, it's still wrong for comparing integers. |
| 478 | 19 |
| 479 | 20 echo "---------------------" |
| 480 | 21 |
| 481 | 22 while [ "$number" \< 5 ] # 1 2 3 4 |
| 482 | 23 do # |
| 483 | 24 echo -n "$number " # It *seems* to work, but . . . |
| 484 | 25 let "number += 1" #+ it actually does an ASCII comparison, |
| 485 | 26 done #+ rather than a numerical one. |
| 486 | 27 |
| 487 | 28 echo; echo "---------------------" |
| 488 | 29 |
| 489 | 30 # This can cause problems. For example: |
| 490 | 31 |
| 491 | 32 lesser=5 |
| 492 | 33 greater=105 |
| 493 | 34 |
| 494 | 35 if [ "$greater" \< "$lesser" ] |
| 495 | 36 then |
| 496 | 37 echo "$greater is less than $lesser" |
| 497 | 38 fi # 105 is less than 5 |
| 498 | 39 # In fact, "105" actually is less than "5" |
| 499 | 40 #+ in a string comparison (ASCII sort order). |
| 500 | 41 |
| 501 | 42 echo |
| 502 | 43 |
| 503 | 44 exit 0</PRE |
| 504 | ></TD |
| 505 | ></TR |
| 506 | ></TABLE |
| 507 | ><HR></DIV |
| 508 | ></LI |
| 509 | ><LI |
| 510 | ><P |
| 511 | ><A |
| 512 | NAME="LETBAD" |
| 513 | ></A |
| 514 | ></P |
| 515 | ><P |
| 516 | >Attempting to use <A |
| 517 | HREF="internal.html#LETREF" |
| 518 | >let</A |
| 519 | > |
| 520 | to set string variables.</P |
| 521 | ><P |
| 522 | ><TABLE |
| 523 | BORDER="0" |
| 524 | BGCOLOR="#E0E0E0" |
| 525 | WIDTH="90%" |
| 526 | ><TR |
| 527 | ><TD |
| 528 | ><PRE |
| 529 | CLASS="PROGRAMLISTING" |
| 530 | > 1 let "a = hello, you" |
| 531 | 2 echo "$a" # 0</PRE |
| 532 | ></TD |
| 533 | ></TR |
| 534 | ></TABLE |
| 535 | ></P |
| 536 | ></LI |
| 537 | ><LI |
| 538 | ><P |
| 539 | ><A |
| 540 | NAME="FAILQUOTE" |
| 541 | ></A |
| 542 | ></P |
| 543 | ><P |
| 544 | >Sometimes variables within <SPAN |
| 545 | CLASS="QUOTE" |
| 546 | >"test"</SPAN |
| 547 | > brackets |
| 548 | ([ ]) need to be quoted (double quotes). Failure to do so may |
| 549 | cause unexpected behavior. See <A |
| 550 | HREF="comparison-ops.html#STRTEST" |
| 551 | >Example 7-6</A |
| 552 | >, <A |
| 553 | HREF="redircb.html#REDIR2" |
| 554 | >Example 20-5</A |
| 555 | >, and <A |
| 556 | HREF="variables2.html#ARGLIST" |
| 557 | >Example 9-6</A |
| 558 | >.</P |
| 559 | ></LI |
| 560 | ><LI |
| 561 | ><P |
| 562 | ><A |
| 563 | NAME="FAILNOTQUOTE" |
| 564 | ></A |
| 565 | ></P |
| 566 | ><P |
| 567 | >Quoting a variable containing whitespace <A |
| 568 | HREF="quoting.html#WSQUO" |
| 569 | >prevents splitting</A |
| 570 | >. Sometimes |
| 571 | this produces <A |
| 572 | HREF="quoting.html#VARSPLITTING" |
| 573 | >unintended |
| 574 | consequences</A |
| 575 | >.</P |
| 576 | ></LI |
| 577 | ><LI |
| 578 | ><P |
| 579 | ><A |
| 580 | NAME="EXECPERM" |
| 581 | ></A |
| 582 | ></P |
| 583 | ><P |
| 584 | >Commands issued from a script may fail to execute because |
| 585 | the script owner lacks execute permission for them. If a user |
| 586 | cannot invoke a command from the command-line, then putting it |
| 587 | into a script will likewise fail. Try changing the attributes of |
| 588 | the command in question, perhaps even setting the suid bit |
| 589 | (as <I |
| 590 | CLASS="FIRSTTERM" |
| 591 | >root</I |
| 592 | >, of course).</P |
| 593 | ></LI |
| 594 | ><LI |
| 595 | ><P |
| 596 | ><A |
| 597 | NAME="DASHNREDR" |
| 598 | ></A |
| 599 | ></P |
| 600 | ><P |
| 601 | >Attempting to use <B |
| 602 | CLASS="COMMAND" |
| 603 | >-</B |
| 604 | > as a redirection |
| 605 | operator (which it is not) will usually result in an unpleasant |
| 606 | surprise.</P |
| 607 | ><P |
| 608 | > <TABLE |
| 609 | BORDER="0" |
| 610 | BGCOLOR="#E0E0E0" |
| 611 | WIDTH="90%" |
| 612 | ><TR |
| 613 | ><TD |
| 614 | ><PRE |
| 615 | CLASS="PROGRAMLISTING" |
| 616 | > 1 command1 2> - | command2 |
| 617 | 2 # Trying to redirect error output of command1 into a pipe . . . |
| 618 | 3 # . . . will not work. |
| 619 | 4 |
| 620 | 5 command1 2>& - | command2 # Also futile. |
| 621 | 6 |
| 622 | 7 Thanks, S.C.</PRE |
| 623 | ></TD |
| 624 | ></TR |
| 625 | ></TABLE |
| 626 | ></P |
| 627 | ></LI |
| 628 | ><LI |
| 629 | ><P |
| 630 | ><A |
| 631 | NAME="LATEVERF" |
| 632 | ></A |
| 633 | ></P |
| 634 | ><P |
| 635 | >Using Bash <A |
| 636 | HREF="bash2.html#BASH2REF" |
| 637 | >version 2+</A |
| 638 | > |
| 639 | functionality may cause a bailout with error messages. Older |
| 640 | Linux machines may have version 1.XX of Bash as the default |
| 641 | installation.</P |
| 642 | ><P |
| 643 | > <TABLE |
| 644 | BORDER="0" |
| 645 | BGCOLOR="#E0E0E0" |
| 646 | WIDTH="90%" |
| 647 | ><TR |
| 648 | ><TD |
| 649 | ><PRE |
| 650 | CLASS="PROGRAMLISTING" |
| 651 | > 1 #!/bin/bash |
| 652 | 2 |
| 653 | 3 minimum_version=2 |
| 654 | 4 # Since Chet Ramey is constantly adding features to Bash, |
| 655 | 5 # you may set $minimum_version to 2.XX, 3.XX, or whatever is appropriate. |
| 656 | 6 E_BAD_VERSION=80 |
| 657 | 7 |
| 658 | 8 if [ "$BASH_VERSION" \< "$minimum_version" ] |
| 659 | 9 then |
| 660 | 10 echo "This script works only with Bash, version $minimum or greater." |
| 661 | 11 echo "Upgrade strongly recommended." |
| 662 | 12 exit $E_BAD_VERSION |
| 663 | 13 fi |
| 664 | 14 |
| 665 | 15 ...</PRE |
| 666 | ></TD |
| 667 | ></TR |
| 668 | ></TABLE |
| 669 | ></P |
| 670 | ></LI |
| 671 | ><LI |
| 672 | ><P |
| 673 | >Using Bash-specific functionality in a <A |
| 674 | HREF="why-shell.html#BASHDEF" |
| 675 | >Bourne shell</A |
| 676 | > script |
| 677 | (<TT |
| 678 | CLASS="USERINPUT" |
| 679 | ><B |
| 680 | >#!/bin/sh</B |
| 681 | ></TT |
| 682 | >) on a non-Linux machine |
| 683 | <A |
| 684 | HREF="gotchas.html#BINSH" |
| 685 | >may cause unexpected behavior</A |
| 686 | >. |
| 687 | A Linux system usually aliases <B |
| 688 | CLASS="COMMAND" |
| 689 | >sh</B |
| 690 | > to |
| 691 | <B |
| 692 | CLASS="COMMAND" |
| 693 | >bash</B |
| 694 | >, but this does not necessarily hold true |
| 695 | for a generic UNIX machine.</P |
| 696 | ></LI |
| 697 | ><LI |
| 698 | ><P |
| 699 | ><A |
| 700 | NAME="UNDOCF" |
| 701 | ></A |
| 702 | ></P |
| 703 | ><P |
| 704 | >Using undocumented features in Bash turns out to be a |
| 705 | dangerous practice. In previous releases of this |
| 706 | book there were several scripts that depended on the |
| 707 | <SPAN |
| 708 | CLASS="QUOTE" |
| 709 | >"feature"</SPAN |
| 710 | > that, although the maximum value |
| 711 | of an <A |
| 712 | HREF="exit-status.html#EXITSTATUSREF" |
| 713 | >exit</A |
| 714 | > or <A |
| 715 | HREF="functions.html#RETURNREF" |
| 716 | >return</A |
| 717 | > value was 255, that limit |
| 718 | did not apply to <SPAN |
| 719 | CLASS="emphasis" |
| 720 | ><I |
| 721 | CLASS="EMPHASIS" |
| 722 | >negative</I |
| 723 | ></SPAN |
| 724 | > integers. |
| 725 | Unfortunately, in version 2.05b and later, that loophole |
| 726 | disappeared. See <A |
| 727 | HREF="functions.html#RETURNTEST" |
| 728 | >Example 24-9</A |
| 729 | >.</P |
| 730 | ></LI |
| 731 | ><LI |
| 732 | ><P |
| 733 | ><A |
| 734 | NAME="GOTCHAEXITVALANAMALIES" |
| 735 | ></A |
| 736 | ></P |
| 737 | ><P |
| 738 | >In certain contexts, a misleading <A |
| 739 | HREF="exit-status.html#EXITSTATUSREF" |
| 740 | >exit status</A |
| 741 | > |
| 742 | may be returned. This may occur when <A |
| 743 | HREF="localvar.html#EXITVALANOMALY01" |
| 744 | >setting a local variable within a |
| 745 | function</A |
| 746 | > or when <A |
| 747 | HREF="internal.html#EXITVALANOMALY02" |
| 748 | >assigning |
| 749 | an arithmetic value to a variable</A |
| 750 | >.</P |
| 751 | ></LI |
| 752 | ><LI |
| 753 | ><P |
| 754 | ><A |
| 755 | NAME="ARXS1" |
| 756 | ></A |
| 757 | >The <A |
| 758 | HREF="tests.html#ARXS" |
| 759 | >exit |
| 760 | status of an arithmetic expression</A |
| 761 | > is |
| 762 | <SPAN |
| 763 | CLASS="emphasis" |
| 764 | ><I |
| 765 | CLASS="EMPHASIS" |
| 766 | >not</I |
| 767 | ></SPAN |
| 768 | > equivalent to an <I |
| 769 | CLASS="FIRSTTERM" |
| 770 | >error |
| 771 | code</I |
| 772 | >.</P |
| 773 | ><P |
| 774 | ><TABLE |
| 775 | BORDER="0" |
| 776 | BGCOLOR="#E0E0E0" |
| 777 | WIDTH="90%" |
| 778 | ><TR |
| 779 | ><TD |
| 780 | ><PRE |
| 781 | CLASS="PROGRAMLISTING" |
| 782 | > 1 var=1 && ((--var)) && echo $var |
| 783 | 2 # ^^^^^^^^^ Here the and-list terminates with exit status 1. |
| 784 | 3 # $var doesn't echo! |
| 785 | 4 echo $? # 1</PRE |
| 786 | ></TD |
| 787 | ></TR |
| 788 | ></TABLE |
| 789 | ></P |
| 790 | ></LI |
| 791 | ><LI |
| 792 | ><P |
| 793 | ><A |
| 794 | NAME="DOSNEWLINES" |
| 795 | ></A |
| 796 | ></P |
| 797 | ><P |
| 798 | > A script with DOS-type newlines (<TT |
| 799 | CLASS="REPLACEABLE" |
| 800 | ><I |
| 801 | >\r\n</I |
| 802 | ></TT |
| 803 | >) |
| 804 | will fail to execute, since <TT |
| 805 | CLASS="USERINPUT" |
| 806 | ><B |
| 807 | >#!/bin/bash\r\n</B |
| 808 | ></TT |
| 809 | > |
| 810 | is <SPAN |
| 811 | CLASS="emphasis" |
| 812 | ><I |
| 813 | CLASS="EMPHASIS" |
| 814 | >not</I |
| 815 | ></SPAN |
| 816 | > recognized, <SPAN |
| 817 | CLASS="emphasis" |
| 818 | ><I |
| 819 | CLASS="EMPHASIS" |
| 820 | >not</I |
| 821 | ></SPAN |
| 822 | > |
| 823 | the same as the expected <TT |
| 824 | CLASS="USERINPUT" |
| 825 | ><B |
| 826 | >#!/bin/bash\n</B |
| 827 | ></TT |
| 828 | >. The |
| 829 | fix is to convert the script to UNIX-style newlines.</P |
| 830 | ><P |
| 831 | > <TABLE |
| 832 | BORDER="0" |
| 833 | BGCOLOR="#E0E0E0" |
| 834 | WIDTH="90%" |
| 835 | ><TR |
| 836 | ><TD |
| 837 | ><PRE |
| 838 | CLASS="PROGRAMLISTING" |
| 839 | > 1 #!/bin/bash |
| 840 | 2 |
| 841 | 3 echo "Here" |
| 842 | 4 |
| 843 | 5 unix2dos $0 # Script changes itself to DOS format. |
| 844 | 6 chmod 755 $0 # Change back to execute permission. |
| 845 | 7 # The 'unix2dos' command removes execute permission. |
| 846 | 8 |
| 847 | 9 ./$0 # Script tries to run itself again. |
| 848 | 10 # But it won't work as a DOS file. |
| 849 | 11 |
| 850 | 12 echo "There" |
| 851 | 13 |
| 852 | 14 exit 0</PRE |
| 853 | ></TD |
| 854 | ></TR |
| 855 | ></TABLE |
| 856 | > |
| 857 | </P |
| 858 | ></LI |
| 859 | ><LI |
| 860 | ><P |
| 861 | ><A |
| 862 | NAME="BINSH" |
| 863 | ></A |
| 864 | ></P |
| 865 | ><P |
| 866 | >A shell script headed by <TT |
| 867 | CLASS="USERINPUT" |
| 868 | ><B |
| 869 | >#!/bin/sh</B |
| 870 | ></TT |
| 871 | > |
| 872 | will not run in full Bash-compatibility mode. Some Bash-specific |
| 873 | functions might be disabled. Scripts that need complete |
| 874 | access to all the Bash-specific extensions should start with |
| 875 | <TT |
| 876 | CLASS="USERINPUT" |
| 877 | ><B |
| 878 | >#!/bin/bash</B |
| 879 | ></TT |
| 880 | >.</P |
| 881 | ></LI |
| 882 | ><LI |
| 883 | ><P |
| 884 | ><A |
| 885 | HREF="here-docs.html#INDENTEDLS" |
| 886 | >Putting whitespace in front of |
| 887 | the terminating limit string</A |
| 888 | > of a <A |
| 889 | HREF="here-docs.html#HEREDOCREF" |
| 890 | >here document</A |
| 891 | > will cause unexpected |
| 892 | behavior in a script.</P |
| 893 | ></LI |
| 894 | ><LI |
| 895 | ><P |
| 896 | ><A |
| 897 | NAME="RVTCAUTION2" |
| 898 | ></A |
| 899 | >Putting more than one |
| 900 | <I |
| 901 | CLASS="FIRSTTERM" |
| 902 | >echo</I |
| 903 | > statement in a function <A |
| 904 | HREF="assortedtips.html#RVT" |
| 905 | >whose output is captured</A |
| 906 | >. |
| 907 | <TABLE |
| 908 | BORDER="0" |
| 909 | BGCOLOR="#E0E0E0" |
| 910 | WIDTH="90%" |
| 911 | ><TR |
| 912 | ><TD |
| 913 | ><PRE |
| 914 | CLASS="PROGRAMLISTING" |
| 915 | > 1 add2 () |
| 916 | 2 { |
| 917 | 3 echo "Whatever ... " # Delete this line! |
| 918 | 4 let "retval = $1 + $2" |
| 919 | 5 echo $retval |
| 920 | 6 } |
| 921 | 7 |
| 922 | 8 num1=12 |
| 923 | 9 num2=43 |
| 924 | 10 echo "Sum of $num1 and $num2 = $(add2 $num1 $num2)" |
| 925 | 11 |
| 926 | 12 # Sum of 12 and 43 = Whatever ... |
| 927 | 13 # 55 |
| 928 | 14 |
| 929 | 15 # The "echoes" concatenate.</PRE |
| 930 | ></TD |
| 931 | ></TR |
| 932 | ></TABLE |
| 933 | > |
| 934 | This <A |
| 935 | HREF="assortedtips.html#RVTCAUTION" |
| 936 | >will not work</A |
| 937 | >.</P |
| 938 | ></LI |
| 939 | ><LI |
| 940 | ><P |
| 941 | ><A |
| 942 | NAME="PARCHILDPROBREF" |
| 943 | ></A |
| 944 | ></P |
| 945 | ><P |
| 946 | >A script may not <B |
| 947 | CLASS="COMMAND" |
| 948 | >export</B |
| 949 | > variables back |
| 950 | to its <A |
| 951 | HREF="internal.html#FORKREF" |
| 952 | >parent process</A |
| 953 | >, the shell, |
| 954 | or to the environment. Just as we learned in biology, a child |
| 955 | process can inherit from a parent, but not vice versa.</P |
| 956 | ><P |
| 957 | > <TABLE |
| 958 | BORDER="0" |
| 959 | BGCOLOR="#E0E0E0" |
| 960 | WIDTH="90%" |
| 961 | ><TR |
| 962 | ><TD |
| 963 | ><PRE |
| 964 | CLASS="PROGRAMLISTING" |
| 965 | > 1 WHATEVER=/home/bozo |
| 966 | 2 export WHATEVER |
| 967 | 3 exit 0</PRE |
| 968 | ></TD |
| 969 | ></TR |
| 970 | ></TABLE |
| 971 | > |
| 972 | <TABLE |
| 973 | BORDER="0" |
| 974 | BGCOLOR="#E0E0E0" |
| 975 | WIDTH="90%" |
| 976 | ><TR |
| 977 | ><TD |
| 978 | ><PRE |
| 979 | CLASS="SCREEN" |
| 980 | > <TT |
| 981 | CLASS="PROMPT" |
| 982 | >bash$ </TT |
| 983 | ><B |
| 984 | CLASS="COMMAND" |
| 985 | >echo $WHATEVER</B |
| 986 | > |
| 987 | <TT |
| 988 | CLASS="COMPUTEROUTPUT" |
| 989 | ></TT |
| 990 | > |
| 991 | <TT |
| 992 | CLASS="PROMPT" |
| 993 | >bash$ </TT |
| 994 | ></PRE |
| 995 | ></TD |
| 996 | ></TR |
| 997 | ></TABLE |
| 998 | > |
| 999 | </P |
| 1000 | ><P |
| 1001 | > Sure enough, back at the command prompt, $WHATEVER remains unset. |
| 1002 | </P |
| 1003 | ></LI |
| 1004 | ><LI |
| 1005 | ><P |
| 1006 | ><A |
| 1007 | NAME="VARSUBSH" |
| 1008 | ></A |
| 1009 | ></P |
| 1010 | ><P |
| 1011 | >Setting and manipulating variables in a <A |
| 1012 | HREF="subshells.html#SUBSHELLSREF" |
| 1013 | >subshell</A |
| 1014 | >, then attempting |
| 1015 | to use those same variables outside the scope of the subshell will |
| 1016 | result an unpleasant surprise.</P |
| 1017 | ><DIV |
| 1018 | CLASS="EXAMPLE" |
| 1019 | ><HR><A |
| 1020 | NAME="SUBPIT" |
| 1021 | ></A |
| 1022 | ><P |
| 1023 | ><B |
| 1024 | >Example 34-2. Subshell Pitfalls</B |
| 1025 | ></P |
| 1026 | ><TABLE |
| 1027 | BORDER="0" |
| 1028 | BGCOLOR="#E0E0E0" |
| 1029 | WIDTH="90%" |
| 1030 | ><TR |
| 1031 | ><TD |
| 1032 | ><PRE |
| 1033 | CLASS="PROGRAMLISTING" |
| 1034 | > 1 #!/bin/bash |
| 1035 | 2 # Pitfalls of variables in a subshell. |
| 1036 | 3 |
| 1037 | 4 outer_variable=outer |
| 1038 | 5 echo |
| 1039 | 6 echo "outer_variable = $outer_variable" |
| 1040 | 7 echo |
| 1041 | 8 |
| 1042 | 9 ( |
| 1043 | 10 # Begin subshell |
| 1044 | 11 |
| 1045 | 12 echo "outer_variable inside subshell = $outer_variable" |
| 1046 | 13 inner_variable=inner # Set |
| 1047 | 14 echo "inner_variable inside subshell = $inner_variable" |
| 1048 | 15 outer_variable=inner # Will value change globally? |
| 1049 | 16 echo "outer_variable inside subshell = $outer_variable" |
| 1050 | 17 |
| 1051 | 18 # Will 'exporting' make a difference? |
| 1052 | 19 # export inner_variable |
| 1053 | 20 # export outer_variable |
| 1054 | 21 # Try it and see. |
| 1055 | 22 |
| 1056 | 23 # End subshell |
| 1057 | 24 ) |
| 1058 | 25 |
| 1059 | 26 echo |
| 1060 | 27 echo "inner_variable outside subshell = $inner_variable" # Unset. |
| 1061 | 28 echo "outer_variable outside subshell = $outer_variable" # Unchanged. |
| 1062 | 29 echo |
| 1063 | 30 |
| 1064 | 31 exit 0 |
| 1065 | 32 |
| 1066 | 33 # What happens if you uncomment lines 19 and 20? |
| 1067 | 34 # Does it make a difference?</PRE |
| 1068 | ></TD |
| 1069 | ></TR |
| 1070 | ></TABLE |
| 1071 | ><HR></DIV |
| 1072 | ></LI |
| 1073 | ><LI |
| 1074 | ><P |
| 1075 | ><A |
| 1076 | NAME="BADREAD0" |
| 1077 | ></A |
| 1078 | ></P |
| 1079 | ><P |
| 1080 | ><A |
| 1081 | HREF="special-chars.html#PIPEREF" |
| 1082 | >Piping</A |
| 1083 | > |
| 1084 | <B |
| 1085 | CLASS="COMMAND" |
| 1086 | >echo</B |
| 1087 | > output to a <A |
| 1088 | HREF="internal.html#READREF" |
| 1089 | >read</A |
| 1090 | > may produce unexpected |
| 1091 | results. In this scenario, the <B |
| 1092 | CLASS="COMMAND" |
| 1093 | >read</B |
| 1094 | > |
| 1095 | acts as if it were running in a subshell. Instead, use |
| 1096 | the <A |
| 1097 | HREF="internal.html#SETREF" |
| 1098 | >set</A |
| 1099 | > command (as in <A |
| 1100 | HREF="internal.html#SETPOS" |
| 1101 | >Example 15-18</A |
| 1102 | >).</P |
| 1103 | ><DIV |
| 1104 | CLASS="EXAMPLE" |
| 1105 | ><HR><A |
| 1106 | NAME="BADREAD" |
| 1107 | ></A |
| 1108 | ><P |
| 1109 | ><B |
| 1110 | >Example 34-3. Piping the output of <I |
| 1111 | CLASS="FIRSTTERM" |
| 1112 | >echo</I |
| 1113 | > to a |
| 1114 | <I |
| 1115 | CLASS="FIRSTTERM" |
| 1116 | >read</I |
| 1117 | ></B |
| 1118 | ></P |
| 1119 | ><TABLE |
| 1120 | BORDER="0" |
| 1121 | BGCOLOR="#E0E0E0" |
| 1122 | WIDTH="90%" |
| 1123 | ><TR |
| 1124 | ><TD |
| 1125 | ><PRE |
| 1126 | CLASS="PROGRAMLISTING" |
| 1127 | > 1 #!/bin/bash |
| 1128 | 2 # badread.sh: |
| 1129 | 3 # Attempting to use 'echo and 'read' |
| 1130 | 4 #+ to assign variables non-interactively. |
| 1131 | 5 |
| 1132 | 6 # shopt -s lastpipe |
| 1133 | 7 |
| 1134 | 8 a=aaa |
| 1135 | 9 b=bbb |
| 1136 | 10 c=ccc |
| 1137 | 11 |
| 1138 | 12 echo "one two three" | read a b c |
| 1139 | 13 # Try to reassign a, b, and c. |
| 1140 | 14 |
| 1141 | 15 echo |
| 1142 | 16 echo "a = $a" # a = aaa |
| 1143 | 17 echo "b = $b" # b = bbb |
| 1144 | 18 echo "c = $c" # c = ccc |
| 1145 | 19 # Reassignment failed. |
| 1146 | 20 |
| 1147 | 21 ### However . . . |
| 1148 | 22 ## Uncommenting line 6: |
| 1149 | 23 # shopt -s lastpipe |
| 1150 | 24 ##+ fixes the problem! |
| 1151 | 25 ### This is a new feature in Bash, version 4.2. |
| 1152 | 26 |
| 1153 | 27 # ------------------------------ |
| 1154 | 28 |
| 1155 | 29 # Try the following alternative. |
| 1156 | 30 |
| 1157 | 31 var=`echo "one two three"` |
| 1158 | 32 set -- $var |
| 1159 | 33 a=$1; b=$2; c=$3 |
| 1160 | 34 |
| 1161 | 35 echo "-------" |
| 1162 | 36 echo "a = $a" # a = one |
| 1163 | 37 echo "b = $b" # b = two |
| 1164 | 38 echo "c = $c" # c = three |
| 1165 | 39 # Reassignment succeeded. |
| 1166 | 40 |
| 1167 | 41 # ------------------------------ |
| 1168 | 42 |
| 1169 | 43 # Note also that an echo to a 'read' works within a subshell. |
| 1170 | 44 # However, the value of the variable changes *only* within the subshell. |
| 1171 | 45 |
| 1172 | 46 a=aaa # Starting all over again. |
| 1173 | 47 b=bbb |
| 1174 | 48 c=ccc |
| 1175 | 49 |
| 1176 | 50 echo; echo |
| 1177 | 51 echo "one two three" | ( read a b c; |
| 1178 | 52 echo "Inside subshell: "; echo "a = $a"; echo "b = $b"; echo "c = $c" ) |
| 1179 | 53 # a = one |
| 1180 | 54 # b = two |
| 1181 | 55 # c = three |
| 1182 | 56 echo "-----------------" |
| 1183 | 57 echo "Outside subshell: " |
| 1184 | 58 echo "a = $a" # a = aaa |
| 1185 | 59 echo "b = $b" # b = bbb |
| 1186 | 60 echo "c = $c" # c = ccc |
| 1187 | 61 echo |
| 1188 | 62 |
| 1189 | 63 exit 0</PRE |
| 1190 | ></TD |
| 1191 | ></TR |
| 1192 | ></TABLE |
| 1193 | ><HR></DIV |
| 1194 | ><P |
| 1195 | ><A |
| 1196 | NAME="PIPELOOP" |
| 1197 | ></A |
| 1198 | ></P |
| 1199 | ><P |
| 1200 | >In fact, as Anthony Richardson points out, piping to |
| 1201 | <SPAN |
| 1202 | CLASS="emphasis" |
| 1203 | ><I |
| 1204 | CLASS="EMPHASIS" |
| 1205 | >any</I |
| 1206 | ></SPAN |
| 1207 | > loop can cause a similar problem.</P |
| 1208 | ><P |
| 1209 | > |
| 1210 | <TABLE |
| 1211 | BORDER="0" |
| 1212 | BGCOLOR="#E0E0E0" |
| 1213 | WIDTH="90%" |
| 1214 | ><TR |
| 1215 | ><TD |
| 1216 | ><PRE |
| 1217 | CLASS="PROGRAMLISTING" |
| 1218 | > 1 # Loop piping troubles. |
| 1219 | 2 # This example by Anthony Richardson, |
| 1220 | 3 #+ with addendum by Wilbert Berendsen. |
| 1221 | 4 |
| 1222 | 5 |
| 1223 | 6 foundone=false |
| 1224 | 7 find $HOME -type f -atime +30 -size 100k | |
| 1225 | 8 while true |
| 1226 | 9 do |
| 1227 | 10 read f |
| 1228 | 11 echo "$f is over 100KB and has not been accessed in over 30 days" |
| 1229 | 12 echo "Consider moving the file to archives." |
| 1230 | 13 foundone=true |
| 1231 | 14 # ------------------------------------ |
| 1232 | 15 echo "Subshell level = $BASH_SUBSHELL" |
| 1233 | 16 # Subshell level = 1 |
| 1234 | 17 # Yes, we're inside a subshell. |
| 1235 | 18 # ------------------------------------ |
| 1236 | 19 done |
| 1237 | 20 |
| 1238 | 21 # foundone will always be false here since it is |
| 1239 | 22 #+ set to true inside a subshell |
| 1240 | 23 if [ $foundone = false ] |
| 1241 | 24 then |
| 1242 | 25 echo "No files need archiving." |
| 1243 | 26 fi |
| 1244 | 27 |
| 1245 | 28 # =====================Now, here is the correct way:================= |
| 1246 | 29 |
| 1247 | 30 foundone=false |
| 1248 | 31 for f in $(find $HOME -type f -atime +30 -size 100k) # No pipe here. |
| 1249 | 32 do |
| 1250 | 33 echo "$f is over 100KB and has not been accessed in over 30 days" |
| 1251 | 34 echo "Consider moving the file to archives." |
| 1252 | 35 foundone=true |
| 1253 | 36 done |
| 1254 | 37 |
| 1255 | 38 if [ $foundone = false ] |
| 1256 | 39 then |
| 1257 | 40 echo "No files need archiving." |
| 1258 | 41 fi |
| 1259 | 42 |
| 1260 | 43 # ==================And here is another alternative================== |
| 1261 | 44 |
| 1262 | 45 # Places the part of the script that reads the variables |
| 1263 | 46 #+ within a code block, so they share the same subshell. |
| 1264 | 47 # Thank you, W.B. |
| 1265 | 48 |
| 1266 | 49 find $HOME -type f -atime +30 -size 100k | { |
| 1267 | 50 foundone=false |
| 1268 | 51 while read f |
| 1269 | 52 do |
| 1270 | 53 echo "$f is over 100KB and has not been accessed in over 30 days" |
| 1271 | 54 echo "Consider moving the file to archives." |
| 1272 | 55 foundone=true |
| 1273 | 56 done |
| 1274 | 57 |
| 1275 | 58 if ! $foundone |
| 1276 | 59 then |
| 1277 | 60 echo "No files need archiving." |
| 1278 | 61 fi |
| 1279 | 62 }</PRE |
| 1280 | ></TD |
| 1281 | ></TR |
| 1282 | ></TABLE |
| 1283 | > |
| 1284 | </P |
| 1285 | ><P |
| 1286 | ><A |
| 1287 | NAME="PTAILGREP" |
| 1288 | ></A |
| 1289 | ></P |
| 1290 | ><P |
| 1291 | > A lookalike problem occurs when trying to write the |
| 1292 | <TT |
| 1293 | CLASS="FILENAME" |
| 1294 | >stdout</TT |
| 1295 | > of a <B |
| 1296 | CLASS="COMMAND" |
| 1297 | >tail -f</B |
| 1298 | > |
| 1299 | piped to <A |
| 1300 | HREF="textproc.html#GREPREF" |
| 1301 | >grep</A |
| 1302 | >. |
| 1303 | <TABLE |
| 1304 | BORDER="0" |
| 1305 | BGCOLOR="#E0E0E0" |
| 1306 | WIDTH="90%" |
| 1307 | ><TR |
| 1308 | ><TD |
| 1309 | ><PRE |
| 1310 | CLASS="PROGRAMLISTING" |
| 1311 | > 1 tail -f /var/log/messages | grep "$ERROR_MSG" >> error.log |
| 1312 | 2 # The "error.log" file will not have anything written to it. |
| 1313 | 3 # As Samuli Kaipiainen points out, this results from grep |
| 1314 | 4 #+ buffering its output. |
| 1315 | 5 # The fix is to add the "--line-buffered" parameter to grep.</PRE |
| 1316 | ></TD |
| 1317 | ></TR |
| 1318 | ></TABLE |
| 1319 | > |
| 1320 | </P |
| 1321 | ></LI |
| 1322 | ><LI |
| 1323 | ><P |
| 1324 | ><A |
| 1325 | NAME="SUIDSCR" |
| 1326 | ></A |
| 1327 | ></P |
| 1328 | ><P |
| 1329 | >Using <SPAN |
| 1330 | CLASS="QUOTE" |
| 1331 | >"suid"</SPAN |
| 1332 | > commands within scripts is risky, |
| 1333 | as it may compromise system security. |
| 1334 | <A |
| 1335 | NAME="AEN19993" |
| 1336 | HREF="#FTN.AEN19993" |
| 1337 | >[1]</A |
| 1338 | > |
| 1339 | </P |
| 1340 | ></LI |
| 1341 | ><LI |
| 1342 | ><P |
| 1343 | ><A |
| 1344 | NAME="CGIREF" |
| 1345 | ></A |
| 1346 | ></P |
| 1347 | ><P |
| 1348 | >Using shell scripts for CGI programming may be problematic. Shell |
| 1349 | script variables are not <SPAN |
| 1350 | CLASS="QUOTE" |
| 1351 | >"typesafe,"</SPAN |
| 1352 | > and this can cause |
| 1353 | undesirable behavior as far as CGI is concerned. Moreover, it is |
| 1354 | difficult to <SPAN |
| 1355 | CLASS="QUOTE" |
| 1356 | >"cracker-proof"</SPAN |
| 1357 | > shell scripts.</P |
| 1358 | ></LI |
| 1359 | ><LI |
| 1360 | ><P |
| 1361 | >Bash does not handle the <A |
| 1362 | HREF="internal.html#DOUBLESLASHREF" |
| 1363 | >double slash |
| 1364 | (<SPAN |
| 1365 | CLASS="TOKEN" |
| 1366 | >//</SPAN |
| 1367 | >) string</A |
| 1368 | > correctly.</P |
| 1369 | ></LI |
| 1370 | ><LI |
| 1371 | ><P |
| 1372 | ><A |
| 1373 | NAME="GNUREF" |
| 1374 | ></A |
| 1375 | ></P |
| 1376 | ><P |
| 1377 | >Bash scripts written for Linux or BSD systems may need |
| 1378 | fixups to run on a commercial UNIX machine. Such |
| 1379 | scripts often employ the GNU set of commands and filters, |
| 1380 | which have greater functionality than their generic UNIX |
| 1381 | counterparts. This is particularly true of such text processing |
| 1382 | utilites as <A |
| 1383 | HREF="textproc.html#TRREF" |
| 1384 | >tr</A |
| 1385 | >.</P |
| 1386 | ></LI |
| 1387 | ><LI |
| 1388 | ><P |
| 1389 | ><A |
| 1390 | NAME="UPDATEBREAKS" |
| 1391 | ></A |
| 1392 | ></P |
| 1393 | ><P |
| 1394 | >Sadly, updates to Bash itself have broken older scripts |
| 1395 | that <A |
| 1396 | HREF="manipulatingvars.html#PARAGRAPHSPACE" |
| 1397 | >used to work perfectly |
| 1398 | fine</A |
| 1399 | >. Let us recall <A |
| 1400 | HREF="gotchas.html#UNDOCF" |
| 1401 | >how |
| 1402 | risky it is to use undocumented Bash features</A |
| 1403 | >.</P |
| 1404 | ></LI |
| 1405 | ></UL |
| 1406 | ><TABLE |
| 1407 | BORDER="0" |
| 1408 | WIDTH="100%" |
| 1409 | CELLSPACING="0" |
| 1410 | CELLPADDING="0" |
| 1411 | CLASS="EPIGRAPH" |
| 1412 | ><TR |
| 1413 | ><TD |
| 1414 | WIDTH="45%" |
| 1415 | > </TD |
| 1416 | ><TD |
| 1417 | WIDTH="45%" |
| 1418 | ALIGN="LEFT" |
| 1419 | VALIGN="TOP" |
| 1420 | ><I |
| 1421 | ><P |
| 1422 | ><I |
| 1423 | >Danger is near thee --</I |
| 1424 | ></P |
| 1425 | ><P |
| 1426 | ><I |
| 1427 | >Beware, beware, beware, beware.</I |
| 1428 | ></P |
| 1429 | ><P |
| 1430 | ><I |
| 1431 | >Many brave hearts are asleep in the deep.</I |
| 1432 | ></P |
| 1433 | ><P |
| 1434 | ><I |
| 1435 | >So beware --</I |
| 1436 | ></P |
| 1437 | ><P |
| 1438 | ><I |
| 1439 | >Beware.</I |
| 1440 | ></P |
| 1441 | ><P |
| 1442 | ><I |
| 1443 | >--A.J. Lamb and H.W. Petrie</I |
| 1444 | ></P |
| 1445 | ></I |
| 1446 | ></TD |
| 1447 | ></TR |
| 1448 | ></TABLE |
| 1449 | ></DIV |
| 1450 | ><H3 |
| 1451 | CLASS="FOOTNOTES" |
| 1452 | >Notes</H3 |
| 1453 | ><TABLE |
| 1454 | BORDER="0" |
| 1455 | CLASS="FOOTNOTES" |
| 1456 | WIDTH="100%" |
| 1457 | ><TR |
| 1458 | ><TD |
| 1459 | ALIGN="LEFT" |
| 1460 | VALIGN="TOP" |
| 1461 | WIDTH="5%" |
| 1462 | ><A |
| 1463 | NAME="FTN.AEN19993" |
| 1464 | HREF="gotchas.html#AEN19993" |
| 1465 | >[1]</A |
| 1466 | ></TD |
| 1467 | ><TD |
| 1468 | ALIGN="LEFT" |
| 1469 | VALIGN="TOP" |
| 1470 | WIDTH="95%" |
| 1471 | ><P |
| 1472 | >Setting the <A |
| 1473 | HREF="fto.html#SUIDREF" |
| 1474 | >suid</A |
| 1475 | > |
| 1476 | permission on the script itself has no effect in Linux |
| 1477 | and most other UNIX flavors.</P |
| 1478 | ></TD |
| 1479 | ></TR |
| 1480 | ></TABLE |
| 1481 | ><DIV |
| 1482 | CLASS="NAVFOOTER" |
| 1483 | ><HR |
| 1484 | ALIGN="LEFT" |
| 1485 | WIDTH="100%"><TABLE |
| 1486 | SUMMARY="Footer navigation table" |
| 1487 | WIDTH="100%" |
| 1488 | BORDER="0" |
| 1489 | CELLPADDING="0" |
| 1490 | CELLSPACING="0" |
| 1491 | ><TR |
| 1492 | ><TD |
| 1493 | WIDTH="33%" |
| 1494 | ALIGN="left" |
| 1495 | VALIGN="top" |
| 1496 | ><A |
| 1497 | HREF="options.html" |
| 1498 | ACCESSKEY="P" |
| 1499 | >Prev</A |
| 1500 | ></TD |
| 1501 | ><TD |
| 1502 | WIDTH="34%" |
| 1503 | ALIGN="center" |
| 1504 | VALIGN="top" |
| 1505 | ><A |
| 1506 | HREF="index.html" |
| 1507 | ACCESSKEY="H" |
| 1508 | >Home</A |
| 1509 | ></TD |
| 1510 | ><TD |
| 1511 | WIDTH="33%" |
| 1512 | ALIGN="right" |
| 1513 | VALIGN="top" |
| 1514 | ><A |
| 1515 | HREF="scrstyle.html" |
| 1516 | ACCESSKEY="N" |
| 1517 | >Next</A |
| 1518 | ></TD |
| 1519 | ></TR |
| 1520 | ><TR |
| 1521 | ><TD |
| 1522 | WIDTH="33%" |
| 1523 | ALIGN="left" |
| 1524 | VALIGN="top" |
| 1525 | >Options</TD |
| 1526 | ><TD |
| 1527 | WIDTH="34%" |
| 1528 | ALIGN="center" |
| 1529 | VALIGN="top" |
| 1530 | ><A |
| 1531 | HREF="part5.html" |
| 1532 | ACCESSKEY="U" |
| 1533 | >Up</A |
| 1534 | ></TD |
| 1535 | ><TD |
| 1536 | WIDTH="33%" |
| 1537 | ALIGN="right" |
| 1538 | VALIGN="top" |
| 1539 | >Scripting With Style</TD |
| 1540 | ></TR |
| 1541 | ></TABLE |
| 1542 | ></DIV |
| 1543 | ></BODY |
| 1544 | ></HTML |
| 1545 | > |