| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Shell Wrappers</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="Miscellany" |
| 18 | HREF="miscellany.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Tests and Comparisons: Alternatives" |
| 21 | HREF="testsandcomparisons.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="miscellany.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="testsandcomparisons.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="WRAPPER" |
| 96 | ></A |
| 97 | >36.2. Shell Wrappers</H1 |
| 98 | ><P |
| 99 | ><A |
| 100 | NAME="SHWRAPPER" |
| 101 | ></A |
| 102 | ></P |
| 103 | ><P |
| 104 | >A <I |
| 105 | CLASS="FIRSTTERM" |
| 106 | >wrapper</I |
| 107 | > is a shell script that embeds |
| 108 | a system command or utility, that accepts and passes a set of |
| 109 | parameters to that command. |
| 110 | |
| 111 | <A |
| 112 | NAME="AEN20130" |
| 113 | HREF="#FTN.AEN20130" |
| 114 | >[1]</A |
| 115 | > |
| 116 | |
| 117 | Wrapping a script around a complex command-line |
| 118 | simplifies invoking it. This is expecially useful |
| 119 | with <A |
| 120 | HREF="sedawk.html#SEDREF" |
| 121 | >sed</A |
| 122 | > and <A |
| 123 | HREF="awk.html#AWKREF" |
| 124 | >awk</A |
| 125 | >.</P |
| 126 | ><P |
| 127 | >A |
| 128 | <B |
| 129 | CLASS="COMMAND" |
| 130 | > sed</B |
| 131 | > or |
| 132 | <B |
| 133 | CLASS="COMMAND" |
| 134 | > |
| 135 | awk</B |
| 136 | > script would normally be invoked |
| 137 | from the command-line by a <TT |
| 138 | CLASS="USERINPUT" |
| 139 | ><B |
| 140 | >sed -e |
| 141 | <TT |
| 142 | CLASS="REPLACEABLE" |
| 143 | ><I |
| 144 | >'commands'</I |
| 145 | ></TT |
| 146 | ></B |
| 147 | ></TT |
| 148 | > |
| 149 | or <TT |
| 150 | CLASS="USERINPUT" |
| 151 | ><B |
| 152 | >awk |
| 153 | <TT |
| 154 | CLASS="REPLACEABLE" |
| 155 | ><I |
| 156 | >'commands'</I |
| 157 | ></TT |
| 158 | ></B |
| 159 | ></TT |
| 160 | >. Embedding |
| 161 | such a script in a Bash script permits calling it more simply, |
| 162 | and makes it <I |
| 163 | CLASS="FIRSTTERM" |
| 164 | >reusable</I |
| 165 | >. This also |
| 166 | enables combining the functionality of <I |
| 167 | CLASS="FIRSTTERM" |
| 168 | >sed</I |
| 169 | > |
| 170 | and <I |
| 171 | CLASS="FIRSTTERM" |
| 172 | >awk</I |
| 173 | >, for example <A |
| 174 | HREF="special-chars.html#PIPEREF" |
| 175 | >piping</A |
| 176 | > the output of a set of |
| 177 | <I |
| 178 | CLASS="FIRSTTERM" |
| 179 | >sed</I |
| 180 | > commands to |
| 181 | <I |
| 182 | CLASS="FIRSTTERM" |
| 183 | >awk</I |
| 184 | >. As a saved executable file, |
| 185 | you can then repeatedly invoke it in its original form or |
| 186 | modified, without the inconvenience of retyping it on the |
| 187 | command-line.</P |
| 188 | ><DIV |
| 189 | CLASS="EXAMPLE" |
| 190 | ><HR><A |
| 191 | NAME="EX3" |
| 192 | ></A |
| 193 | ><P |
| 194 | ><B |
| 195 | >Example 36-1. <I |
| 196 | CLASS="FIRSTTERM" |
| 197 | >shell wrapper</I |
| 198 | ></B |
| 199 | ></P |
| 200 | ><TABLE |
| 201 | BORDER="0" |
| 202 | BGCOLOR="#E0E0E0" |
| 203 | WIDTH="100%" |
| 204 | ><TR |
| 205 | ><TD |
| 206 | ><PRE |
| 207 | CLASS="PROGRAMLISTING" |
| 208 | > 1 #!/bin/bash |
| 209 | 2 |
| 210 | 3 # This simple script removes blank lines from a file. |
| 211 | 4 # No argument checking. |
| 212 | 5 # |
| 213 | 6 # You might wish to add something like: |
| 214 | 7 # |
| 215 | 8 # E_NOARGS=85 |
| 216 | 9 # if [ -z "$1" ] |
| 217 | 10 # then |
| 218 | 11 # echo "Usage: `basename $0` target-file" |
| 219 | 12 # exit $E_NOARGS |
| 220 | 13 # fi |
| 221 | 14 |
| 222 | 15 |
| 223 | 16 |
| 224 | 17 sed -e /^$/d "$1" |
| 225 | 18 # Same as |
| 226 | 19 # sed -e '/^$/d' filename |
| 227 | 20 # invoked from the command-line. |
| 228 | 21 |
| 229 | 22 # The '-e' means an "editing" command follows (optional here). |
| 230 | 23 # '^' indicates the beginning of line, '$' the end. |
| 231 | 24 # This matches lines with nothing between the beginning and the end -- |
| 232 | 25 #+ blank lines. |
| 233 | 26 # The 'd' is the delete command. |
| 234 | 27 |
| 235 | 28 # Quoting the command-line arg permits |
| 236 | 29 #+ whitespace and special characters in the filename. |
| 237 | 30 |
| 238 | 31 # Note that this script doesn't actually change the target file. |
| 239 | 32 # If you need to do that, redirect its output. |
| 240 | 33 |
| 241 | 34 exit</PRE |
| 242 | ></TD |
| 243 | ></TR |
| 244 | ></TABLE |
| 245 | ><HR></DIV |
| 246 | ><DIV |
| 247 | CLASS="EXAMPLE" |
| 248 | ><HR><A |
| 249 | NAME="EX4" |
| 250 | ></A |
| 251 | ><P |
| 252 | ><B |
| 253 | >Example 36-2. A slightly more complex <I |
| 254 | CLASS="FIRSTTERM" |
| 255 | >shell |
| 256 | wrapper</I |
| 257 | ></B |
| 258 | ></P |
| 259 | ><TABLE |
| 260 | BORDER="0" |
| 261 | BGCOLOR="#E0E0E0" |
| 262 | WIDTH="100%" |
| 263 | ><TR |
| 264 | ><TD |
| 265 | ><PRE |
| 266 | CLASS="PROGRAMLISTING" |
| 267 | > 1 #!/bin/bash |
| 268 | 2 |
| 269 | 3 # subst.sh: a script that substitutes one pattern for |
| 270 | 4 #+ another in a file, |
| 271 | 5 #+ i.e., "sh subst.sh Smith Jones letter.txt". |
| 272 | 6 # Jones replaces Smith. |
| 273 | 7 |
| 274 | 8 ARGS=3 # Script requires 3 arguments. |
| 275 | 9 E_BADARGS=85 # Wrong number of arguments passed to script. |
| 276 | 10 |
| 277 | 11 if [ $# -ne "$ARGS" ] |
| 278 | 12 then |
| 279 | 13 echo "Usage: `basename $0` old-pattern new-pattern filename" |
| 280 | 14 exit $E_BADARGS |
| 281 | 15 fi |
| 282 | 16 |
| 283 | 17 old_pattern=$1 |
| 284 | 18 new_pattern=$2 |
| 285 | 19 |
| 286 | 20 if [ -f "$3" ] |
| 287 | 21 then |
| 288 | 22 file_name=$3 |
| 289 | 23 else |
| 290 | 24 echo "File \"$3\" does not exist." |
| 291 | 25 exit $E_BADARGS |
| 292 | 26 fi |
| 293 | 27 |
| 294 | 28 |
| 295 | 29 # ----------------------------------------------- |
| 296 | 30 # Here is where the heavy work gets done. |
| 297 | 31 sed -e "s/$old_pattern/$new_pattern/g" $file_name |
| 298 | 32 # ----------------------------------------------- |
| 299 | 33 |
| 300 | 34 # 's' is, of course, the substitute command in sed, |
| 301 | 35 #+ and /pattern/ invokes address matching. |
| 302 | 36 # The 'g,' or global flag causes substitution for EVERY |
| 303 | 37 #+ occurence of $old_pattern on each line, not just the first. |
| 304 | 38 # Read the 'sed' docs for an in-depth explanation. |
| 305 | 39 |
| 306 | 40 exit $? # Redirect the output of this script to write to a file.</PRE |
| 307 | ></TD |
| 308 | ></TR |
| 309 | ></TABLE |
| 310 | ><HR></DIV |
| 311 | ><DIV |
| 312 | CLASS="EXAMPLE" |
| 313 | ><HR><A |
| 314 | NAME="LOGGINGWRAPPER" |
| 315 | ></A |
| 316 | ><P |
| 317 | ><B |
| 318 | >Example 36-3. A generic <I |
| 319 | CLASS="FIRSTTERM" |
| 320 | >shell wrapper</I |
| 321 | > that |
| 322 | writes to a logfile</B |
| 323 | ></P |
| 324 | ><TABLE |
| 325 | BORDER="0" |
| 326 | BGCOLOR="#E0E0E0" |
| 327 | WIDTH="100%" |
| 328 | ><TR |
| 329 | ><TD |
| 330 | ><PRE |
| 331 | CLASS="PROGRAMLISTING" |
| 332 | > 1 #!/bin/bash |
| 333 | 2 # logging-wrapper.sh |
| 334 | 3 # Generic shell wrapper that performs an operation |
| 335 | 4 #+ and logs it. |
| 336 | 5 |
| 337 | 6 DEFAULT_LOGFILE=logfile.txt |
| 338 | 7 |
| 339 | 8 # Set the following two variables. |
| 340 | 9 OPERATION= |
| 341 | 10 # Can be a complex chain of commands, |
| 342 | 11 #+ for example an awk script or a pipe . . . |
| 343 | 12 |
| 344 | 13 LOGFILE= |
| 345 | 14 if [ -z "$LOGFILE" ] |
| 346 | 15 then # If not set, default to ... |
| 347 | 16 LOGFILE="$DEFAULT_LOGFILE" |
| 348 | 17 fi |
| 349 | 18 |
| 350 | 19 # Command-line arguments, if any, for the operation. |
| 351 | 20 OPTIONS="$@" |
| 352 | 21 |
| 353 | 22 |
| 354 | 23 # Log it. |
| 355 | 24 echo "`date` + `whoami` + $OPERATION "$@"" >> $LOGFILE |
| 356 | 25 # Now, do it. |
| 357 | 26 exec $OPERATION "$@" |
| 358 | 27 |
| 359 | 28 # It's necessary to do the logging before the operation. |
| 360 | 29 # Why?</PRE |
| 361 | ></TD |
| 362 | ></TR |
| 363 | ></TABLE |
| 364 | ><HR></DIV |
| 365 | ><DIV |
| 366 | CLASS="EXAMPLE" |
| 367 | ><HR><A |
| 368 | NAME="PRASC" |
| 369 | ></A |
| 370 | ><P |
| 371 | ><B |
| 372 | >Example 36-4. A <I |
| 373 | CLASS="FIRSTTERM" |
| 374 | >shell wrapper</I |
| 375 | > around an awk |
| 376 | script</B |
| 377 | ></P |
| 378 | ><TABLE |
| 379 | BORDER="0" |
| 380 | BGCOLOR="#E0E0E0" |
| 381 | WIDTH="100%" |
| 382 | ><TR |
| 383 | ><TD |
| 384 | ><PRE |
| 385 | CLASS="PROGRAMLISTING" |
| 386 | > 1 #!/bin/bash |
| 387 | 2 # pr-ascii.sh: Prints a table of ASCII characters. |
| 388 | 3 |
| 389 | 4 START=33 # Range of printable ASCII characters (decimal). |
| 390 | 5 END=127 # Will not work for unprintable characters (> 127). |
| 391 | 6 |
| 392 | 7 echo " Decimal Hex Character" # Header. |
| 393 | 8 echo " ------- --- ---------" |
| 394 | 9 |
| 395 | 10 for ((i=START; i<=END; i++)) |
| 396 | 11 do |
| 397 | 12 echo $i | awk '{printf(" %3d %2x %c\n", $1, $1, $1)}' |
| 398 | 13 # The Bash printf builtin will not work in this context: |
| 399 | 14 # printf "%c" "$i" |
| 400 | 15 done |
| 401 | 16 |
| 402 | 17 exit 0 |
| 403 | 18 |
| 404 | 19 |
| 405 | 20 # Decimal Hex Character |
| 406 | 21 # ------- --- --------- |
| 407 | 22 # 33 21 ! |
| 408 | 23 # 34 22 " |
| 409 | 24 # 35 23 # |
| 410 | 25 # 36 24 $ |
| 411 | 26 # |
| 412 | 27 # . . . |
| 413 | 28 # |
| 414 | 29 # 122 7a z |
| 415 | 30 # 123 7b { |
| 416 | 31 # 124 7c | |
| 417 | 32 # 125 7d } |
| 418 | 33 |
| 419 | 34 |
| 420 | 35 # Redirect the output of this script to a file |
| 421 | 36 #+ or pipe it to "more": sh pr-asc.sh | more</PRE |
| 422 | ></TD |
| 423 | ></TR |
| 424 | ></TABLE |
| 425 | ><HR></DIV |
| 426 | ><DIV |
| 427 | CLASS="EXAMPLE" |
| 428 | ><HR><A |
| 429 | NAME="COLTOTALER" |
| 430 | ></A |
| 431 | ><P |
| 432 | ><B |
| 433 | >Example 36-5. A <I |
| 434 | CLASS="FIRSTTERM" |
| 435 | >shell wrapper</I |
| 436 | > around another |
| 437 | awk script</B |
| 438 | ></P |
| 439 | ><TABLE |
| 440 | BORDER="0" |
| 441 | BGCOLOR="#E0E0E0" |
| 442 | WIDTH="100%" |
| 443 | ><TR |
| 444 | ><TD |
| 445 | ><PRE |
| 446 | CLASS="PROGRAMLISTING" |
| 447 | > 1 #!/bin/bash |
| 448 | 2 |
| 449 | 3 # Adds up a specified column (of numbers) in the target file. |
| 450 | 4 # Floating-point (decimal) numbers okay, because awk can handle them. |
| 451 | 5 |
| 452 | 6 ARGS=2 |
| 453 | 7 E_WRONGARGS=85 |
| 454 | 8 |
| 455 | 9 if [ $# -ne "$ARGS" ] # Check for proper number of command-line args. |
| 456 | 10 then |
| 457 | 11 echo "Usage: `basename $0` filename column-number" |
| 458 | 12 exit $E_WRONGARGS |
| 459 | 13 fi |
| 460 | 14 |
| 461 | 15 filename=$1 |
| 462 | 16 column_number=$2 |
| 463 | 17 |
| 464 | 18 # Passing shell variables to the awk part of the script is a bit tricky. |
| 465 | 19 # One method is to strong-quote the Bash-script variable |
| 466 | 20 #+ within the awk script. |
| 467 | 21 # $'$BASH_SCRIPT_VAR' |
| 468 | 22 # ^ ^ |
| 469 | 23 # This is done in the embedded awk script below. |
| 470 | 24 # See the awk documentation for more details. |
| 471 | 25 |
| 472 | 26 # A multi-line awk script is here invoked by |
| 473 | 27 # awk ' |
| 474 | 28 # ... |
| 475 | 29 # ... |
| 476 | 30 # ... |
| 477 | 31 # ' |
| 478 | 32 |
| 479 | 33 |
| 480 | 34 # Begin awk script. |
| 481 | 35 # ----------------------------- |
| 482 | 36 awk ' |
| 483 | 37 |
| 484 | 38 { total += $'"${column_number}"' |
| 485 | 39 } |
| 486 | 40 END { |
| 487 | 41 print total |
| 488 | 42 } |
| 489 | 43 |
| 490 | 44 ' "$filename" |
| 491 | 45 # ----------------------------- |
| 492 | 46 # End awk script. |
| 493 | 47 |
| 494 | 48 |
| 495 | 49 # It may not be safe to pass shell variables to an embedded awk script, |
| 496 | 50 #+ so Stephane Chazelas proposes the following alternative: |
| 497 | 51 # --------------------------------------- |
| 498 | 52 # awk -v column_number="$column_number" ' |
| 499 | 53 # { total += $column_number |
| 500 | 54 # } |
| 501 | 55 # END { |
| 502 | 56 # print total |
| 503 | 57 # }' "$filename" |
| 504 | 58 # --------------------------------------- |
| 505 | 59 |
| 506 | 60 |
| 507 | 61 exit 0</PRE |
| 508 | ></TD |
| 509 | ></TR |
| 510 | ></TABLE |
| 511 | ><HR></DIV |
| 512 | ><P |
| 513 | ><A |
| 514 | NAME="PERLREF" |
| 515 | ></A |
| 516 | >For those scripts needing a single |
| 517 | do-it-all tool, a Swiss army knife, there is |
| 518 | <I |
| 519 | CLASS="FIRSTTERM" |
| 520 | >Perl</I |
| 521 | >. Perl combines the |
| 522 | capabilities of <A |
| 523 | HREF="sedawk.html#SEDREF" |
| 524 | >sed</A |
| 525 | > and <A |
| 526 | HREF="awk.html#AWKREF" |
| 527 | >awk</A |
| 528 | >, and throws in a large subset of |
| 529 | <B |
| 530 | CLASS="COMMAND" |
| 531 | >C</B |
| 532 | >, to boot. It is modular and contains support |
| 533 | for everything ranging from object-oriented programming up to and |
| 534 | including the kitchen sink. Short Perl scripts lend themselves to |
| 535 | embedding within shell scripts, and there may be some substance |
| 536 | to the claim that Perl can totally replace shell scripting |
| 537 | (though the author of the <SPAN |
| 538 | CLASS="emphasis" |
| 539 | ><I |
| 540 | CLASS="EMPHASIS" |
| 541 | >ABS Guide</I |
| 542 | ></SPAN |
| 543 | > remains |
| 544 | skeptical).</P |
| 545 | ><P |
| 546 | ><A |
| 547 | NAME="PERLEMB" |
| 548 | ></A |
| 549 | ></P |
| 550 | ><DIV |
| 551 | CLASS="EXAMPLE" |
| 552 | ><HR><A |
| 553 | NAME="EX56" |
| 554 | ></A |
| 555 | ><P |
| 556 | ><B |
| 557 | >Example 36-6. Perl embedded in a <I |
| 558 | CLASS="FIRSTTERM" |
| 559 | >Bash</I |
| 560 | > script</B |
| 561 | ></P |
| 562 | ><TABLE |
| 563 | BORDER="0" |
| 564 | BGCOLOR="#E0E0E0" |
| 565 | WIDTH="100%" |
| 566 | ><TR |
| 567 | ><TD |
| 568 | ><PRE |
| 569 | CLASS="PROGRAMLISTING" |
| 570 | > 1 #!/bin/bash |
| 571 | 2 |
| 572 | 3 # Shell commands may precede the Perl script. |
| 573 | 4 echo "This precedes the embedded Perl script within \"$0\"." |
| 574 | 5 echo "===============================================================" |
| 575 | 6 |
| 576 | 7 perl -e 'print "This line prints from an embedded Perl script.\n";' |
| 577 | 8 # Like sed, Perl also uses the "-e" option. |
| 578 | 9 |
| 579 | 10 echo "===============================================================" |
| 580 | 11 echo "However, the script may also contain shell and system commands." |
| 581 | 12 |
| 582 | 13 exit 0</PRE |
| 583 | ></TD |
| 584 | ></TR |
| 585 | ></TABLE |
| 586 | ><HR></DIV |
| 587 | ><P |
| 588 | >It is even possible to combine a Bash script and Perl script |
| 589 | within the same file. Depending on how the script is invoked, either |
| 590 | the Bash part or the Perl part will execute.</P |
| 591 | ><P |
| 592 | ><A |
| 593 | NAME="BASHANDPERL0" |
| 594 | ></A |
| 595 | ></P |
| 596 | ><DIV |
| 597 | CLASS="EXAMPLE" |
| 598 | ><HR><A |
| 599 | NAME="BASHANDPERL" |
| 600 | ></A |
| 601 | ><P |
| 602 | ><B |
| 603 | >Example 36-7. Bash and Perl scripts combined</B |
| 604 | ></P |
| 605 | ><TABLE |
| 606 | BORDER="0" |
| 607 | BGCOLOR="#E0E0E0" |
| 608 | WIDTH="100%" |
| 609 | ><TR |
| 610 | ><TD |
| 611 | ><PRE |
| 612 | CLASS="PROGRAMLISTING" |
| 613 | > 1 #!/bin/bash |
| 614 | 2 # bashandperl.sh |
| 615 | 3 |
| 616 | 4 echo "Greetings from the Bash part of the script, $0." |
| 617 | 5 # More Bash commands may follow here. |
| 618 | 6 |
| 619 | 7 exit |
| 620 | 8 # End of Bash part of the script. |
| 621 | 9 |
| 622 | 10 # ======================================================= |
| 623 | 11 |
| 624 | 12 #!/usr/bin/perl |
| 625 | 13 # This part of the script must be invoked with |
| 626 | 14 # perl -x bashandperl.sh |
| 627 | 15 |
| 628 | 16 print "Greetings from the Perl part of the script, $0.\n"; |
| 629 | 17 # Perl doesn't seem to like "echo" ... |
| 630 | 18 # More Perl commands may follow here. |
| 631 | 19 |
| 632 | 20 # End of Perl part of the script.</PRE |
| 633 | ></TD |
| 634 | ></TR |
| 635 | ></TABLE |
| 636 | ><HR></DIV |
| 637 | ><P |
| 638 | > <TABLE |
| 639 | BORDER="0" |
| 640 | BGCOLOR="#E0E0E0" |
| 641 | WIDTH="100%" |
| 642 | ><TR |
| 643 | ><TD |
| 644 | ><PRE |
| 645 | CLASS="SCREEN" |
| 646 | > <TT |
| 647 | CLASS="PROMPT" |
| 648 | >bash$ </TT |
| 649 | ><TT |
| 650 | CLASS="USERINPUT" |
| 651 | ><B |
| 652 | >bash bashandperl.sh</B |
| 653 | ></TT |
| 654 | > |
| 655 | <TT |
| 656 | CLASS="COMPUTEROUTPUT" |
| 657 | >Greetings from the Bash part of the script.</TT |
| 658 | > |
| 659 | |
| 660 | |
| 661 | <TT |
| 662 | CLASS="PROMPT" |
| 663 | >bash$ </TT |
| 664 | ><TT |
| 665 | CLASS="USERINPUT" |
| 666 | ><B |
| 667 | >perl -x bashandperl.sh</B |
| 668 | ></TT |
| 669 | > |
| 670 | <TT |
| 671 | CLASS="COMPUTEROUTPUT" |
| 672 | >Greetings from the Perl part of the script.</TT |
| 673 | > |
| 674 | </PRE |
| 675 | ></TD |
| 676 | ></TR |
| 677 | ></TABLE |
| 678 | > |
| 679 | </P |
| 680 | ><P |
| 681 | >It is, of course, possible to embed even more exotic scripting |
| 682 | languages within shell wrappers. <I |
| 683 | CLASS="FIRSTTERM" |
| 684 | >Python</I |
| 685 | >, |
| 686 | for example ...</P |
| 687 | ><P |
| 688 | ><A |
| 689 | NAME="PYTHONEMB" |
| 690 | ></A |
| 691 | ></P |
| 692 | ><DIV |
| 693 | CLASS="EXAMPLE" |
| 694 | ><HR><A |
| 695 | NAME="EX56PY" |
| 696 | ></A |
| 697 | ><P |
| 698 | ><B |
| 699 | >Example 36-8. Python embedded in a <I |
| 700 | CLASS="FIRSTTERM" |
| 701 | >Bash</I |
| 702 | > script</B |
| 703 | ></P |
| 704 | ><TABLE |
| 705 | BORDER="0" |
| 706 | BGCOLOR="#E0E0E0" |
| 707 | WIDTH="100%" |
| 708 | ><TR |
| 709 | ><TD |
| 710 | ><PRE |
| 711 | CLASS="PROGRAMLISTING" |
| 712 | > 1 #!/bin/bash |
| 713 | 2 # ex56py.sh |
| 714 | 3 |
| 715 | 4 # Shell commands may precede the Python script. |
| 716 | 5 echo "This precedes the embedded Python script within \"$0.\"" |
| 717 | 6 echo "===============================================================" |
| 718 | 7 |
| 719 | 8 python -c 'print "This line prints from an embedded Python script.\n";' |
| 720 | 9 # Unlike sed and perl, Python uses the "-c" option. |
| 721 | 10 python -c 'k = raw_input( "Hit a key to exit to outer script. " )' |
| 722 | 11 |
| 723 | 12 echo "===============================================================" |
| 724 | 13 echo "However, the script may also contain shell and system commands." |
| 725 | 14 |
| 726 | 15 exit 0</PRE |
| 727 | ></TD |
| 728 | ></TR |
| 729 | ></TABLE |
| 730 | ><HR></DIV |
| 731 | ><P |
| 732 | >Wrapping a script around <I |
| 733 | CLASS="FIRSTTERM" |
| 734 | >mplayer</I |
| 735 | > |
| 736 | and the Google's translation server, you can create something |
| 737 | that talks back to you.</P |
| 738 | ><P |
| 739 | ><A |
| 740 | NAME="SPEECH00" |
| 741 | ></A |
| 742 | ></P |
| 743 | ><DIV |
| 744 | CLASS="EXAMPLE" |
| 745 | ><HR><A |
| 746 | NAME="SPEECH0" |
| 747 | ></A |
| 748 | ><P |
| 749 | ><B |
| 750 | >Example 36-9. A script that speaks</B |
| 751 | ></P |
| 752 | ><TABLE |
| 753 | BORDER="0" |
| 754 | BGCOLOR="#E0E0E0" |
| 755 | WIDTH="100%" |
| 756 | ><TR |
| 757 | ><TD |
| 758 | ><PRE |
| 759 | CLASS="PROGRAMLISTING" |
| 760 | > 1 #!/bin/bash |
| 761 | 2 # Courtesy of: |
| 762 | 3 # http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis) |
| 763 | 4 |
| 764 | 5 # You must be on-line for this script to work, |
| 765 | 6 #+ so you can access the Google translation server. |
| 766 | 7 # Of course, mplayer must be present on your computer. |
| 767 | 8 |
| 768 | 9 speak() |
| 769 | 10 { |
| 770 | 11 local IFS=+ |
| 771 | 12 # Invoke mplayer, then connect to Google translation server. |
| 772 | 13 /usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols \ |
| 773 | 14 "http://translate.google.com/translate_tts?tl=en&q="$*"" |
| 774 | 15 # Google translates, but can also speak. |
| 775 | 16 } |
| 776 | 17 |
| 777 | 18 LINES=4 |
| 778 | 19 |
| 779 | 20 spk=$(tail -$LINES $0) # Tail end of same script! |
| 780 | 21 speak "$spk" |
| 781 | 22 exit |
| 782 | 23 # Browns. Nice talking to you.</PRE |
| 783 | ></TD |
| 784 | ></TR |
| 785 | ></TABLE |
| 786 | ><HR></DIV |
| 787 | ><P |
| 788 | >One interesting example of a complex shell wrapper is Martin |
| 789 | Matusiak's <A |
| 790 | HREF="http://sourceforge.net/projects/undvd/" |
| 791 | TARGET="_top" |
| 792 | ><I |
| 793 | CLASS="FIRSTTERM" |
| 794 | >undvd</I |
| 795 | > |
| 796 | script</A |
| 797 | >, which provides an easy-to-use |
| 798 | command-line interface to the complex <A |
| 799 | HREF="http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html" |
| 800 | TARGET="_top" |
| 801 | >mencoder</A |
| 802 | > |
| 803 | utility. Another example is Itzchak Rehberg's <A |
| 804 | HREF="http://projects.izzysoft.de/trac/ext3undel" |
| 805 | TARGET="_top" |
| 806 | >Ext3Undel</A |
| 807 | >, |
| 808 | a set of scripts to recover deleted file on an |
| 809 | <I |
| 810 | CLASS="FIRSTTERM" |
| 811 | >ext3</I |
| 812 | > filesystem.</P |
| 813 | ></DIV |
| 814 | ><H3 |
| 815 | CLASS="FOOTNOTES" |
| 816 | >Notes</H3 |
| 817 | ><TABLE |
| 818 | BORDER="0" |
| 819 | CLASS="FOOTNOTES" |
| 820 | WIDTH="100%" |
| 821 | ><TR |
| 822 | ><TD |
| 823 | ALIGN="LEFT" |
| 824 | VALIGN="TOP" |
| 825 | WIDTH="5%" |
| 826 | ><A |
| 827 | NAME="FTN.AEN20130" |
| 828 | HREF="wrapper.html#AEN20130" |
| 829 | >[1]</A |
| 830 | ></TD |
| 831 | ><TD |
| 832 | ALIGN="LEFT" |
| 833 | VALIGN="TOP" |
| 834 | WIDTH="95%" |
| 835 | ><P |
| 836 | >Quite a number of Linux utilities are, in fact, |
| 837 | shell wrappers. Some examples are |
| 838 | <TT |
| 839 | CLASS="FILENAME" |
| 840 | >/usr/bin/pdf2ps</TT |
| 841 | >, |
| 842 | <TT |
| 843 | CLASS="FILENAME" |
| 844 | >/usr/bin/batch</TT |
| 845 | >, and |
| 846 | <TT |
| 847 | CLASS="FILENAME" |
| 848 | >/usr/bin/xmkmf</TT |
| 849 | >.</P |
| 850 | ></TD |
| 851 | ></TR |
| 852 | ></TABLE |
| 853 | ><DIV |
| 854 | CLASS="NAVFOOTER" |
| 855 | ><HR |
| 856 | ALIGN="LEFT" |
| 857 | WIDTH="100%"><TABLE |
| 858 | SUMMARY="Footer navigation table" |
| 859 | WIDTH="100%" |
| 860 | BORDER="0" |
| 861 | CELLPADDING="0" |
| 862 | CELLSPACING="0" |
| 863 | ><TR |
| 864 | ><TD |
| 865 | WIDTH="33%" |
| 866 | ALIGN="left" |
| 867 | VALIGN="top" |
| 868 | ><A |
| 869 | HREF="miscellany.html" |
| 870 | ACCESSKEY="P" |
| 871 | >Prev</A |
| 872 | ></TD |
| 873 | ><TD |
| 874 | WIDTH="34%" |
| 875 | ALIGN="center" |
| 876 | VALIGN="top" |
| 877 | ><A |
| 878 | HREF="index.html" |
| 879 | ACCESSKEY="H" |
| 880 | >Home</A |
| 881 | ></TD |
| 882 | ><TD |
| 883 | WIDTH="33%" |
| 884 | ALIGN="right" |
| 885 | VALIGN="top" |
| 886 | ><A |
| 887 | HREF="testsandcomparisons.html" |
| 888 | ACCESSKEY="N" |
| 889 | >Next</A |
| 890 | ></TD |
| 891 | ></TR |
| 892 | ><TR |
| 893 | ><TD |
| 894 | WIDTH="33%" |
| 895 | ALIGN="left" |
| 896 | VALIGN="top" |
| 897 | >Miscellany</TD |
| 898 | ><TD |
| 899 | WIDTH="34%" |
| 900 | ALIGN="center" |
| 901 | VALIGN="top" |
| 902 | ><A |
| 903 | HREF="miscellany.html" |
| 904 | ACCESSKEY="U" |
| 905 | >Up</A |
| 906 | ></TD |
| 907 | ><TD |
| 908 | WIDTH="33%" |
| 909 | ALIGN="right" |
| 910 | VALIGN="top" |
| 911 | >Tests and Comparisons: Alternatives</TD |
| 912 | ></TR |
| 913 | ></TABLE |
| 914 | ></DIV |
| 915 | ></BODY |
| 916 | ></HTML |
| 917 | > |