Debian Codegraph @

HTML/manipulatingvars.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Manipulating Variables</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="Beyond the Basics"
15 HREF="part3.html"><LINK
16 REL="PREVIOUS"
17 TITLE="$RANDOM: generate random integer"
18 HREF="randomvar.html"><LINK
19 REL="NEXT"
20 TITLE="Parameter Substitution"
21 HREF="parameter-substitution.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="CHAPTER"
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="randomvar.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="parameter-substitution.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="CHAPTER"
92 ><H1
93 ><A
94 NAME="MANIPULATINGVARS"
95 ></A
96 >Chapter 10. Manipulating Variables</H1
97 ><DIV
98 CLASS="SECT1"
99 ><H1
100 CLASS="SECT1"
101 ><A
102 NAME="STRING-MANIPULATION"
103 ></A
104 >10.1. Manipulating Strings</H1
105 ><P
106 ><A
107 NAME="STRINGMANIP"
108 ></A
109 ></P
110 ><P
111 >Bash supports a surprising number of string manipulation
112 operations. Unfortunately, these tools lack
113 a unified focus. Some are a subset of <A
114 HREF="parameter-substitution.html#PARAMSUBREF"
115 >parameter substitution</A
116 >, and
117 others fall under the functionality of the UNIX <A
118 HREF="moreadv.html#EXPRREF"
119 >expr</A
120 > command. This results in
121 inconsistent command syntax and overlap of functionality,
122 not to mention confusion.</P
123 ><DIV
124 CLASS="VARIABLELIST"
125 ><P
126 ><B
127 >String Length</B
128 ></P
129 ><DL
130 ><DT
131 >${#string}</DT
132 ><DD
133 ><P
134 ></P
135 ></DD
136 ><DT
137 >expr length $string</DT
138 ><DD
139 ><P
140 ><A
141 NAME="STRLEN"
142 ></A
143 >These are the equivalent of
144 <I
145 CLASS="FIRSTTERM"
146 >strlen()</I
147 > in
148 <I
149 CLASS="FIRSTTERM"
150 >C</I
151 >.</P
152 ></DD
153 ><DT
154 >expr "$string" : '.*'</DT
155 ><DD
156 ><P
157 > <TABLE
158 BORDER="0"
159 BGCOLOR="#E0E0E0"
160 WIDTH="90%"
161 ><TR
162 ><TD
163 ><PRE
164 CLASS="PROGRAMLISTING"
165 > 1&nbsp;stringZ=abcABC123ABCabc
166 2&nbsp;
167 3&nbsp;echo ${#stringZ} # 15
168 4&nbsp;echo `expr length $stringZ` # 15
169 5&nbsp;echo `expr "$stringZ" : '.*'` # 15</PRE
170 ></TD
171 ></TR
172 ></TABLE
173 >
174 </P
175 ></DD
176 ></DL
177 ></DIV
178 ><DIV
179 CLASS="EXAMPLE"
180 ><HR><A
181 NAME="PARAGRAPHSPACE"
182 ></A
183 ><P
184 ><B
185 >Example 10-1. Inserting a blank line between paragraphs in a text file</B
186 ></P
187 ><TABLE
188 BORDER="0"
189 BGCOLOR="#E0E0E0"
190 WIDTH="100%"
191 ><TR
192 ><TD
193 ><PRE
194 CLASS="PROGRAMLISTING"
195 > 1&nbsp;#!/bin/bash
196 2&nbsp;# paragraph-space.sh
197 3&nbsp;# Ver. 2.1, Reldate 29Jul12 [fixup]
198 4&nbsp;
199 5&nbsp;# Inserts a blank line between paragraphs of a single-spaced text file.
200 6&nbsp;# Usage: $0 &#60;FILENAME
201 7&nbsp;
202 8&nbsp;MINLEN=60 # Change this value? It's a judgment call.
203 9&nbsp;# Assume lines shorter than $MINLEN characters ending in a period
204 10&nbsp;#+ terminate a paragraph. See exercises below.
205 11&nbsp;
206 12&nbsp;while read line # For as many lines as the input file has ...
207 13&nbsp;do
208 14&nbsp; echo "$line" # Output the line itself.
209 15&nbsp;
210 16&nbsp; len=${#line}
211 17&nbsp; if [[ "$len" -lt "$MINLEN" &#38;&#38; "$line" =~ [*{\.}]$ ]]
212 18&nbsp;# if [[ "$len" -lt "$MINLEN" &#38;&#38; "$line" =~ \[*\.\] ]]
213 19&nbsp;# An update to Bash broke the previous version of this script. Ouch!
214 20&nbsp;# Thank you, Halim Srama, for pointing this out and suggesting a fix.
215 21&nbsp; then echo # Add a blank line immediately
216 22&nbsp; fi #+ after a short line terminated by a period.
217 23&nbsp;done
218 24&nbsp;
219 25&nbsp;exit
220 26&nbsp;
221 27&nbsp;# Exercises:
222 28&nbsp;# ---------
223 29&nbsp;# 1) The script usually inserts a blank line at the end
224 30&nbsp;#+ of the target file. Fix this.
225 31&nbsp;# 2) Line 17 only considers periods as sentence terminators.
226 32&nbsp;# Modify this to include other common end-of-sentence characters,
227 33&nbsp;#+ such as ?, !, and ".</PRE
228 ></TD
229 ></TR
230 ></TABLE
231 ><HR></DIV
232 ><DIV
233 CLASS="VARIABLELIST"
234 ><P
235 ><B
236 >Length of Matching Substring at Beginning of String</B
237 ></P
238 ><DL
239 ><DT
240 ><A
241 NAME="EXPRMATCH"
242 ></A
243 >expr match "$string"
244 '$substring'</DT
245 ><DD
246 ><P
247 ><TT
248 CLASS="REPLACEABLE"
249 ><I
250 >$substring</I
251 ></TT
252 > is a <A
253 HREF="regexp.html#REGEXREF"
254 >regular expression</A
255 >.</P
256 ></DD
257 ><DT
258 >expr "$string" : '$substring'</DT
259 ><DD
260 ><P
261 ><TT
262 CLASS="REPLACEABLE"
263 ><I
264 >$substring</I
265 ></TT
266 > is a regular
267 expression.</P
268 ><P
269 >&#13; <TABLE
270 BORDER="0"
271 BGCOLOR="#E0E0E0"
272 WIDTH="90%"
273 ><TR
274 ><TD
275 ><PRE
276 CLASS="PROGRAMLISTING"
277 > 1&nbsp;stringZ=abcABC123ABCabc
278 2&nbsp;# |------|
279 3&nbsp;# 12345678
280 4&nbsp;
281 5&nbsp;echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
282 6&nbsp;echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8</PRE
283 ></TD
284 ></TR
285 ></TABLE
286 >
287 </P
288 ></DD
289 ></DL
290 ></DIV
291 ><DIV
292 CLASS="VARIABLELIST"
293 ><P
294 ><B
295 >Index</B
296 ></P
297 ><DL
298 ><DT
299 ><A
300 NAME="SUBSTRINGINDEX2"
301 ></A
302 >expr index $string
303 $substring</DT
304 ><DD
305 ><P
306 >Numerical position in $string of first character in
307 $substring that matches.</P
308 ><P
309 ><TABLE
310 BORDER="0"
311 BGCOLOR="#E0E0E0"
312 WIDTH="90%"
313 ><TR
314 ><TD
315 ><PRE
316 CLASS="PROGRAMLISTING"
317 > 1&nbsp;stringZ=abcABC123ABCabc
318 2&nbsp;# 123456 ...
319 3&nbsp;echo `expr index "$stringZ" C12` # 6
320 4&nbsp; # C position.
321 5&nbsp;
322 6&nbsp;echo `expr index "$stringZ" 1c` # 3
323 7&nbsp;# 'c' (in #3 position) matches before '1'.</PRE
324 ></TD
325 ></TR
326 ></TABLE
327 ></P
328 ><P
329 >This is the near equivalent of
330 <I
331 CLASS="FIRSTTERM"
332 >strchr()</I
333 > in
334 <I
335 CLASS="FIRSTTERM"
336 >C</I
337 >.</P
338 ></DD
339 ></DL
340 ></DIV
341 ><DIV
342 CLASS="VARIABLELIST"
343 ><P
344 ><B
345 >Substring Extraction</B
346 ></P
347 ><DL
348 ><DT
349 ><A
350 NAME="SUBSTREXTR01"
351 ></A
352 >${string:position}</DT
353 ><DD
354 ><P
355 >Extracts substring from <TT
356 CLASS="REPLACEABLE"
357 ><I
358 >$string</I
359 ></TT
360 > at
361 <TT
362 CLASS="REPLACEABLE"
363 ><I
364 >$position</I
365 ></TT
366 >.</P
367 ><P
368 >If the <TT
369 CLASS="VARNAME"
370 >$string</TT
371 > parameter is
372 <SPAN
373 CLASS="QUOTE"
374 >"<SPAN
375 CLASS="TOKEN"
376 >*</SPAN
377 >"</SPAN
378 >
379 or <SPAN
380 CLASS="QUOTE"
381 >"<SPAN
382 CLASS="TOKEN"
383 >@</SPAN
384 >"</SPAN
385 >, then this extracts the
386 <A
387 HREF="variables2.html#POSPARAMREF"
388 >positional parameters</A
389 >,
390 <A
391 NAME="AEN5987"
392 HREF="#FTN.AEN5987"
393 >[1]</A
394 >
395 starting at <TT
396 CLASS="VARNAME"
397 >$position</TT
398 >.</P
399 ></DD
400 ><DT
401 ><A
402 NAME="SUBSTREXTR02"
403 ></A
404 >${string:position:length}</DT
405 ><DD
406 ><P
407 >Extracts <TT
408 CLASS="REPLACEABLE"
409 ><I
410 >$length</I
411 ></TT
412 > characters
413 of substring from <TT
414 CLASS="REPLACEABLE"
415 ><I
416 >$string</I
417 ></TT
418 > at
419 <TT
420 CLASS="REPLACEABLE"
421 ><I
422 >$position</I
423 ></TT
424 >.</P
425 ><P
426 > <TABLE
427 BORDER="0"
428 BGCOLOR="#E0E0E0"
429 WIDTH="90%"
430 ><TR
431 ><TD
432 ><PRE
433 CLASS="PROGRAMLISTING"
434 > 1&nbsp;stringZ=abcABC123ABCabc
435 2&nbsp;# 0123456789.....
436 3&nbsp;# 0-based indexing.
437 4&nbsp;
438 5&nbsp;echo ${stringZ:0} # abcABC123ABCabc
439 6&nbsp;echo ${stringZ:1} # bcABC123ABCabc
440 7&nbsp;echo ${stringZ:7} # 23ABCabc
441 8&nbsp;
442 9&nbsp;echo ${stringZ:7:3} # 23A
443 10&nbsp; # Three characters of substring.
444 11&nbsp;
445 12&nbsp;
446 13&nbsp;
447 14&nbsp;# Is it possible to index from the right end of the string?
448 15&nbsp;
449 16&nbsp;echo ${stringZ:-4} # abcABC123ABCabc
450 17&nbsp;# Defaults to full string, as in ${parameter:-default}.
451 18&nbsp;# However . . .
452 19&nbsp;
453 20&nbsp;echo ${stringZ:(-4)} # Cabc
454 21&nbsp;echo ${stringZ: -4} # Cabc
455 22&nbsp;# Now, it works.
456 23&nbsp;# Parentheses or added space "escape" the position parameter.
457 24&nbsp;
458 25&nbsp;# Thank you, Dan Jacobson, for pointing this out.</PRE
459 ></TD
460 ></TR
461 ></TABLE
462 >
463 </P
464 ><P
465 >The <I
466 CLASS="FIRSTTERM"
467 >position</I
468 > and
469 <I
470 CLASS="FIRSTTERM"
471 >length</I
472 > arguments can be
473 <SPAN
474 CLASS="QUOTE"
475 >"parameterized,"</SPAN
476 > that is, represented as a
477 variable, rather than as a numerical constant.</P
478 ><P
479 ><A
480 NAME="RANDSTRING0"
481 ></A
482 ></P
483 ><DIV
484 CLASS="EXAMPLE"
485 ><HR><A
486 NAME="RANDSTRING"
487 ></A
488 ><P
489 ><B
490 >Example 10-2. Generating an 8-character <SPAN
491 CLASS="QUOTE"
492 >"random"</SPAN
493 >
494 string</B
495 ></P
496 ><TABLE
497 BORDER="0"
498 BGCOLOR="#E0E0E0"
499 WIDTH="90%"
500 ><TR
501 ><TD
502 ><PRE
503 CLASS="PROGRAMLISTING"
504 > 1&nbsp;#!/bin/bash
505 2&nbsp;# rand-string.sh
506 3&nbsp;# Generating an 8-character "random" string.
507 4&nbsp;
508 5&nbsp;if [ -n "$1" ] # If command-line argument present,
509 6&nbsp;then #+ then set start-string to it.
510 7&nbsp; str0="$1"
511 8&nbsp;else # Else use PID of script as start-string.
512 9&nbsp; str0="$$"
513 10&nbsp;fi
514 11&nbsp;
515 12&nbsp;POS=2 # Starting from position 2 in the string.
516 13&nbsp;LEN=8 # Extract eight characters.
517 14&nbsp;
518 15&nbsp;str1=$( echo "$str0" | md5sum | md5sum )
519 16&nbsp;# Doubly scramble ^^^^^^ ^^^^^^
520 17&nbsp;#+ by piping and repiping to md5sum.
521 18&nbsp;
522 19&nbsp;randstring="${str1:$POS:$LEN}"
523 20&nbsp;# Can parameterize ^^^^ ^^^^
524 21&nbsp;
525 22&nbsp;echo "$randstring"
526 23&nbsp;
527 24&nbsp;exit $?
528 25&nbsp;
529 26&nbsp;# bozo$ ./rand-string.sh my-password
530 27&nbsp;# 1bdd88c4
531 28&nbsp;
532 29&nbsp;# No, this is is not recommended
533 30&nbsp;#+ as a method of generating hack-proof passwords.</PRE
534 ></TD
535 ></TR
536 ></TABLE
537 ><HR></DIV
538 ><P
539 ><A
540 NAME="SUBSTREXTRP"
541 ></A
542 ></P
543 ><P
544 >If the <TT
545 CLASS="VARNAME"
546 >$string</TT
547 > parameter is
548 <SPAN
549 CLASS="QUOTE"
550 >"<SPAN
551 CLASS="TOKEN"
552 >*</SPAN
553 >"</SPAN
554 > or
555 <SPAN
556 CLASS="QUOTE"
557 >"<SPAN
558 CLASS="TOKEN"
559 >@</SPAN
560 >"</SPAN
561 >, then this extracts a maximum
562 of <TT
563 CLASS="VARNAME"
564 >$length</TT
565 > positional parameters, starting
566 at <TT
567 CLASS="VARNAME"
568 >$position</TT
569 >.</P
570 ><P
571 > <TABLE
572 BORDER="0"
573 BGCOLOR="#E0E0E0"
574 WIDTH="90%"
575 ><TR
576 ><TD
577 ><PRE
578 CLASS="PROGRAMLISTING"
579 > 1&nbsp;echo ${*:2} # Echoes second and following positional parameters.
580 2&nbsp;echo ${@:2} # Same as above.
581 3&nbsp;
582 4&nbsp;echo ${*:2:3} # Echoes three positional parameters, starting at second.</PRE
583 ></TD
584 ></TR
585 ></TABLE
586 >
587 </P
588 ></DD
589 ><DT
590 >expr substr $string $position $length</DT
591 ><DD
592 ><P
593 >Extracts <TT
594 CLASS="REPLACEABLE"
595 ><I
596 >$length</I
597 ></TT
598 > characters
599 from <TT
600 CLASS="REPLACEABLE"
601 ><I
602 >$string</I
603 ></TT
604 > starting at
605 <TT
606 CLASS="REPLACEABLE"
607 ><I
608 >$position</I
609 ></TT
610 >.</P
611 ><P
612 > <TABLE
613 BORDER="0"
614 BGCOLOR="#E0E0E0"
615 WIDTH="90%"
616 ><TR
617 ><TD
618 ><PRE
619 CLASS="PROGRAMLISTING"
620 > 1&nbsp;stringZ=abcABC123ABCabc
621 2&nbsp;# 123456789......
622 3&nbsp;# 1-based indexing.
623 4&nbsp;
624 5&nbsp;echo `expr substr $stringZ 1 2` # ab
625 6&nbsp;echo `expr substr $stringZ 4 3` # ABC</PRE
626 ></TD
627 ></TR
628 ></TABLE
629 >
630 </P
631 ><P
632 ><A
633 NAME="EXPRPAREN"
634 ></A
635 ></P
636 ></DD
637 ><DT
638 >expr match "$string" '\($substring\)'</DT
639 ><DD
640 ><P
641 >Extracts <TT
642 CLASS="REPLACEABLE"
643 ><I
644 >$substring</I
645 ></TT
646 >
647 at beginning of <TT
648 CLASS="REPLACEABLE"
649 ><I
650 >$string</I
651 ></TT
652 >,
653 where <TT
654 CLASS="REPLACEABLE"
655 ><I
656 >$substring</I
657 ></TT
658 > is a <A
659 HREF="regexp.html#REGEXREF"
660 >regular expression</A
661 >.</P
662 ></DD
663 ><DT
664 >expr "$string" : '\($substring\)'</DT
665 ><DD
666 ><P
667 >Extracts <TT
668 CLASS="REPLACEABLE"
669 ><I
670 >$substring</I
671 ></TT
672 >
673 at beginning of <TT
674 CLASS="REPLACEABLE"
675 ><I
676 >$string</I
677 ></TT
678 >,
679 where <TT
680 CLASS="REPLACEABLE"
681 ><I
682 >$substring</I
683 ></TT
684 > is a regular
685 expression.</P
686 ><P
687 > <TABLE
688 BORDER="0"
689 BGCOLOR="#E0E0E0"
690 WIDTH="90%"
691 ><TR
692 ><TD
693 ><PRE
694 CLASS="PROGRAMLISTING"
695 > 1&nbsp;stringZ=abcABC123ABCabc
696 2&nbsp;# =======
697 3&nbsp;
698 4&nbsp;echo `expr match "$stringZ" '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1
699 5&nbsp;echo `expr "$stringZ" : '\(.[b-c]*[A-Z]..[0-9]\)'` # abcABC1
700 6&nbsp;echo `expr "$stringZ" : '\(.......\)'` # abcABC1
701 7&nbsp;# All of the above forms give an identical result.</PRE
702 ></TD
703 ></TR
704 ></TABLE
705 >
706 </P
707 ></DD
708 ><DT
709 >expr match "$string" '.*\($substring\)'</DT
710 ><DD
711 ><P
712 >Extracts <TT
713 CLASS="REPLACEABLE"
714 ><I
715 >$substring</I
716 ></TT
717 >
718 at <SPAN
719 CLASS="emphasis"
720 ><I
721 CLASS="EMPHASIS"
722 >end</I
723 ></SPAN
724 > of
725 <TT
726 CLASS="REPLACEABLE"
727 ><I
728 >$string</I
729 ></TT
730 >, where
731 <TT
732 CLASS="REPLACEABLE"
733 ><I
734 >$substring</I
735 ></TT
736 > is a regular
737 expression.</P
738 ></DD
739 ><DT
740 >expr "$string" : '.*\($substring\)'</DT
741 ><DD
742 ><P
743 >Extracts <TT
744 CLASS="REPLACEABLE"
745 ><I
746 >$substring</I
747 ></TT
748 >
749 at <SPAN
750 CLASS="emphasis"
751 ><I
752 CLASS="EMPHASIS"
753 >end</I
754 ></SPAN
755 > of <TT
756 CLASS="REPLACEABLE"
757 ><I
758 >$string</I
759 ></TT
760 >,
761 where <TT
762 CLASS="REPLACEABLE"
763 ><I
764 >$substring</I
765 ></TT
766 > is a regular
767 expression.</P
768 ><P
769 > <TABLE
770 BORDER="0"
771 BGCOLOR="#E0E0E0"
772 WIDTH="90%"
773 ><TR
774 ><TD
775 ><PRE
776 CLASS="PROGRAMLISTING"
777 > 1&nbsp;stringZ=abcABC123ABCabc
778 2&nbsp;# ======
779 3&nbsp;
780 4&nbsp;echo `expr match "$stringZ" '.*\([A-C][A-C][A-C][a-c]*\)'` # ABCabc
781 5&nbsp;echo `expr "$stringZ" : '.*\(......\)'` # ABCabc</PRE
782 ></TD
783 ></TR
784 ></TABLE
785 >
786
787 </P
788 ></DD
789 ></DL
790 ></DIV
791 ><DIV
792 CLASS="VARIABLELIST"
793 ><P
794 ><B
795 >Substring Removal</B
796 ></P
797 ><DL
798 ><DT
799 >${string#substring}</DT
800 ><DD
801 ><P
802 >Deletes shortest match of
803 <TT
804 CLASS="REPLACEABLE"
805 ><I
806 >$substring</I
807 ></TT
808 > from
809 <SPAN
810 CLASS="emphasis"
811 ><I
812 CLASS="EMPHASIS"
813 >front</I
814 ></SPAN
815 > of
816 <TT
817 CLASS="REPLACEABLE"
818 ><I
819 >$string</I
820 ></TT
821 >.</P
822 ></DD
823 ><DT
824 >${string##substring}</DT
825 ><DD
826 ><P
827 >Deletes longest match of
828 <TT
829 CLASS="REPLACEABLE"
830 ><I
831 >$substring</I
832 ></TT
833 > from
834 <SPAN
835 CLASS="emphasis"
836 ><I
837 CLASS="EMPHASIS"
838 >front</I
839 ></SPAN
840 > of
841 <TT
842 CLASS="REPLACEABLE"
843 ><I
844 >$string</I
845 ></TT
846 >.</P
847 ><P
848 >
849 <TABLE
850 BORDER="0"
851 BGCOLOR="#E0E0E0"
852 WIDTH="90%"
853 ><TR
854 ><TD
855 ><PRE
856 CLASS="PROGRAMLISTING"
857 > 1&nbsp;stringZ=abcABC123ABCabc
858 2&nbsp;# |----| shortest
859 3&nbsp;# |----------| longest
860 4&nbsp;
861 5&nbsp;echo ${stringZ#a*C} # 123ABCabc
862 6&nbsp;# Strip out shortest match between 'a' and 'C'.
863 7&nbsp;
864 8&nbsp;echo ${stringZ##a*C} # abc
865 9&nbsp;# Strip out longest match between 'a' and 'C'.
866 10&nbsp;
867 11&nbsp;
868 12&nbsp;
869 13&nbsp;# You can parameterize the substrings.
870 14&nbsp;
871 15&nbsp;X='a*C'
872 16&nbsp;
873 17&nbsp;echo ${stringZ#$X} # 123ABCabc
874 18&nbsp;echo ${stringZ##$X} # abc
875 19&nbsp; # As above.</PRE
876 ></TD
877 ></TR
878 ></TABLE
879 >
880 </P
881 ></DD
882 ><DT
883 >${string%substring}</DT
884 ><DD
885 ><P
886 >Deletes shortest match of
887 <TT
888 CLASS="REPLACEABLE"
889 ><I
890 >$substring</I
891 ></TT
892 > from
893 <SPAN
894 CLASS="emphasis"
895 ><I
896 CLASS="EMPHASIS"
897 >back</I
898 ></SPAN
899 > of
900 <TT
901 CLASS="REPLACEABLE"
902 ><I
903 >$string</I
904 ></TT
905 >.</P
906 ><P
907 >For example:
908 <TABLE
909 BORDER="0"
910 BGCOLOR="#E0E0E0"
911 WIDTH="90%"
912 ><TR
913 ><TD
914 ><PRE
915 CLASS="PROGRAMLISTING"
916 > 1&nbsp;# Rename all filenames in $PWD with "TXT" suffix to a "txt" suffix.
917 2&nbsp;# For example, "file1.TXT" becomes "file1.txt" . . .
918 3&nbsp;
919 4&nbsp;SUFF=TXT
920 5&nbsp;suff=txt
921 6&nbsp;
922 7&nbsp;for i in $(ls *.$SUFF)
923 8&nbsp;do
924 9&nbsp; mv -f $i ${i%.$SUFF}.$suff
925 10&nbsp; # Leave unchanged everything *except* the shortest pattern match
926 11&nbsp; #+ starting from the right-hand-side of the variable $i . . .
927 12&nbsp;done ### This could be condensed into a "one-liner" if desired.
928 13&nbsp;
929 14&nbsp;# Thank you, Rory Winston.</PRE
930 ></TD
931 ></TR
932 ></TABLE
933 >
934 </P
935 ></DD
936 ><DT
937 >${string%%substring}</DT
938 ><DD
939 ><P
940 >Deletes longest match of
941 <TT
942 CLASS="REPLACEABLE"
943 ><I
944 >$substring</I
945 ></TT
946 > from
947 <SPAN
948 CLASS="emphasis"
949 ><I
950 CLASS="EMPHASIS"
951 >back</I
952 ></SPAN
953 > of
954 <TT
955 CLASS="REPLACEABLE"
956 ><I
957 >$string</I
958 ></TT
959 >.</P
960 ><P
961 >
962 <TABLE
963 BORDER="0"
964 BGCOLOR="#E0E0E0"
965 WIDTH="90%"
966 ><TR
967 ><TD
968 ><PRE
969 CLASS="PROGRAMLISTING"
970 > 1&nbsp;stringZ=abcABC123ABCabc
971 2&nbsp;# || shortest
972 3&nbsp;# |------------| longest
973 4&nbsp;
974 5&nbsp;echo ${stringZ%b*c} # abcABC123ABCa
975 6&nbsp;# Strip out shortest match between 'b' and 'c', from back of $stringZ.
976 7&nbsp;
977 8&nbsp;echo ${stringZ%%b*c} # a
978 9&nbsp;# Strip out longest match between 'b' and 'c', from back of $stringZ.</PRE
979 ></TD
980 ></TR
981 ></TABLE
982 >
983 </P
984 ><P
985 >This operator is useful for generating filenames.</P
986 ><DIV
987 CLASS="EXAMPLE"
988 ><HR><A
989 NAME="CVT"
990 ></A
991 ><P
992 ><B
993 >Example 10-3. Converting graphic file formats, with filename change</B
994 ></P
995 ><TABLE
996 BORDER="0"
997 BGCOLOR="#E0E0E0"
998 WIDTH="90%"
999 ><TR
1000 ><TD
1001 ><PRE
1002 CLASS="PROGRAMLISTING"
1003 > 1&nbsp;#!/bin/bash
1004 2&nbsp;# cvt.sh:
1005 3&nbsp;# Converts all the MacPaint image files in a directory to "pbm" format.
1006 4&nbsp;
1007 5&nbsp;# Uses the "macptopbm" binary from the "netpbm" package,
1008 6&nbsp;#+ which is maintained by Brian Henderson ([email protected]).
1009 7&nbsp;# Netpbm is a standard part of most Linux distros.
1010 8&nbsp;
1011 9&nbsp;OPERATION=macptopbm
1012 10&nbsp;SUFFIX=pbm # New filename suffix.
1013 11&nbsp;
1014 12&nbsp;if [ -n "$1" ]
1015 13&nbsp;then
1016 14&nbsp; directory=$1 # If directory name given as a script argument...
1017 15&nbsp;else
1018 16&nbsp; directory=$PWD # Otherwise use current working directory.
1019 17&nbsp;fi
1020 18&nbsp;
1021 19&nbsp;# Assumes all files in the target directory are MacPaint image files,
1022 20&nbsp;#+ with a ".mac" filename suffix.
1023 21&nbsp;
1024 22&nbsp;for file in $directory/* # Filename globbing.
1025 23&nbsp;do
1026 24&nbsp; filename=${file%.*c} # Strip ".mac" suffix off filename
1027 25&nbsp; #+ ('.*c' matches everything
1028 26&nbsp; #+ between '.' and 'c', inclusive).
1029 27&nbsp; $OPERATION $file &#62; "$filename.$SUFFIX"
1030 28&nbsp; # Redirect conversion to new filename.
1031 29&nbsp; rm -f $file # Delete original files after converting.
1032 30&nbsp; echo "$filename.$SUFFIX" # Log what is happening to stdout.
1033 31&nbsp;done
1034 32&nbsp;
1035 33&nbsp;exit 0
1036 34&nbsp;
1037 35&nbsp;# Exercise:
1038 36&nbsp;# --------
1039 37&nbsp;# As it stands, this script converts *all* the files in the current
1040 38&nbsp;#+ working directory.
1041 39&nbsp;# Modify it to work *only* on files with a ".mac" suffix.
1042 40&nbsp;
1043 41&nbsp;
1044 42&nbsp;
1045 43&nbsp;# *** And here's another way to do it. *** #
1046 44&nbsp;
1047 45&nbsp;#!/bin/bash
1048 46&nbsp;# Batch convert into different graphic formats.
1049 47&nbsp;# Assumes imagemagick installed (standard in most Linux distros).
1050 48&nbsp;
1051 49&nbsp;INFMT=png # Can be tif, jpg, gif, etc.
1052 50&nbsp;OUTFMT=pdf # Can be tif, jpg, gif, pdf, etc.
1053 51&nbsp;
1054 52&nbsp;for pic in *"$INFMT"
1055 53&nbsp;do
1056 54&nbsp; p2=$(ls "$pic" | sed -e s/\.$INFMT//)
1057 55&nbsp; # echo $p2
1058 56&nbsp; convert "$pic" $p2.$OUTFMT
1059 57&nbsp; done
1060 58&nbsp;
1061 59&nbsp;exit $?</PRE
1062 ></TD
1063 ></TR
1064 ></TABLE
1065 ><HR></DIV
1066 ><DIV
1067 CLASS="EXAMPLE"
1068 ><HR><A
1069 NAME="RA2OGG"
1070 ></A
1071 ><P
1072 ><B
1073 >Example 10-4. Converting streaming audio files to
1074 <I
1075 CLASS="FIRSTTERM"
1076 >ogg</I
1077 ></B
1078 ></P
1079 ><TABLE
1080 BORDER="0"
1081 BGCOLOR="#E0E0E0"
1082 WIDTH="90%"
1083 ><TR
1084 ><TD
1085 ><PRE
1086 CLASS="PROGRAMLISTING"
1087 > 1&nbsp;#!/bin/bash
1088 2&nbsp;# ra2ogg.sh: Convert streaming audio files (*.ra) to ogg.
1089 3&nbsp;
1090 4&nbsp;# Uses the "mplayer" media player program:
1091 5&nbsp;# http://www.mplayerhq.hu/homepage
1092 6&nbsp;# Uses the "ogg" library and "oggenc":
1093 7&nbsp;# http://www.xiph.org/
1094 8&nbsp;#
1095 9&nbsp;# This script may need appropriate codecs installed, such as sipr.so ...
1096 10&nbsp;# Possibly also the compat-libstdc++ package.
1097 11&nbsp;
1098 12&nbsp;
1099 13&nbsp;OFILEPREF=${1%%ra} # Strip off the "ra" suffix.
1100 14&nbsp;OFILESUFF=wav # Suffix for wav file.
1101 15&nbsp;OUTFILE="$OFILEPREF""$OFILESUFF"
1102 16&nbsp;E_NOARGS=85
1103 17&nbsp;
1104 18&nbsp;if [ -z "$1" ] # Must specify a filename to convert.
1105 19&nbsp;then
1106 20&nbsp; echo "Usage: `basename $0` [filename]"
1107 21&nbsp; exit $E_NOARGS
1108 22&nbsp;fi
1109 23&nbsp;
1110 24&nbsp;
1111 25&nbsp;##########################################################################
1112 26&nbsp;mplayer "$1" -ao pcm:file=$OUTFILE
1113 27&nbsp;oggenc "$OUTFILE" # Correct file extension automatically added by oggenc.
1114 28&nbsp;##########################################################################
1115 29&nbsp;
1116 30&nbsp;rm "$OUTFILE" # Delete intermediate *.wav file.
1117 31&nbsp; # If you want to keep it, comment out above line.
1118 32&nbsp;
1119 33&nbsp;exit $?
1120 34&nbsp;
1121 35&nbsp;# Note:
1122 36&nbsp;# ----
1123 37&nbsp;# On a Website, simply clicking on a *.ram streaming audio file
1124 38&nbsp;#+ usually only downloads the URL of the actual *.ra audio file.
1125 39&nbsp;# You can then use "wget" or something similar
1126 40&nbsp;#+ to download the *.ra file itself.
1127 41&nbsp;
1128 42&nbsp;
1129 43&nbsp;# Exercises:
1130 44&nbsp;# ---------
1131 45&nbsp;# As is, this script converts only *.ra filenames.
1132 46&nbsp;# Add flexibility by permitting use of *.ram and other filenames.
1133 47&nbsp;#
1134 48&nbsp;# If you're really ambitious, expand the script
1135 49&nbsp;#+ to do automatic downloads and conversions of streaming audio files.
1136 50&nbsp;# Given a URL, batch download streaming audio files (using "wget")
1137 51&nbsp;#+ and convert them on the fly.</PRE
1138 ></TD
1139 ></TR
1140 ></TABLE
1141 ><HR></DIV
1142 ><P
1143 ><A
1144 NAME="GETOPTSIMPLE1"
1145 ></A
1146 ></P
1147 ><P
1148 >A simple emulation of <A
1149 HREF="extmisc.html#GETOPTY"
1150 >getopt</A
1151 >
1152 using substring-extraction constructs.</P
1153 ><DIV
1154 CLASS="EXAMPLE"
1155 ><HR><A
1156 NAME="GETOPTSIMPLE"
1157 ></A
1158 ><P
1159 ><B
1160 >Example 10-5. Emulating <I
1161 CLASS="FIRSTTERM"
1162 >getopt</I
1163 ></B
1164 ></P
1165 ><TABLE
1166 BORDER="0"
1167 BGCOLOR="#E0E0E0"
1168 WIDTH="90%"
1169 ><TR
1170 ><TD
1171 ><PRE
1172 CLASS="PROGRAMLISTING"
1173 > 1&nbsp;#!/bin/bash
1174 2&nbsp;# getopt-simple.sh
1175 3&nbsp;# Author: Chris Morgan
1176 4&nbsp;# Used in the ABS Guide with permission.
1177 5&nbsp;
1178 6&nbsp;
1179 7&nbsp;getopt_simple()
1180 8&nbsp;{
1181 9&nbsp; echo "getopt_simple()"
1182 10&nbsp; echo "Parameters are '$*'"
1183 11&nbsp; until [ -z "$1" ]
1184 12&nbsp; do
1185 13&nbsp; echo "Processing parameter of: '$1'"
1186 14&nbsp; if [ ${1:0:1} = '/' ]
1187 15&nbsp; then
1188 16&nbsp; tmp=${1:1} # Strip off leading '/' . . .
1189 17&nbsp; parameter=${tmp%%=*} # Extract name.
1190 18&nbsp; value=${tmp##*=} # Extract value.
1191 19&nbsp; echo "Parameter: '$parameter', value: '$value'"
1192 20&nbsp; eval $parameter=$value
1193 21&nbsp; fi
1194 22&nbsp; shift
1195 23&nbsp; done
1196 24&nbsp;}
1197 25&nbsp;
1198 26&nbsp;# Pass all options to getopt_simple().
1199 27&nbsp;getopt_simple $*
1200 28&nbsp;
1201 29&nbsp;echo "test is '$test'"
1202 30&nbsp;echo "test2 is '$test2'"
1203 31&nbsp;
1204 32&nbsp;exit 0 # See also, UseGetOpt.sh, a modified version of this script.
1205 33&nbsp;
1206 34&nbsp;---
1207 35&nbsp;
1208 36&nbsp;sh getopt_example.sh /test=value1 /test2=value2
1209 37&nbsp;
1210 38&nbsp;Parameters are '/test=value1 /test2=value2'
1211 39&nbsp;Processing parameter of: '/test=value1'
1212 40&nbsp;Parameter: 'test', value: 'value1'
1213 41&nbsp;Processing parameter of: '/test2=value2'
1214 42&nbsp;Parameter: 'test2', value: 'value2'
1215 43&nbsp;test is 'value1'
1216 44&nbsp;test2 is 'value2'
1217 45&nbsp;</PRE
1218 ></TD
1219 ></TR
1220 ></TABLE
1221 ><HR></DIV
1222 ></DD
1223 ></DL
1224 ></DIV
1225 ><DIV
1226 CLASS="VARIABLELIST"
1227 ><P
1228 ><B
1229 >Substring Replacement</B
1230 ></P
1231 ><DL
1232 ><DT
1233 ><A
1234 NAME="SUBSTRREPL00"
1235 ></A
1236 >${string/substring/replacement}</DT
1237 ><DD
1238 ><P
1239 > Replace first <I
1240 CLASS="FIRSTTERM"
1241 >match</I
1242 > of
1243 <TT
1244 CLASS="REPLACEABLE"
1245 ><I
1246 >$substring</I
1247 ></TT
1248 > with
1249 <TT
1250 CLASS="REPLACEABLE"
1251 ><I
1252 >$replacement</I
1253 ></TT
1254 >.
1255 <A
1256 NAME="AEN6164"
1257 HREF="#FTN.AEN6164"
1258 >[2]</A
1259 >
1260 </P
1261 ></DD
1262 ><DT
1263 ><A
1264 NAME="SUBSTRREPL01"
1265 ></A
1266 >${string//substring/replacement}</DT
1267 ><DD
1268 ><P
1269 >Replace all matches of
1270 <TT
1271 CLASS="REPLACEABLE"
1272 ><I
1273 >$substring</I
1274 ></TT
1275 > with
1276 <TT
1277 CLASS="REPLACEABLE"
1278 ><I
1279 >$replacement</I
1280 ></TT
1281 >.</P
1282 ><P
1283 >
1284 <TABLE
1285 BORDER="0"
1286 BGCOLOR="#E0E0E0"
1287 WIDTH="90%"
1288 ><TR
1289 ><TD
1290 ><PRE
1291 CLASS="PROGRAMLISTING"
1292 > 1&nbsp;stringZ=abcABC123ABCabc
1293 2&nbsp;
1294 3&nbsp;echo ${stringZ/abc/xyz} # xyzABC123ABCabc
1295 4&nbsp; # Replaces first match of 'abc' with 'xyz'.
1296 5&nbsp;
1297 6&nbsp;echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
1298 7&nbsp; # Replaces all matches of 'abc' with # 'xyz'.
1299 8&nbsp;
1300 9&nbsp;echo ---------------
1301 10&nbsp;echo "$stringZ" # abcABC123ABCabc
1302 11&nbsp;echo ---------------
1303 12&nbsp; # The string itself is not altered!
1304 13&nbsp;
1305 14&nbsp;# Can the match and replacement strings be parameterized?
1306 15&nbsp;match=abc
1307 16&nbsp;repl=000
1308 17&nbsp;echo ${stringZ/$match/$repl} # 000ABC123ABCabc
1309 18&nbsp;# ^ ^ ^^^
1310 19&nbsp;echo ${stringZ//$match/$repl} # 000ABC123ABC000
1311 20&nbsp;# Yes! ^ ^ ^^^ ^^^
1312 21&nbsp;
1313 22&nbsp;echo
1314 23&nbsp;
1315 24&nbsp;# What happens if no $replacement string is supplied?
1316 25&nbsp;echo ${stringZ/abc} # ABC123ABCabc
1317 26&nbsp;echo ${stringZ//abc} # ABC123ABC
1318 27&nbsp;# A simple deletion takes place.</PRE
1319 ></TD
1320 ></TR
1321 ></TABLE
1322 >
1323 </P
1324 ></DD
1325 ><DT
1326 ><A
1327 NAME="SUBSTRREPL02"
1328 ></A
1329 >${string/#substring/replacement}</DT
1330 ><DD
1331 ><P
1332 >If <TT
1333 CLASS="REPLACEABLE"
1334 ><I
1335 >$substring</I
1336 ></TT
1337 > matches
1338 <SPAN
1339 CLASS="emphasis"
1340 ><I
1341 CLASS="EMPHASIS"
1342 >front</I
1343 ></SPAN
1344 > end of
1345 <TT
1346 CLASS="REPLACEABLE"
1347 ><I
1348 >$string</I
1349 ></TT
1350 >, substitute
1351 <TT
1352 CLASS="REPLACEABLE"
1353 ><I
1354 >$replacement</I
1355 ></TT
1356 > for
1357 <TT
1358 CLASS="REPLACEABLE"
1359 ><I
1360 >$substring</I
1361 ></TT
1362 >.</P
1363 ></DD
1364 ><DT
1365 ><A
1366 NAME="SUBSTRREPL03"
1367 ></A
1368 >${string/%substring/replacement}</DT
1369 ><DD
1370 ><P
1371 >If <TT
1372 CLASS="REPLACEABLE"
1373 ><I
1374 >$substring</I
1375 ></TT
1376 > matches
1377 <SPAN
1378 CLASS="emphasis"
1379 ><I
1380 CLASS="EMPHASIS"
1381 >back</I
1382 ></SPAN
1383 > end of
1384 <TT
1385 CLASS="REPLACEABLE"
1386 ><I
1387 >$string</I
1388 ></TT
1389 >, substitute
1390 <TT
1391 CLASS="REPLACEABLE"
1392 ><I
1393 >$replacement</I
1394 ></TT
1395 > for
1396 <TT
1397 CLASS="REPLACEABLE"
1398 ><I
1399 >$substring</I
1400 ></TT
1401 >.</P
1402 ><P
1403 >
1404 <TABLE
1405 BORDER="0"
1406 BGCOLOR="#E0E0E0"
1407 WIDTH="90%"
1408 ><TR
1409 ><TD
1410 ><PRE
1411 CLASS="PROGRAMLISTING"
1412 > 1&nbsp;stringZ=abcABC123ABCabc
1413 2&nbsp;
1414 3&nbsp;echo ${stringZ/#abc/XYZ} # XYZABC123ABCabc
1415 4&nbsp; # Replaces front-end match of 'abc' with 'XYZ'.
1416 5&nbsp;
1417 6&nbsp;echo ${stringZ/%abc/XYZ} # abcABC123ABCXYZ
1418 7&nbsp; # Replaces back-end match of 'abc' with 'XYZ'.</PRE
1419 ></TD
1420 ></TR
1421 ></TABLE
1422 >
1423 </P
1424 ></DD
1425 ></DL
1426 ></DIV
1427 ><DIV
1428 CLASS="SECT2"
1429 ><H2
1430 CLASS="SECT2"
1431 ><A
1432 NAME="AWKSTRINGMANIP"
1433 ></A
1434 >10.1.1. Manipulating strings using awk</H2
1435 ><P
1436 ><A
1437 NAME="AWKSTRINGMANIP2"
1438 ></A
1439 ></P
1440 ><P
1441 >A Bash script may invoke the string manipulation facilities of
1442 <A
1443 HREF="awk.html#AWKREF"
1444 >awk</A
1445 > as an alternative to using its
1446 built-in operations.</P
1447 ><DIV
1448 CLASS="EXAMPLE"
1449 ><HR><A
1450 NAME="SUBSTRINGEX"
1451 ></A
1452 ><P
1453 ><B
1454 >Example 10-6. Alternate ways of extracting and locating substrings</B
1455 ></P
1456 ><TABLE
1457 BORDER="0"
1458 BGCOLOR="#E0E0E0"
1459 WIDTH="100%"
1460 ><TR
1461 ><TD
1462 ><PRE
1463 CLASS="PROGRAMLISTING"
1464 > 1&nbsp;#!/bin/bash
1465 2&nbsp;# substring-extraction.sh
1466 3&nbsp;
1467 4&nbsp;String=23skidoo1
1468 5&nbsp;# 012345678 Bash
1469 6&nbsp;# 123456789 awk
1470 7&nbsp;# Note different string indexing system:
1471 8&nbsp;# Bash numbers first character of string as 0.
1472 9&nbsp;# Awk numbers first character of string as 1.
1473 10&nbsp;
1474 11&nbsp;echo ${String:2:4} # position 3 (0-1-2), 4 characters long
1475 12&nbsp; # skid
1476 13&nbsp;
1477 14&nbsp;# The awk equivalent of ${string:pos:length} is substr(string,pos,length).
1478 15&nbsp;echo | awk '
1479 16&nbsp;{ print substr("'"${String}"'",3,4) # skid
1480 17&nbsp;}
1481 18&nbsp;'
1482 19&nbsp;# Piping an empty "echo" to awk gives it dummy input,
1483 20&nbsp;#+ and thus makes it unnecessary to supply a filename.
1484 21&nbsp;
1485 22&nbsp;echo "----"
1486 23&nbsp;
1487 24&nbsp;# And likewise:
1488 25&nbsp;
1489 26&nbsp;echo | awk '
1490 27&nbsp;{ print index("'"${String}"'", "skid") # 3
1491 28&nbsp;} # (skid starts at position 3)
1492 29&nbsp;' # The awk equivalent of "expr index" ...
1493 30&nbsp;
1494 31&nbsp;exit 0</PRE
1495 ></TD
1496 ></TR
1497 ></TABLE
1498 ><HR></DIV
1499 ></DIV
1500 ><DIV
1501 CLASS="SECT2"
1502 ><H2
1503 CLASS="SECT2"
1504 ><A
1505 NAME="STRFDISC"
1506 ></A
1507 >10.1.2. Further Reference</H2
1508 ><P
1509 >For more on string manipulation in scripts, refer to <A
1510 HREF="parameter-substitution.html"
1511 >Section 10.2</A
1512 > and the
1513 <A
1514 HREF="moreadv.html#EXPEXTRSUB"
1515 >relevant section</A
1516 > of the <A
1517 HREF="moreadv.html#EXPRREF"
1518 >expr</A
1519 > command listing.</P
1520 ><P
1521 >Script examples:
1522 <OL
1523 TYPE="1"
1524 ><LI
1525 ><P
1526 ><A
1527 HREF="moreadv.html#EX45"
1528 >Example 16-9</A
1529 ></P
1530 ></LI
1531 ><LI
1532 ><P
1533 ><A
1534 HREF="parameter-substitution.html#LENGTH"
1535 >Example 10-9</A
1536 ></P
1537 ></LI
1538 ><LI
1539 ><P
1540 ><A
1541 HREF="parameter-substitution.html#PATTMATCHING"
1542 >Example 10-10</A
1543 ></P
1544 ></LI
1545 ><LI
1546 ><P
1547 ><A
1548 HREF="parameter-substitution.html#RFE"
1549 >Example 10-11</A
1550 ></P
1551 ></LI
1552 ><LI
1553 ><P
1554 ><A
1555 HREF="parameter-substitution.html#VARMATCH"
1556 >Example 10-13</A
1557 ></P
1558 ></LI
1559 ><LI
1560 ><P
1561 ><A
1562 HREF="contributed-scripts.html#INSERTIONSORT"
1563 >Example A-36</A
1564 ></P
1565 ></LI
1566 ><LI
1567 ><P
1568 ><A
1569 HREF="contributed-scripts.html#QKY"
1570 >Example A-41</A
1571 ></P
1572 ></LI
1573 ></OL
1574 >
1575 </P
1576 ></DIV
1577 ></DIV
1578 ></DIV
1579 ><H3
1580 CLASS="FOOTNOTES"
1581 >Notes</H3
1582 ><TABLE
1583 BORDER="0"
1584 CLASS="FOOTNOTES"
1585 WIDTH="100%"
1586 ><TR
1587 ><TD
1588 ALIGN="LEFT"
1589 VALIGN="TOP"
1590 WIDTH="5%"
1591 ><A
1592 NAME="FTN.AEN5987"
1593 HREF="manipulatingvars.html#AEN5987"
1594 >[1]</A
1595 ></TD
1596 ><TD
1597 ALIGN="LEFT"
1598 VALIGN="TOP"
1599 WIDTH="95%"
1600 ><P
1601 >This applies to either command-line
1602 arguments or parameters passed to a <A
1603 HREF="functions.html#FUNCTIONREF"
1604 >function</A
1605 >.</P
1606 ></TD
1607 ></TR
1608 ><TR
1609 ><TD
1610 ALIGN="LEFT"
1611 VALIGN="TOP"
1612 WIDTH="5%"
1613 ><A
1614 NAME="FTN.AEN6164"
1615 HREF="manipulatingvars.html#AEN6164"
1616 >[2]</A
1617 ></TD
1618 ><TD
1619 ALIGN="LEFT"
1620 VALIGN="TOP"
1621 WIDTH="95%"
1622 ><P
1623 >Note that
1624 <TT
1625 CLASS="REPLACEABLE"
1626 ><I
1627 >$substring</I
1628 ></TT
1629 > and
1630 <TT
1631 CLASS="REPLACEABLE"
1632 ><I
1633 >$replacement</I
1634 ></TT
1635 > may refer to
1636 either <I
1637 CLASS="FIRSTTERM"
1638 >literal strings</I
1639 > or
1640 <I
1641 CLASS="FIRSTTERM"
1642 >variables</I
1643 >, depending on
1644 context. See the first usage example.</P
1645 ></TD
1646 ></TR
1647 ></TABLE
1648 ><DIV
1649 CLASS="NAVFOOTER"
1650 ><HR
1651 ALIGN="LEFT"
1652 WIDTH="100%"><TABLE
1653 SUMMARY="Footer navigation table"
1654 WIDTH="100%"
1655 BORDER="0"
1656 CELLPADDING="0"
1657 CELLSPACING="0"
1658 ><TR
1659 ><TD
1660 WIDTH="33%"
1661 ALIGN="left"
1662 VALIGN="top"
1663 ><A
1664 HREF="randomvar.html"
1665 ACCESSKEY="P"
1666 >Prev</A
1667 ></TD
1668 ><TD
1669 WIDTH="34%"
1670 ALIGN="center"
1671 VALIGN="top"
1672 ><A
1673 HREF="index.html"
1674 ACCESSKEY="H"
1675 >Home</A
1676 ></TD
1677 ><TD
1678 WIDTH="33%"
1679 ALIGN="right"
1680 VALIGN="top"
1681 ><A
1682 HREF="parameter-substitution.html"
1683 ACCESSKEY="N"
1684 >Next</A
1685 ></TD
1686 ></TR
1687 ><TR
1688 ><TD
1689 WIDTH="33%"
1690 ALIGN="left"
1691 VALIGN="top"
1692 >$RANDOM: generate random integer</TD
1693 ><TD
1694 WIDTH="34%"
1695 ALIGN="center"
1696 VALIGN="top"
1697 ><A
1698 HREF="part3.html"
1699 ACCESSKEY="U"
1700 >Up</A
1701 ></TD
1702 ><TD
1703 WIDTH="33%"
1704 ALIGN="right"
1705 VALIGN="top"
1706 >Parameter Substitution</TD
1707 ></TR
1708 ></TABLE
1709 ></DIV
1710 ></BODY
1711 ></HTML
1712 >