File: wrapper.html

package info (click to toggle)
abs-guide 10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,960 kB
  • sloc: sh: 14,129; makefile: 81
file content (917 lines) | stat: -rw-r--r-- 18,765 bytes parent folder | download | duplicates (3)
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Shell Wrappers</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="Miscellany"
HREF="miscellany.html"><LINK
REL="NEXT"
TITLE="Tests and Comparisons: Alternatives"
HREF="testsandcomparisons.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="miscellany.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="testsandcomparisons.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="WRAPPER"
></A
>36.2. Shell Wrappers</H1
><P
><A
NAME="SHWRAPPER"
></A
></P
><P
>A <I
CLASS="FIRSTTERM"
>wrapper</I
> is a shell script that embeds
	a system command or utility, that accepts and passes a set of
	parameters to that command.

      <A
NAME="AEN20130"
HREF="#FTN.AEN20130"
>[1]</A
>
	
	Wrapping a script around a complex command-line
	simplifies invoking it.  This is expecially useful
	with <A
HREF="sedawk.html#SEDREF"
>sed</A
> and <A
HREF="awk.html#AWKREF"
>awk</A
>.</P
><P
>A
	<B
CLASS="COMMAND"
>	    sed</B
> or
	<B
CLASS="COMMAND"
>	   
	    awk</B
> script would normally be invoked
	    from the command-line by a <TT
CLASS="USERINPUT"
><B
>sed -e
	    <TT
CLASS="REPLACEABLE"
><I
>'commands'</I
></TT
></B
></TT
>
	    or <TT
CLASS="USERINPUT"
><B
>awk
	    <TT
CLASS="REPLACEABLE"
><I
>'commands'</I
></TT
></B
></TT
>.	Embedding
	    such a script in a Bash script permits calling it more simply,
	    and makes it <I
CLASS="FIRSTTERM"
>reusable</I
>. This also
	    enables combining the functionality of <I
CLASS="FIRSTTERM"
>sed</I
>
	    and <I
CLASS="FIRSTTERM"
>awk</I
>, for example <A
HREF="special-chars.html#PIPEREF"
>piping</A
> the output of a set of
	    <I
CLASS="FIRSTTERM"
>sed</I
> commands to
	    <I
CLASS="FIRSTTERM"
>awk</I
>. As a saved executable file,
	    you can then repeatedly invoke it in its original form or
	    modified, without the inconvenience of retyping it on the
	    command-line.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX3"
></A
><P
><B
>Example 36-1. <I
CLASS="FIRSTTERM"
>shell wrapper</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# This simple script removes blank lines from a file.
   4&nbsp;# No argument checking.
   5&nbsp;#
   6&nbsp;# You might wish to add something like:
   7&nbsp;#
   8&nbsp;# E_NOARGS=85
   9&nbsp;# if [ -z "$1" ]
  10&nbsp;# then
  11&nbsp;#  echo "Usage: `basename $0` target-file"
  12&nbsp;#  exit $E_NOARGS
  13&nbsp;# fi
  14&nbsp;
  15&nbsp;
  16&nbsp;
  17&nbsp;sed -e /^$/d "$1"
  18&nbsp;# Same as
  19&nbsp;#    sed -e '/^$/d' filename
  20&nbsp;# invoked from the command-line.
  21&nbsp;
  22&nbsp;#  The '-e' means an "editing" command follows (optional here).
  23&nbsp;#  '^' indicates the beginning of line, '$' the end.
  24&nbsp;#  This matches lines with nothing between the beginning and the end --
  25&nbsp;#+ blank lines.
  26&nbsp;#  The 'd' is the delete command.
  27&nbsp;
  28&nbsp;#  Quoting the command-line arg permits
  29&nbsp;#+ whitespace and special characters in the filename.
  30&nbsp;
  31&nbsp;#  Note that this script doesn't actually change the target file.
  32&nbsp;#  If you need to do that, redirect its output.
  33&nbsp;
  34&nbsp;exit</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX4"
></A
><P
><B
>Example 36-2.  A slightly more complex <I
CLASS="FIRSTTERM"
>shell
	wrapper</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;#  subst.sh: a script that substitutes one pattern for
   4&nbsp;#+ another in a file,
   5&nbsp;#+ i.e., "sh subst.sh Smith Jones letter.txt".
   6&nbsp;#                     Jones replaces Smith.
   7&nbsp;
   8&nbsp;ARGS=3         # Script requires 3 arguments.
   9&nbsp;E_BADARGS=85   # Wrong number of arguments passed to script.
  10&nbsp;
  11&nbsp;if [ $# -ne "$ARGS" ]
  12&nbsp;then
  13&nbsp;  echo "Usage: `basename $0` old-pattern new-pattern filename"
  14&nbsp;  exit $E_BADARGS
  15&nbsp;fi
  16&nbsp;
  17&nbsp;old_pattern=$1
  18&nbsp;new_pattern=$2
  19&nbsp;
  20&nbsp;if [ -f "$3" ]
  21&nbsp;then
  22&nbsp;    file_name=$3
  23&nbsp;else
  24&nbsp;    echo "File \"$3\" does not exist."
  25&nbsp;    exit $E_BADARGS
  26&nbsp;fi
  27&nbsp;
  28&nbsp;
  29&nbsp;# -----------------------------------------------
  30&nbsp;#  Here is where the heavy work gets done.
  31&nbsp;sed -e "s/$old_pattern/$new_pattern/g" $file_name
  32&nbsp;# -----------------------------------------------
  33&nbsp;
  34&nbsp;#  's' is, of course, the substitute command in sed,
  35&nbsp;#+ and /pattern/ invokes address matching.
  36&nbsp;#  The 'g,' or global flag causes substitution for EVERY
  37&nbsp;#+ occurence of $old_pattern on each line, not just the first.
  38&nbsp;#  Read the 'sed' docs for an in-depth explanation.
  39&nbsp;
  40&nbsp;exit $?  # Redirect the output of this script to write to a file.</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="LOGGINGWRAPPER"
></A
><P
><B
>Example 36-3.  A generic <I
CLASS="FIRSTTERM"
>shell wrapper</I
> that
	writes to a logfile</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;#  logging-wrapper.sh
   3&nbsp;#  Generic shell wrapper that performs an operation
   4&nbsp;#+ and logs it.
   5&nbsp;
   6&nbsp;DEFAULT_LOGFILE=logfile.txt
   7&nbsp;
   8&nbsp;# Set the following two variables.
   9&nbsp;OPERATION=
  10&nbsp;#         Can be a complex chain of commands,
  11&nbsp;#+        for example an awk script or a pipe . . .
  12&nbsp;
  13&nbsp;LOGFILE=
  14&nbsp;if [ -z "$LOGFILE" ]
  15&nbsp;then     # If not set, default to ...
  16&nbsp;  LOGFILE="$DEFAULT_LOGFILE"
  17&nbsp;fi
  18&nbsp;
  19&nbsp;#         Command-line arguments, if any, for the operation.
  20&nbsp;OPTIONS="$@"
  21&nbsp;
  22&nbsp;
  23&nbsp;# Log it.
  24&nbsp;echo "`date` + `whoami` + $OPERATION "$@"" &#62;&#62; $LOGFILE
  25&nbsp;# Now, do it.
  26&nbsp;exec $OPERATION "$@"
  27&nbsp;
  28&nbsp;# It's necessary to do the logging before the operation.
  29&nbsp;# Why?</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="PRASC"
></A
><P
><B
>Example 36-4.  A <I
CLASS="FIRSTTERM"
>shell wrapper</I
> around an awk
	script</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# pr-ascii.sh: Prints a table of ASCII characters.
   3&nbsp;
   4&nbsp;START=33   # Range of printable ASCII characters (decimal).
   5&nbsp;END=127    # Will not work for unprintable characters (&#62; 127).
   6&nbsp;
   7&nbsp;echo " Decimal   Hex     Character"   # Header.
   8&nbsp;echo " -------   ---     ---------"
   9&nbsp;
  10&nbsp;for ((i=START; i&#60;=END; i++))
  11&nbsp;do
  12&nbsp;  echo $i | awk '{printf("  %3d       %2x         %c\n", $1, $1, $1)}'
  13&nbsp;# The Bash printf builtin will not work in this context:
  14&nbsp;#     printf "%c" "$i"
  15&nbsp;done
  16&nbsp;
  17&nbsp;exit 0
  18&nbsp;
  19&nbsp;
  20&nbsp;#  Decimal   Hex     Character
  21&nbsp;#  -------   ---     ---------
  22&nbsp;#    33       21         !
  23&nbsp;#    34       22         "
  24&nbsp;#    35       23         #
  25&nbsp;#    36       24         $
  26&nbsp;#
  27&nbsp;#    . . .
  28&nbsp;#
  29&nbsp;#   122       7a         z
  30&nbsp;#   123       7b         {
  31&nbsp;#   124       7c         |
  32&nbsp;#   125       7d         }
  33&nbsp;
  34&nbsp;
  35&nbsp;#  Redirect the output of this script to a file
  36&nbsp;#+ or pipe it to "more":  sh pr-asc.sh | more</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="COLTOTALER"
></A
><P
><B
>Example 36-5.  A <I
CLASS="FIRSTTERM"
>shell wrapper</I
> around another
	awk script</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Adds up a specified column (of numbers) in the target file.
   4&nbsp;# Floating-point (decimal) numbers okay, because awk can handle them.
   5&nbsp;
   6&nbsp;ARGS=2
   7&nbsp;E_WRONGARGS=85
   8&nbsp;
   9&nbsp;if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
  10&nbsp;then
  11&nbsp;   echo "Usage: `basename $0` filename column-number"
  12&nbsp;   exit $E_WRONGARGS
  13&nbsp;fi
  14&nbsp;
  15&nbsp;filename=$1
  16&nbsp;column_number=$2
  17&nbsp;
  18&nbsp;#  Passing shell variables to the awk part of the script is a bit tricky.
  19&nbsp;#  One method is to strong-quote the Bash-script variable
  20&nbsp;#+ within the awk script.
  21&nbsp;#     $'$BASH_SCRIPT_VAR'
  22&nbsp;#      ^                ^
  23&nbsp;#  This is done in the embedded awk script below.
  24&nbsp;#  See the awk documentation for more details.
  25&nbsp;
  26&nbsp;# A multi-line awk script is here invoked by
  27&nbsp;#   awk '
  28&nbsp;#   ...
  29&nbsp;#   ...
  30&nbsp;#   ...
  31&nbsp;#   '
  32&nbsp;
  33&nbsp;
  34&nbsp;# Begin awk script.
  35&nbsp;# -----------------------------
  36&nbsp;awk '
  37&nbsp;
  38&nbsp;{ total += $'"${column_number}"'
  39&nbsp;}
  40&nbsp;END {
  41&nbsp;     print total
  42&nbsp;}     
  43&nbsp;
  44&nbsp;' "$filename"
  45&nbsp;# -----------------------------
  46&nbsp;# End awk script.
  47&nbsp;
  48&nbsp;
  49&nbsp;#   It may not be safe to pass shell variables to an embedded awk script,
  50&nbsp;#+  so Stephane Chazelas proposes the following alternative:
  51&nbsp;#   ---------------------------------------
  52&nbsp;#   awk -v column_number="$column_number" '
  53&nbsp;#   { total += $column_number
  54&nbsp;#   }
  55&nbsp;#   END {
  56&nbsp;#       print total
  57&nbsp;#   }' "$filename"
  58&nbsp;#   ---------------------------------------
  59&nbsp;
  60&nbsp;
  61&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="PERLREF"
></A
>For those scripts needing a single
	do-it-all tool, a Swiss army knife, there is
	<I
CLASS="FIRSTTERM"
>Perl</I
>. Perl combines the
	capabilities of <A
HREF="sedawk.html#SEDREF"
>sed</A
> and <A
HREF="awk.html#AWKREF"
>awk</A
>, and throws in a large subset of
	<B
CLASS="COMMAND"
>C</B
>, to boot. It is modular and contains support
	for everything ranging from object-oriented programming up to and
	including the kitchen sink. Short Perl scripts lend themselves to
	embedding within shell scripts, and there may be some substance
	to the claim that Perl can totally replace shell scripting
	(though the author of the <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>ABS Guide</I
></SPAN
> remains
	skeptical).</P
><P
><A
NAME="PERLEMB"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX56"
></A
><P
><B
>Example 36-6. Perl embedded in a <I
CLASS="FIRSTTERM"
>Bash</I
> script</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Shell commands may precede the Perl script.
   4&nbsp;echo "This precedes the embedded Perl script within \"$0\"."
   5&nbsp;echo "==============================================================="
   6&nbsp;
   7&nbsp;perl -e 'print "This line prints from an embedded Perl script.\n";'
   8&nbsp;# Like sed, Perl also uses the "-e" option.
   9&nbsp;
  10&nbsp;echo "==============================================================="
  11&nbsp;echo "However, the script may also contain shell and system commands."
  12&nbsp;
  13&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>It is even possible to combine a Bash script and Perl script
        within the same file. Depending on how the script is invoked, either
	the Bash part or the Perl part will execute.</P
><P
><A
NAME="BASHANDPERL0"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BASHANDPERL"
></A
><P
><B
>Example 36-7. Bash and Perl scripts combined</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# bashandperl.sh
   3&nbsp;
   4&nbsp;echo "Greetings from the Bash part of the script, $0."
   5&nbsp;# More Bash commands may follow here.
   6&nbsp;
   7&nbsp;exit
   8&nbsp;# End of Bash part of the script.
   9&nbsp;
  10&nbsp;# =======================================================
  11&nbsp;
  12&nbsp;#!/usr/bin/perl
  13&nbsp;# This part of the script must be invoked with
  14&nbsp;#    perl -x bashandperl.sh
  15&nbsp;
  16&nbsp;print "Greetings from the Perl part of the script, $0.\n";
  17&nbsp;#      Perl doesn't seem to like "echo" ...
  18&nbsp;# More Perl commands may follow here.
  19&nbsp;
  20&nbsp;# End of Perl part of the script.</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>bash bashandperl.sh</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>Greetings from the Bash part of the script.</TT
>
 
 
 <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>perl -x bashandperl.sh</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>Greetings from the Perl part of the script.</TT
>
 	      </PRE
></TD
></TR
></TABLE
>
	      </P
><P
>It is, of course, possible to embed even more exotic scripting
        languages within shell wrappers.  <I
CLASS="FIRSTTERM"
>Python</I
>,
        for example ...</P
><P
><A
NAME="PYTHONEMB"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX56PY"
></A
><P
><B
>Example 36-8. Python embedded in a <I
CLASS="FIRSTTERM"
>Bash</I
> script</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# ex56py.sh
   3&nbsp;
   4&nbsp;# Shell commands may precede the Python script.
   5&nbsp;echo "This precedes the embedded Python script within \"$0.\""
   6&nbsp;echo "==============================================================="
   7&nbsp;
   8&nbsp;python -c 'print "This line prints from an embedded Python script.\n";'
   9&nbsp;# Unlike sed and perl, Python uses the "-c" option.
  10&nbsp;python -c 'k = raw_input( "Hit a key to exit to outer script. " )'
  11&nbsp;
  12&nbsp;echo "==============================================================="
  13&nbsp;echo "However, the script may also contain shell and system commands."
  14&nbsp;
  15&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Wrapping a script around <I
CLASS="FIRSTTERM"
>mplayer</I
>
        and the Google's translation server, you can create something
        that talks back to you.</P
><P
><A
NAME="SPEECH00"
></A
></P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="SPEECH0"
></A
><P
><B
>Example 36-9. A script that speaks</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;#   Courtesy of:
   3&nbsp;#   http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis)
   4&nbsp;
   5&nbsp;#  You must be on-line for this script to work,
   6&nbsp;#+ so you can access the Google translation server.
   7&nbsp;#  Of course, mplayer must be present on your computer.
   8&nbsp;
   9&nbsp;speak()
  10&nbsp;  {
  11&nbsp;  local IFS=+
  12&nbsp;  # Invoke mplayer, then connect to Google translation server.
  13&nbsp;  /usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols \
  14&nbsp; "http://translate.google.com/translate_tts?tl=en&#38;q="$*""
  15&nbsp;  # Google translates, but can also speak.
  16&nbsp;  }
  17&nbsp;
  18&nbsp;LINES=4
  19&nbsp;
  20&nbsp;spk=$(tail -$LINES $0) # Tail end of same script!
  21&nbsp;speak "$spk"
  22&nbsp;exit
  23&nbsp;# Browns. Nice talking to you.</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>One interesting example of a complex shell wrapper is Martin
         Matusiak's <A
HREF="http://sourceforge.net/projects/undvd/"
TARGET="_top"
><I
CLASS="FIRSTTERM"
>undvd</I
>
         script</A
>, which provides an easy-to-use
         command-line interface to the complex <A
HREF="http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html"
TARGET="_top"
>mencoder</A
>
         utility. Another example is Itzchak Rehberg's <A
HREF="http://projects.izzysoft.de/trac/ext3undel"
TARGET="_top"
>Ext3Undel</A
>,
         a set of scripts to recover deleted file on an
         <I
CLASS="FIRSTTERM"
>ext3</I
> filesystem.</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN20130"
HREF="wrapper.html#AEN20130"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Quite a number of Linux utilities are, in fact,
	shell wrappers. Some examples are
	<TT
CLASS="FILENAME"
>/usr/bin/pdf2ps</TT
>,
	<TT
CLASS="FILENAME"
>/usr/bin/batch</TT
>, and
	<TT
CLASS="FILENAME"
>/usr/bin/xmkmf</TT
>.</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="miscellany.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="testsandcomparisons.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Miscellany</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="miscellany.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Tests and Comparisons: Alternatives</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>