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