Debian Codegraph @

HTML/wrapper.html

raw symbols SCIP
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&nbsp;#!/bin/bash
209 2&nbsp;
210 3&nbsp;# This simple script removes blank lines from a file.
211 4&nbsp;# No argument checking.
212 5&nbsp;#
213 6&nbsp;# You might wish to add something like:
214 7&nbsp;#
215 8&nbsp;# E_NOARGS=85
216 9&nbsp;# if [ -z "$1" ]
217 10&nbsp;# then
218 11&nbsp;# echo "Usage: `basename $0` target-file"
219 12&nbsp;# exit $E_NOARGS
220 13&nbsp;# fi
221 14&nbsp;
222 15&nbsp;
223 16&nbsp;
224 17&nbsp;sed -e /^$/d "$1"
225 18&nbsp;# Same as
226 19&nbsp;# sed -e '/^$/d' filename
227 20&nbsp;# invoked from the command-line.
228 21&nbsp;
229 22&nbsp;# The '-e' means an "editing" command follows (optional here).
230 23&nbsp;# '^' indicates the beginning of line, '$' the end.
231 24&nbsp;# This matches lines with nothing between the beginning and the end --
232 25&nbsp;#+ blank lines.
233 26&nbsp;# The 'd' is the delete command.
234 27&nbsp;
235 28&nbsp;# Quoting the command-line arg permits
236 29&nbsp;#+ whitespace and special characters in the filename.
237 30&nbsp;
238 31&nbsp;# Note that this script doesn't actually change the target file.
239 32&nbsp;# If you need to do that, redirect its output.
240 33&nbsp;
241 34&nbsp;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&nbsp;#!/bin/bash
268 2&nbsp;
269 3&nbsp;# subst.sh: a script that substitutes one pattern for
270 4&nbsp;#+ another in a file,
271 5&nbsp;#+ i.e., "sh subst.sh Smith Jones letter.txt".
272 6&nbsp;# Jones replaces Smith.
273 7&nbsp;
274 8&nbsp;ARGS=3 # Script requires 3 arguments.
275 9&nbsp;E_BADARGS=85 # Wrong number of arguments passed to script.
276 10&nbsp;
277 11&nbsp;if [ $# -ne "$ARGS" ]
278 12&nbsp;then
279 13&nbsp; echo "Usage: `basename $0` old-pattern new-pattern filename"
280 14&nbsp; exit $E_BADARGS
281 15&nbsp;fi
282 16&nbsp;
283 17&nbsp;old_pattern=$1
284 18&nbsp;new_pattern=$2
285 19&nbsp;
286 20&nbsp;if [ -f "$3" ]
287 21&nbsp;then
288 22&nbsp; file_name=$3
289 23&nbsp;else
290 24&nbsp; echo "File \"$3\" does not exist."
291 25&nbsp; exit $E_BADARGS
292 26&nbsp;fi
293 27&nbsp;
294 28&nbsp;
295 29&nbsp;# -----------------------------------------------
296 30&nbsp;# Here is where the heavy work gets done.
297 31&nbsp;sed -e "s/$old_pattern/$new_pattern/g" $file_name
298 32&nbsp;# -----------------------------------------------
299 33&nbsp;
300 34&nbsp;# 's' is, of course, the substitute command in sed,
301 35&nbsp;#+ and /pattern/ invokes address matching.
302 36&nbsp;# The 'g,' or global flag causes substitution for EVERY
303 37&nbsp;#+ occurence of $old_pattern on each line, not just the first.
304 38&nbsp;# Read the 'sed' docs for an in-depth explanation.
305 39&nbsp;
306 40&nbsp;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&nbsp;#!/bin/bash
333 2&nbsp;# logging-wrapper.sh
334 3&nbsp;# Generic shell wrapper that performs an operation
335 4&nbsp;#+ and logs it.
336 5&nbsp;
337 6&nbsp;DEFAULT_LOGFILE=logfile.txt
338 7&nbsp;
339 8&nbsp;# Set the following two variables.
340 9&nbsp;OPERATION=
341 10&nbsp;# Can be a complex chain of commands,
342 11&nbsp;#+ for example an awk script or a pipe . . .
343 12&nbsp;
344 13&nbsp;LOGFILE=
345 14&nbsp;if [ -z "$LOGFILE" ]
346 15&nbsp;then # If not set, default to ...
347 16&nbsp; LOGFILE="$DEFAULT_LOGFILE"
348 17&nbsp;fi
349 18&nbsp;
350 19&nbsp;# Command-line arguments, if any, for the operation.
351 20&nbsp;OPTIONS="$@"
352 21&nbsp;
353 22&nbsp;
354 23&nbsp;# Log it.
355 24&nbsp;echo "`date` + `whoami` + $OPERATION "$@"" &#62;&#62; $LOGFILE
356 25&nbsp;# Now, do it.
357 26&nbsp;exec $OPERATION "$@"
358 27&nbsp;
359 28&nbsp;# It's necessary to do the logging before the operation.
360 29&nbsp;# 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&nbsp;#!/bin/bash
387 2&nbsp;# pr-ascii.sh: Prints a table of ASCII characters.
388 3&nbsp;
389 4&nbsp;START=33 # Range of printable ASCII characters (decimal).
390 5&nbsp;END=127 # Will not work for unprintable characters (&#62; 127).
391 6&nbsp;
392 7&nbsp;echo " Decimal Hex Character" # Header.
393 8&nbsp;echo " ------- --- ---------"
394 9&nbsp;
395 10&nbsp;for ((i=START; i&#60;=END; i++))
396 11&nbsp;do
397 12&nbsp; echo $i | awk '{printf(" %3d %2x %c\n", $1, $1, $1)}'
398 13&nbsp;# The Bash printf builtin will not work in this context:
399 14&nbsp;# printf "%c" "$i"
400 15&nbsp;done
401 16&nbsp;
402 17&nbsp;exit 0
403 18&nbsp;
404 19&nbsp;
405 20&nbsp;# Decimal Hex Character
406 21&nbsp;# ------- --- ---------
407 22&nbsp;# 33 21 !
408 23&nbsp;# 34 22 "
409 24&nbsp;# 35 23 #
410 25&nbsp;# 36 24 $
411 26&nbsp;#
412 27&nbsp;# . . .
413 28&nbsp;#
414 29&nbsp;# 122 7a z
415 30&nbsp;# 123 7b {
416 31&nbsp;# 124 7c |
417 32&nbsp;# 125 7d }
418 33&nbsp;
419 34&nbsp;
420 35&nbsp;# Redirect the output of this script to a file
421 36&nbsp;#+ 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&nbsp;#!/bin/bash
448 2&nbsp;
449 3&nbsp;# Adds up a specified column (of numbers) in the target file.
450 4&nbsp;# Floating-point (decimal) numbers okay, because awk can handle them.
451 5&nbsp;
452 6&nbsp;ARGS=2
453 7&nbsp;E_WRONGARGS=85
454 8&nbsp;
455 9&nbsp;if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
456 10&nbsp;then
457 11&nbsp; echo "Usage: `basename $0` filename column-number"
458 12&nbsp; exit $E_WRONGARGS
459 13&nbsp;fi
460 14&nbsp;
461 15&nbsp;filename=$1
462 16&nbsp;column_number=$2
463 17&nbsp;
464 18&nbsp;# Passing shell variables to the awk part of the script is a bit tricky.
465 19&nbsp;# One method is to strong-quote the Bash-script variable
466 20&nbsp;#+ within the awk script.
467 21&nbsp;# $'$BASH_SCRIPT_VAR'
468 22&nbsp;# ^ ^
469 23&nbsp;# This is done in the embedded awk script below.
470 24&nbsp;# See the awk documentation for more details.
471 25&nbsp;
472 26&nbsp;# A multi-line awk script is here invoked by
473 27&nbsp;# awk '
474 28&nbsp;# ...
475 29&nbsp;# ...
476 30&nbsp;# ...
477 31&nbsp;# '
478 32&nbsp;
479 33&nbsp;
480 34&nbsp;# Begin awk script.
481 35&nbsp;# -----------------------------
482 36&nbsp;awk '
483 37&nbsp;
484 38&nbsp;{ total += $'"${column_number}"'
485 39&nbsp;}
486 40&nbsp;END {
487 41&nbsp; print total
488 42&nbsp;}
489 43&nbsp;
490 44&nbsp;' "$filename"
491 45&nbsp;# -----------------------------
492 46&nbsp;# End awk script.
493 47&nbsp;
494 48&nbsp;
495 49&nbsp;# It may not be safe to pass shell variables to an embedded awk script,
496 50&nbsp;#+ so Stephane Chazelas proposes the following alternative:
497 51&nbsp;# ---------------------------------------
498 52&nbsp;# awk -v column_number="$column_number" '
499 53&nbsp;# { total += $column_number
500 54&nbsp;# }
501 55&nbsp;# END {
502 56&nbsp;# print total
503 57&nbsp;# }' "$filename"
504 58&nbsp;# ---------------------------------------
505 59&nbsp;
506 60&nbsp;
507 61&nbsp;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&nbsp;#!/bin/bash
571 2&nbsp;
572 3&nbsp;# Shell commands may precede the Perl script.
573 4&nbsp;echo "This precedes the embedded Perl script within \"$0\"."
574 5&nbsp;echo "==============================================================="
575 6&nbsp;
576 7&nbsp;perl -e 'print "This line prints from an embedded Perl script.\n";'
577 8&nbsp;# Like sed, Perl also uses the "-e" option.
578 9&nbsp;
579 10&nbsp;echo "==============================================================="
580 11&nbsp;echo "However, the script may also contain shell and system commands."
581 12&nbsp;
582 13&nbsp;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&nbsp;#!/bin/bash
614 2&nbsp;# bashandperl.sh
615 3&nbsp;
616 4&nbsp;echo "Greetings from the Bash part of the script, $0."
617 5&nbsp;# More Bash commands may follow here.
618 6&nbsp;
619 7&nbsp;exit
620 8&nbsp;# End of Bash part of the script.
621 9&nbsp;
622 10&nbsp;# =======================================================
623 11&nbsp;
624 12&nbsp;#!/usr/bin/perl
625 13&nbsp;# This part of the script must be invoked with
626 14&nbsp;# perl -x bashandperl.sh
627 15&nbsp;
628 16&nbsp;print "Greetings from the Perl part of the script, $0.\n";
629 17&nbsp;# Perl doesn't seem to like "echo" ...
630 18&nbsp;# More Perl commands may follow here.
631 19&nbsp;
632 20&nbsp;# 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&nbsp;#!/bin/bash
713 2&nbsp;# ex56py.sh
714 3&nbsp;
715 4&nbsp;# Shell commands may precede the Python script.
716 5&nbsp;echo "This precedes the embedded Python script within \"$0.\""
717 6&nbsp;echo "==============================================================="
718 7&nbsp;
719 8&nbsp;python -c 'print "This line prints from an embedded Python script.\n";'
720 9&nbsp;# Unlike sed and perl, Python uses the "-c" option.
721 10&nbsp;python -c 'k = raw_input( "Hit a key to exit to outer script. " )'
722 11&nbsp;
723 12&nbsp;echo "==============================================================="
724 13&nbsp;echo "However, the script may also contain shell and system commands."
725 14&nbsp;
726 15&nbsp;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&nbsp;#!/bin/bash
761 2&nbsp;# Courtesy of:
762 3&nbsp;# http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis)
763 4&nbsp;
764 5&nbsp;# You must be on-line for this script to work,
765 6&nbsp;#+ so you can access the Google translation server.
766 7&nbsp;# Of course, mplayer must be present on your computer.
767 8&nbsp;
768 9&nbsp;speak()
769 10&nbsp; {
770 11&nbsp; local IFS=+
771 12&nbsp; # Invoke mplayer, then connect to Google translation server.
772 13&nbsp; /usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols \
773 14&nbsp; "http://translate.google.com/translate_tts?tl=en&#38;q="$*""
774 15&nbsp; # Google translates, but can also speak.
775 16&nbsp; }
776 17&nbsp;
777 18&nbsp;LINES=4
778 19&nbsp;
779 20&nbsp;spk=$(tail -$LINES $0) # Tail end of same script!
780 21&nbsp;speak "$spk"
781 22&nbsp;exit
782 23&nbsp;# 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 >