| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Portability Issues</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="Miscellany" |
| 15 | HREF="miscellany.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Security Issues" |
| 18 | HREF="securityissues.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Shell Scripting Under Windows" |
| 21 | HREF="winscript.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="SECT1" |
| 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="securityissues.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | >Chapter 36. Miscellany</TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="winscript.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="SECT1" |
| 92 | ><H1 |
| 93 | CLASS="SECT1" |
| 94 | ><A |
| 95 | NAME="PORTABILITYISSUES" |
| 96 | ></A |
| 97 | >36.9. Portability Issues</H1 |
| 98 | ><TABLE |
| 99 | BORDER="0" |
| 100 | WIDTH="100%" |
| 101 | CELLSPACING="0" |
| 102 | CELLPADDING="0" |
| 103 | CLASS="EPIGRAPH" |
| 104 | ><TR |
| 105 | ><TD |
| 106 | WIDTH="45%" |
| 107 | > </TD |
| 108 | ><TD |
| 109 | WIDTH="45%" |
| 110 | ALIGN="LEFT" |
| 111 | VALIGN="TOP" |
| 112 | ><I |
| 113 | ><P |
| 114 | ><I |
| 115 | >It is easier to port a shell than a shell script.</I |
| 116 | ></P |
| 117 | ><P |
| 118 | ><I |
| 119 | >--Larry Wall</I |
| 120 | ></P |
| 121 | ></I |
| 122 | ></TD |
| 123 | ></TR |
| 124 | ></TABLE |
| 125 | ><P |
| 126 | >This book deals specifically with Bash scripting on |
| 127 | a GNU/Linux system. All the same, users of <B |
| 128 | CLASS="COMMAND" |
| 129 | >sh</B |
| 130 | > |
| 131 | and <B |
| 132 | CLASS="COMMAND" |
| 133 | >ksh</B |
| 134 | > will find much of value here.</P |
| 135 | ><P |
| 136 | ><A |
| 137 | NAME="POSIX3REF" |
| 138 | ></A |
| 139 | >As it happens, many of the various |
| 140 | shells and scripting languages seem to be converging toward the |
| 141 | <A |
| 142 | HREF="sha-bang.html#POSIX2REF" |
| 143 | >POSIX</A |
| 144 | > 1003.2 standard. Invoking |
| 145 | Bash with the <TT |
| 146 | CLASS="OPTION" |
| 147 | >--posix</TT |
| 148 | > option or inserting |
| 149 | a <B |
| 150 | CLASS="COMMAND" |
| 151 | >set -o posix</B |
| 152 | > at the head of a script |
| 153 | causes Bash to conform very closely to this standard. Another |
| 154 | alternative is to use a <I |
| 155 | CLASS="FIRSTTERM" |
| 156 | >#!/bin/sh</I |
| 157 | > <A |
| 158 | HREF="sha-bang.html#SHABANGREF" |
| 159 | >sha-bang header</A |
| 160 | > in the script, |
| 161 | rather than <I |
| 162 | CLASS="FIRSTTERM" |
| 163 | >#!/bin/bash</I |
| 164 | >. |
| 165 | |
| 166 | <A |
| 167 | NAME="AEN20799" |
| 168 | HREF="#FTN.AEN20799" |
| 169 | >[1]</A |
| 170 | > |
| 171 | |
| 172 | Note that <TT |
| 173 | CLASS="FILENAME" |
| 174 | >/bin/sh</TT |
| 175 | > is a <A |
| 176 | HREF="external.html#LINKREF" |
| 177 | >link</A |
| 178 | > to <TT |
| 179 | CLASS="FILENAME" |
| 180 | >/bin/bash</TT |
| 181 | > |
| 182 | in Linux and certain other flavors of UNIX, and a script invoked |
| 183 | this way disables extended Bash functionality.</P |
| 184 | ><P |
| 185 | >Most Bash scripts will run as-is under |
| 186 | <B |
| 187 | CLASS="COMMAND" |
| 188 | >ksh</B |
| 189 | >, and vice-versa, since Chet Ramey has |
| 190 | been busily porting <B |
| 191 | CLASS="COMMAND" |
| 192 | >ksh</B |
| 193 | > features to the |
| 194 | latest versions of Bash.</P |
| 195 | ><P |
| 196 | >On a commercial UNIX machine, scripts using GNU-specific |
| 197 | features of standard commands may not work. This has become less |
| 198 | of a problem in the last few years, as the GNU utilities have |
| 199 | pretty much displaced their proprietary |
| 200 | counterparts even on <SPAN |
| 201 | CLASS="QUOTE" |
| 202 | >"big-iron"</SPAN |
| 203 | > UNIX. |
| 204 | <A |
| 205 | HREF="http://linux.oreillynet.com/pub/a/linux/2002/02/28/caldera.html" |
| 206 | TARGET="_top" |
| 207 | >Caldera's |
| 208 | release of the source</A |
| 209 | > to many of the original UNIX |
| 210 | utilities has accelerated the trend.</P |
| 211 | ><P |
| 212 | ><A |
| 213 | NAME="BASHCOMPAT" |
| 214 | ></A |
| 215 | ></P |
| 216 | ><P |
| 217 | >Bash has certain features that the traditional <A |
| 218 | HREF="why-shell.html#BASHDEF" |
| 219 | >Bourne shell</A |
| 220 | > lacks. Among these are: |
| 221 | |
| 222 | <UL |
| 223 | ><LI |
| 224 | ><P |
| 225 | >Certain extended <A |
| 226 | HREF="options.html#INVOCATIONOPTIONSREF" |
| 227 | >invocation options</A |
| 228 | ></P |
| 229 | ></LI |
| 230 | ><LI |
| 231 | ><P |
| 232 | ><A |
| 233 | HREF="commandsub.html#COMMANDSUBREF" |
| 234 | >Command substitution</A |
| 235 | > using |
| 236 | <B |
| 237 | CLASS="COMMAND" |
| 238 | >$( )</B |
| 239 | > notation</P |
| 240 | ></LI |
| 241 | ><LI |
| 242 | ><P |
| 243 | ><A |
| 244 | HREF="bashver3.html#BRACEEXPREF3" |
| 245 | >Brace expansion</A |
| 246 | ></P |
| 247 | ></LI |
| 248 | ><LI |
| 249 | ><P |
| 250 | >Certain <A |
| 251 | HREF="arrays.html#ARRAYREF" |
| 252 | >array</A |
| 253 | > operations, |
| 254 | and <A |
| 255 | HREF="bashver4.html#ASSOCARR" |
| 256 | >associative arrays</A |
| 257 | ></P |
| 258 | ></LI |
| 259 | ><LI |
| 260 | ><P |
| 261 | >The <A |
| 262 | HREF="tests.html#DBLBRACKETS" |
| 263 | >double brackets</A |
| 264 | > |
| 265 | extended test construct</P |
| 266 | ></LI |
| 267 | ><LI |
| 268 | ><P |
| 269 | >The <A |
| 270 | HREF="dblparens.html#DBLPARENSREF" |
| 271 | >double-parentheses</A |
| 272 | > |
| 273 | arithmetic-evaluation construct</P |
| 274 | ></LI |
| 275 | ><LI |
| 276 | ><P |
| 277 | >Certain <A |
| 278 | HREF="manipulatingvars.html#STRINGMANIP" |
| 279 | >string manipulation</A |
| 280 | > |
| 281 | operations</P |
| 282 | ></LI |
| 283 | ><LI |
| 284 | ><P |
| 285 | ><A |
| 286 | HREF="process-sub.html#PROCESSSUBREF" |
| 287 | >Process substitution</A |
| 288 | ></P |
| 289 | ></LI |
| 290 | ><LI |
| 291 | ><P |
| 292 | >A Regular Expression <A |
| 293 | HREF="bashver3.html#REGEXMATCHREF" |
| 294 | >matching |
| 295 | operator</A |
| 296 | ></P |
| 297 | ></LI |
| 298 | ><LI |
| 299 | ><P |
| 300 | >Bash-specific <A |
| 301 | HREF="internal.html#BUILTINREF" |
| 302 | >builtins</A |
| 303 | ></P |
| 304 | ></LI |
| 305 | ><LI |
| 306 | ><P |
| 307 | ><A |
| 308 | HREF="bashver4.html#COPROCREF" |
| 309 | >Coprocesses</A |
| 310 | ></P |
| 311 | ></LI |
| 312 | ></UL |
| 313 | > |
| 314 | </P |
| 315 | ><P |
| 316 | >See the <A |
| 317 | HREF="ftp://ftp.cwru.edu/pub/bash/FAQ" |
| 318 | TARGET="_top" |
| 319 | >Bash |
| 320 | F.A.Q.</A |
| 321 | > for a complete listing.</P |
| 322 | ><DIV |
| 323 | CLASS="SECT2" |
| 324 | ><H2 |
| 325 | CLASS="SECT2" |
| 326 | ><A |
| 327 | NAME="AEN20853" |
| 328 | ></A |
| 329 | >36.9.1. A Test Suite</H2 |
| 330 | ><P |
| 331 | ><A |
| 332 | NAME="TESTSUITE0" |
| 333 | ></A |
| 334 | >Let us illustrate some of the |
| 335 | incompatibilities between Bash and the classic |
| 336 | Bourne shell. Download and install the <A |
| 337 | HREF="http://freshmeat.net/projects/bournesh" |
| 338 | TARGET="_top" |
| 339 | ><SPAN |
| 340 | CLASS="QUOTE" |
| 341 | >"Heirloom |
| 342 | Bourne Shell"</SPAN |
| 343 | ></A |
| 344 | > and run the following |
| 345 | script, first using Bash, then the classic |
| 346 | <I |
| 347 | CLASS="FIRSTTERM" |
| 348 | >sh</I |
| 349 | >.</P |
| 350 | ><DIV |
| 351 | CLASS="EXAMPLE" |
| 352 | ><HR><A |
| 353 | NAME="TESTSUITE" |
| 354 | ></A |
| 355 | ><P |
| 356 | ><B |
| 357 | >Example 36-23. Test Suite</B |
| 358 | ></P |
| 359 | ><TABLE |
| 360 | BORDER="0" |
| 361 | BGCOLOR="#E0E0E0" |
| 362 | WIDTH="100%" |
| 363 | ><TR |
| 364 | ><TD |
| 365 | ><PRE |
| 366 | CLASS="PROGRAMLISTING" |
| 367 | > 1 #!/bin/bash |
| 368 | 2 # test-suite.sh |
| 369 | 3 # A partial Bash compatibility test suite. |
| 370 | 4 # Run this on your version of Bash, or some other shell. |
| 371 | 5 |
| 372 | 6 default_option=FAIL # Tests below will fail unless . . . |
| 373 | 7 |
| 374 | 8 echo |
| 375 | 9 echo -n "Testing " |
| 376 | 10 sleep 1; echo -n ". " |
| 377 | 11 sleep 1; echo -n ". " |
| 378 | 12 sleep 1; echo ". " |
| 379 | 13 echo |
| 380 | 14 |
| 381 | 15 # Double brackets |
| 382 | 16 String="Double brackets supported?" |
| 383 | 17 echo -n "Double brackets test: " |
| 384 | 18 if [[ "$String" = "Double brackets supported?" ]] |
| 385 | 19 then |
| 386 | 20 echo "PASS" |
| 387 | 21 else |
| 388 | 22 echo "FAIL" |
| 389 | 23 fi |
| 390 | 24 |
| 391 | 25 |
| 392 | 26 # Double brackets and regex matching |
| 393 | 27 String="Regex matching supported?" |
| 394 | 28 echo -n "Regex matching: " |
| 395 | 29 if [[ "$String" =~ R.....matching* ]] |
| 396 | 30 then |
| 397 | 31 echo "PASS" |
| 398 | 32 else |
| 399 | 33 echo "FAIL" |
| 400 | 34 fi |
| 401 | 35 |
| 402 | 36 |
| 403 | 37 # Arrays |
| 404 | 38 test_arr=$default_option # FAIL |
| 405 | 39 Array=( If supports arrays will print PASS ) |
| 406 | 40 test_arr=${Array[5]} |
| 407 | 41 echo "Array test: $test_arr" |
| 408 | 42 |
| 409 | 43 |
| 410 | 44 # Command Substitution |
| 411 | 45 csub_test () |
| 412 | 46 { |
| 413 | 47 echo "PASS" |
| 414 | 48 } |
| 415 | 49 |
| 416 | 50 test_csub=$default_option # FAIL |
| 417 | 51 test_csub=$(csub_test) |
| 418 | 52 echo "Command substitution test: $test_csub" |
| 419 | 53 |
| 420 | 54 echo |
| 421 | 55 |
| 422 | 56 # Completing this script is an exercise for the reader. |
| 423 | 57 # Add to the above similar tests for double parentheses, |
| 424 | 58 #+ brace expansion, process substitution, etc. |
| 425 | 59 |
| 426 | 60 exit $?</PRE |
| 427 | ></TD |
| 428 | ></TR |
| 429 | ></TABLE |
| 430 | ><HR></DIV |
| 431 | ></DIV |
| 432 | ></DIV |
| 433 | ><H3 |
| 434 | CLASS="FOOTNOTES" |
| 435 | >Notes</H3 |
| 436 | ><TABLE |
| 437 | BORDER="0" |
| 438 | CLASS="FOOTNOTES" |
| 439 | WIDTH="100%" |
| 440 | ><TR |
| 441 | ><TD |
| 442 | ALIGN="LEFT" |
| 443 | VALIGN="TOP" |
| 444 | WIDTH="5%" |
| 445 | ><A |
| 446 | NAME="FTN.AEN20799" |
| 447 | HREF="portabilityissues.html#AEN20799" |
| 448 | >[1]</A |
| 449 | ></TD |
| 450 | ><TD |
| 451 | ALIGN="LEFT" |
| 452 | VALIGN="TOP" |
| 453 | WIDTH="95%" |
| 454 | ><P |
| 455 | >Or, better yet, <A |
| 456 | HREF="system.html#ENVV2REF" |
| 457 | >#!/bin/env sh</A |
| 458 | >.</P |
| 459 | ></TD |
| 460 | ></TR |
| 461 | ></TABLE |
| 462 | ><DIV |
| 463 | CLASS="NAVFOOTER" |
| 464 | ><HR |
| 465 | ALIGN="LEFT" |
| 466 | WIDTH="100%"><TABLE |
| 467 | SUMMARY="Footer navigation table" |
| 468 | WIDTH="100%" |
| 469 | BORDER="0" |
| 470 | CELLPADDING="0" |
| 471 | CELLSPACING="0" |
| 472 | ><TR |
| 473 | ><TD |
| 474 | WIDTH="33%" |
| 475 | ALIGN="left" |
| 476 | VALIGN="top" |
| 477 | ><A |
| 478 | HREF="securityissues.html" |
| 479 | ACCESSKEY="P" |
| 480 | >Prev</A |
| 481 | ></TD |
| 482 | ><TD |
| 483 | WIDTH="34%" |
| 484 | ALIGN="center" |
| 485 | VALIGN="top" |
| 486 | ><A |
| 487 | HREF="index.html" |
| 488 | ACCESSKEY="H" |
| 489 | >Home</A |
| 490 | ></TD |
| 491 | ><TD |
| 492 | WIDTH="33%" |
| 493 | ALIGN="right" |
| 494 | VALIGN="top" |
| 495 | ><A |
| 496 | HREF="winscript.html" |
| 497 | ACCESSKEY="N" |
| 498 | >Next</A |
| 499 | ></TD |
| 500 | ></TR |
| 501 | ><TR |
| 502 | ><TD |
| 503 | WIDTH="33%" |
| 504 | ALIGN="left" |
| 505 | VALIGN="top" |
| 506 | >Security Issues</TD |
| 507 | ><TD |
| 508 | WIDTH="34%" |
| 509 | ALIGN="center" |
| 510 | VALIGN="top" |
| 511 | ><A |
| 512 | HREF="miscellany.html" |
| 513 | ACCESSKEY="U" |
| 514 | >Up</A |
| 515 | ></TD |
| 516 | ><TD |
| 517 | WIDTH="33%" |
| 518 | ALIGN="right" |
| 519 | VALIGN="top" |
| 520 | >Shell Scripting Under Windows</TD |
| 521 | ></TR |
| 522 | ></TABLE |
| 523 | ></DIV |
| 524 | ></BODY |
| 525 | ></HTML |
| 526 | > |