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 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 99.2beta8 (1.46)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7. Shell Scripting</TITLE>
<META NAME="description" CONTENT="7. Shell Scripting">
<META NAME="keywords" CONTENT="rute">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v99.2beta8">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="rute.css">
<LINK REL="next" HREF="node11.html">
<LINK REL="previous" HREF="node9.html">
<LINK REL="up" HREF="rute.html">
<LINK REL="next" HREF="node11.html">
</HEAD>
<BODY BGCOLOR=#FFFFFF >
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR><TD align=left bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-purchase.html"><FONT COLOR=white>Purchase</FONT></A>
</FONT>
</TD><TD align=center bgcolor="#000000">
<FONT COLOR=white>
Copyright © 2002 Paul Sheer. <A HREF="copying.html"><FONT COLOR=white>Click here for copying permissions.</FONT></A>
</FONT>
</TD><TD align=right bgcolor="#000000">
<FONT COLOR=white>
<A HREF="http://www.icon.co.za/~psheer/rute-home.html"><FONT COLOR=white>Home</FONT></A>
</FONT>
</TD></TR>
<TR><TD colspan=2 align=left bgcolor="#ECEBF4">
<IMG SRC="va-btn-small-light-60.png">
</TD><TD align=right bgcolor="#ECEBF4">
<IMG SRC="sflogo2-steel-60.png">
</TD></TR>
</TABLE><BR>
<!--Navigation Panel-->
<A NAME="tex2html1851"
HREF="node11.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html1847"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html1841"
HREF="node9.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html1849"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html1852"
HREF="node11.html">8. Streams and sed</A>
<B> Up:</B> <A NAME="tex2html1848"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html1842"
HREF="node9.html">6. Editing Text Files</A>
  <B> <A NAME="tex2html1850"
HREF="node1.html">Contents</A></B>
<BR>
<BR>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html1853"
HREF="#SECTION001010000000000000000">7.1 Introduction</A>
<LI><A NAME="tex2html1854"
HREF="#SECTION001020000000000000000">7.2 Looping: the <TT>
<FONT COLOR="#0000ff">while</FONT></TT> and <TT>
<FONT COLOR="#0000ff">until</FONT></TT> Statements</A>
<LI><A NAME="tex2html1855"
HREF="#SECTION001030000000000000000">7.3 Looping: the <TT>
<FONT COLOR="#0000ff">for</FONT></TT> Statement</A>
<LI><A NAME="tex2html1856"
HREF="#SECTION001040000000000000000">7.4 <TT>
<FONT COLOR="#0000ff">break</FONT></TT>ing Out of Loops and <TT>
<FONT COLOR="#0000ff">continue</FONT></TT>ing</A>
<LI><A NAME="tex2html1857"
HREF="#SECTION001050000000000000000">7.5 Looping Over Glob Expressions</A>
<LI><A NAME="tex2html1858"
HREF="#SECTION001060000000000000000">7.6 The <TT>
<FONT COLOR="#0000ff">case</FONT></TT> Statement</A>
<LI><A NAME="tex2html1859"
HREF="#SECTION001070000000000000000">7.7 Using Functions: the <TT>
<FONT COLOR="#0000ff">function</FONT></TT> Keyword</A>
<LI><A NAME="tex2html1860"
HREF="#SECTION001080000000000000000">7.8 Properly Processing Command-Line Args: <TT>
<FONT COLOR="#0000ff">shift</FONT></TT></A>
<LI><A NAME="tex2html1861"
HREF="#SECTION001090000000000000000">7.9 More on Command-Line Arguments: <TT>
<FONT COLOR="#0000ff">$@</FONT></TT> and <TT>
<FONT COLOR="#0000ff">$0</FONT></TT></A>
<LI><A NAME="tex2html1862"
HREF="#SECTION0010100000000000000000">7.10 Single Forward Quote Notation</A>
<LI><A NAME="tex2html1863"
HREF="#SECTION0010110000000000000000">7.11 Double-Quote Notation</A>
<LI><A NAME="tex2html1864"
HREF="#SECTION0010120000000000000000">7.12 Backward-Quote Substitution</A>
</UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION001000000000000000000">
7. Shell Scripting</A>
</H1>
<P>
<A NAME="chap:shellscript"></A>
<P>
This chapter introduces you to the concept of <I>computer programming</I>.
So far, you have entered commands one at a time. Computer programming is merely
the idea of getting a number of commands to be executed, that in combination
do some unique powerful function.
<P>
<H1><A NAME="SECTION001010000000000000000">
7.1 Introduction</A>
</H1>
<P>
To execute a number of commands in sequence, create a file with a <TT>
<FONT COLOR="#0000ff">.sh</FONT></TT>
extension, into which you will enter your commands. The <TT>
<FONT COLOR="#0000ff">.sh</FONT></TT> extension
is not strictly necessary but serves as a reminder that the file contains special
text called a <I>shell script</I>. From now on, the word <I>script</I> will
be used to describe any sequence of commands placed in a text file. Now do a
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>chmod 0755 myfile.sh</code><br>
</FONT></TD></TR></TABLE><P>
which allows the file to be run in the explained way.
<P>
Edit the file using your favorite text editor. The first line should be as follows with no
whitespace. <FONT COLOR="#ffa500">[Whitespace are tabs and spaces, and in some contexts, newline (end of
line) characters.]</FONT>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
</FONT></TD></TR></TABLE><P>
The line dictates that the following program is a <I>shell</I> script, meaning
that it accepts the same sort of commands that you have normally been typing
at the prompt. Now enter a number of commands that you would like to be executed.
You can start with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo "Hi there"</code><br>
<code>echo "what is your name? (Type your name here and press Enter)"</code><br>
<code>read NM</code><br>
<code>echo "Hello $NM"</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Now, exit from your editor and type <TT>
<FONT COLOR="#0000ff">./myfile.sh</FONT></TT>. This will <I>execute</I> <FONT COLOR="#ffa500">[Cause the computer to read and act on your list of commands, also called <I>running</I>
the program.
]</FONT> the file. Note that typing <TT>
<FONT COLOR="#0000ff">./myfile.sh</FONT></TT> is no different from typing
any other command at the shell prompt. Your file <TT>
<FONT COLOR="#0000ff">myfile.sh</FONT></TT> has in fact
become a new U<SMALL>NIX</SMALL> command all of its own.
<P>
Note what the <TT>
<FONT COLOR="#0000ff">read</FONT></TT> command is doing. It creates a pigeonhole called
<TT>
<FONT COLOR="#0000ff">NM</FONT></TT>, and then inserts text read from the keyboard into that pigeonhole.
Thereafter, whenever the shell encounters <TT>
<FONT COLOR="#0000ff">NM</FONT></TT>, its contents are
written out instead of the letters NM (provided you write a <TT>
<FONT COLOR="#0000ff">$</FONT></TT> in front
of it). We say that <TT>
<FONT COLOR="#0000ff">NM</FONT></TT> is a <I>variable</I> because its contents can
vary.
<P>
You can use shell scripts like a calculator. Try
<A NAME="page:arithshellbrace"></A>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo "I will work out X*Y"</code><br>
<code>echo "Enter X"</code><br>
<code>read X</code><br>
<code>echo "Enter Y"</code><br>
<code>read Y</code><br>
<code>echo "X*Y = $X*$Y = $[X*Y]"</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">[</FONT></TT> and <TT>
<FONT COLOR="#0000ff">]</FONT></TT> mean that everything between must be <I>evaluated</I> <FONT COLOR="#ffa500">[Substituted, worked out, or reduced to some simplified form.
]</FONT> as a <I>numerical expression</I> <FONT COLOR="#ffa500">[Sequence of numbers with <TT>
<FONT COLOR="#0000ff">+</FONT></TT>, <TT>
<FONT COLOR="#0000ff">-</FONT></TT>, <TT>
<FONT COLOR="#0000ff">*</FONT></TT>, etc. between them.
]</FONT>. You can, in fact, do a calculation at any time by typing at the prompt
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>echo $[3*6+2*8+9]</code><br>
</FONT></TD></TR></TABLE><P>
<FONT COLOR="#ffa500">[Note that the shell that you are using allows such <TT>
<FONT COLOR="#0000ff">[ ]</FONT></TT>
notation. On some U<SMALL>NIX</SMALL> systems you will have to use the <TT>
<FONT COLOR="#0000ff">expr</FONT></TT>
command to get the same effect.]</FONT>
<P>
<H1><A NAME="SECTION001020000000000000000">
7.2 Looping to Repeat Commands: the <TT>
<FONT COLOR="#0000ff">while</FONT></TT> and <TT>
<FONT COLOR="#0000ff">until</FONT></TT> Statements</A>
</H1>
<P>
The shell reads each line in succession from top to bottom: this is called <I>program
flow</I>. Now suppose you would like a command to be executed more than once--you
would like to alter the program flow so that the shell reads particular commands
repeatedly. The <TT>
<FONT COLOR="#0000ff">while</FONT></TT> command executes a sequence of commands many
times. Here is an example (<TT>
<FONT COLOR="#0000ff">-le</FONT></TT> stands for <I>less than or equal</I>):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>N=1</code><br>
<code>while test "$N" -le "10"</code><br>
<code>do</code><br>
<code> echo "Number $N"</code><br>
<code> N=$[N+1]</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
The <TT>
<FONT COLOR="#0000ff">N=1</FONT></TT> creates a variable called <TT>
<FONT COLOR="#0000ff">N</FONT></TT> and places the number
<TT>
<FONT COLOR="#0000ff">1</FONT></TT> into it. The <TT>
<FONT COLOR="#0000ff">while</FONT></TT> command executes all the commands between
the <TT>
<FONT COLOR="#0000ff">do</FONT></TT> and the <TT>
<FONT COLOR="#0000ff">done</FONT></TT> repetitively until the <TT>
<FONT COLOR="#0000ff">test</FONT></TT>
condition is no longer true (i.e., until <TT>
<FONT COLOR="#0000ff">N</FONT></TT> is greater than <TT>
<FONT COLOR="#0000ff">10</FONT></TT>).
The <TT>
<FONT COLOR="#0000ff">-le</FONT></TT> stands for <I>less than or equal to</I>. See <TT>
<FONT COLOR="#0000ff">test</FONT></TT>(1) (that is, run <TT>
<FONT COLOR="#0000ff">man 1 test</FONT></TT>)
to learn about the other types of tests you can do on variables. Also be aware
of how <TT>
<FONT COLOR="#0000ff">N</FONT></TT> is replaced with a new value that becomes <TT>
<FONT COLOR="#0000ff">1</FONT></TT> greater
with each repetition of the <TT>
<FONT COLOR="#0000ff">while</FONT></TT> loop.
<P>
You should note here that each line is a distinct command--the commands are
<I>newline-separated</I>. You can also have more than one command on a line
by separating them with a semicolon as follows:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>N=1 ; while test "$N" -le "10"; do echo "Number $N"; N=$[N+1] ; done</code><br>
</FONT></TD></TR></TABLE><P>
(Try counting down from 10 with <TT>
<FONT COLOR="#0000ff">-ge</FONT></TT> (<I>greater than or equal</I>).)
It is easy to see that shell scripts are extremely powerful, because any kind
of command can be executed with conditions and loops.
<P>
The <TT>
<FONT COLOR="#0000ff">until</FONT></TT> statement is identical to <TT>
<FONT COLOR="#0000ff">while</FONT></TT> except that the
reverse logic is applied. The same functionality can be achieved with
<TT>
<FONT COLOR="#0000ff">-gt</FONT></TT> (<I>greater than</I>):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>N=1 ; until test "$N" -gt "10"; do echo "Number $N"; N=$[N+1] ; done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION001030000000000000000">
7.3 Looping to Repeat Commands: the <TT>
<FONT COLOR="#0000ff">for</FONT></TT> Statement</A>
</H1>
<P>
The <TT>
<FONT COLOR="#0000ff">for</FONT></TT> command also allows execution of commands multiple times. It
works like this:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in cows sheep chickens pigs</code><br>
<code>do</code><br>
<code> echo "$i is a farm animal"</code><br>
<code>done</code><br>
<code>echo -e "but\nGNUs are not farm animals"</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">for</FONT></TT> command takes each string after the <TT>
<FONT COLOR="#0000ff">in</FONT></TT>, and executes
the lines between <TT>
<FONT COLOR="#0000ff">do</FONT></TT> and <TT>
<FONT COLOR="#0000ff">done</FONT></TT> with <TT>
<FONT COLOR="#0000ff">i</FONT></TT> substituted
for that string. The strings can be anything (even numbers) but are often file names.
<P>
The <TT>
<FONT COLOR="#0000ff">if</FONT></TT> command executes a number of commands if a condition is met
(<TT>
<FONT COLOR="#0000ff">-gt</FONT></TT> stands for <I>greater than</I>, <TT>
<FONT COLOR="#0000ff">-lt</FONT></TT> stands for <I>less
than</I>). The <TT>
<FONT COLOR="#0000ff">if</FONT></TT> command executes all the lines between the <TT>
<FONT COLOR="#0000ff">if</FONT></TT>
and the <TT>
<FONT COLOR="#0000ff">fi</FONT></TT> (``if'' spelled backwards).
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>X=10</code><br>
<code>Y=5</code><br>
<code>if test "$X" -gt "$Y" ; then</code><br>
<code> echo "$X is greater than $Y"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">if</FONT></TT> command in its full form can contain as much as:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>X=10</code><br>
<code>Y=5</code><br>
<code>if test "$X" -gt "$Y" ; then</code><br>
<code> echo "$X is greater than $Y"</code><br>
<code>elif test "$X" -lt "$Y" ; then</code><br>
<code> echo "$X is less than $Y"</code><br>
<code>else</code><br>
<code> echo "$X is equal to $Y"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Now let us create a script that interprets its arguments. Create a new script
called <TT>
<FONT COLOR="#0000ff">backup-lots.sh</FONT></TT>, containing:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>for i in 0 1 2 3 4 5 6 7 8 9 ; do</code><br>
<code> cp $1 $1.BAK-$i</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Now create a file <TT>
<FONT COLOR="#0000ff">important_data</FONT></TT> with anything in it and then run
<TT>
<FONT COLOR="#0000ff">./backup-lots.sh important_data</FONT></TT>, which will copy the file 10 times
with 10 different extensions. As you can see, the variable <TT>
<FONT COLOR="#0000ff">$1</FONT></TT> has
a special meaning--it is the first argument on the command-line. Now let's
get a little bit more sophisticated (<TT>
<FONT COLOR="#0000ff">-e</FONT></TT> test whether the file <I>exists</I>):
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>if test "$1" = "" ; then</code><br>
<code> echo "Usage: backup-lots.sh <filename>"</code><br>
<code> exit</code><br>
<code>fi</code><br>
<code>for i in 0 1 2 3 4 5 6 7 8 9 ; do</code><br>
<code> NEW_FILE=$1.BAK-$i</code><br>
<code> if test -e $NEW_FILE ; then</code><br>
<code> echo "backup-lots.sh: **warning** $NEW_FILE"</code><br>
<code> echo " already exists - skipping"</code><br>
<code> else</code><br>
<code> cp $1 $NEW_FILE</code><br>
<code> fi</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION001040000000000000000">
7.4 <TT>
<FONT COLOR="#0000ff">break</FONT></TT>ing Out of Loops and <TT>
<FONT COLOR="#0000ff">continue</FONT></TT>ing</A>
</H1>
<P>
A loop that requires premature termination can
include the <TT>
<FONT COLOR="#0000ff">break</FONT></TT> statement within it:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>for i in 0 1 2 3 4 5 6 7 8 9 ; do</code><br>
<code> NEW_FILE=$1.BAK-$i</code><br>
<code> if test -e $NEW_FILE ; then</code><br>
<code> echo "backup-lots.sh: **error** $NEW_FILE"</code><br>
<code> echo " already exists - exitting"</code><br>
<code> break</code><br>
<code> else</code><br>
<code> cp $1 $NEW_FILE</code><br>
<code> fi</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
which causes program execution to continue on the line after the <TT>
<FONT COLOR="#0000ff">done</FONT></TT>.
If two loops are nested within each other, then the command <TT>
<FONT COLOR="#0000ff">break 2</FONT></TT>
causes program execution to break out of <I>both</I> loops; and so
on for values above <TT>
<FONT COLOR="#0000ff">2</FONT></TT>.
<P>
The <TT>
<FONT COLOR="#0000ff">continue</FONT></TT> statement is also useful for terminating
the current iteration of the loop. This means that if a <TT>
<FONT COLOR="#0000ff">continue</FONT></TT>
statement is encountered, execution will immediately continue from
the top of the loop, thus ignoring the remainder of the body of
the loop:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>for i in 0 1 2 3 4 5 6 7 8 9 ; do</code><br>
<code> NEW_FILE=$1.BAK-$i</code><br>
<code> if test -e $NEW_FILE ; then</code><br>
<code> echo "backup-lots.sh: **warning** $NEW_FILE"</code><br>
<code> echo " already exists - skipping"</code><br>
<code> continue</code><br>
<code> fi</code><br>
<code> cp $1 $NEW_FILE</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Note that both <TT>
<FONT COLOR="#0000ff">break</FONT></TT> and <TT>
<FONT COLOR="#0000ff">continue</FONT></TT> work inside
<TT>
<FONT COLOR="#0000ff">for</FONT></TT>, <TT>
<FONT COLOR="#0000ff">while</FONT></TT>, and <TT>
<FONT COLOR="#0000ff">until</FONT></TT> loops.
<P>
<H1><A NAME="SECTION001050000000000000000">
7.5 Looping Over Glob Expressions</A>
</H1>
<P>
We know that the shell can expand file names when given <I>wildcards</I>. For
instance, we can type <TT>
<FONT COLOR="#0000ff">ls *.txt</FONT></TT> to list all files ending with <TT>
<FONT COLOR="#0000ff">.txt</FONT></TT>.
This applies equally well in any situation, for instance:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>for i in *.txt ; do</code><br>
<code> echo "found a file:" $i</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The <TT>
<FONT COLOR="#0000ff">*.txt</FONT></TT> is expanded to all matching files. <I>These files
are searched for in the current directory</I>. If you include an absolute path
then the shell will search in that directory:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>for i in /usr/doc/*/*.txt ; do</code><br>
<code> echo "found a file:" $i</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
This example demonstrates the shell's ability to search for matching files and expand
an absolute path.
<P>
<H1><A NAME="SECTION001060000000000000000">
7.6 The <TT>
<FONT COLOR="#0000ff">case</FONT></TT> Statement</A>
</H1>
<P>
The <TT>
<FONT COLOR="#0000ff">case</FONT></TT> statement can make a potentially complicated program very
short. It is best explained with an example.
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code>case $1 in</code><br>
<code> --test|-t)</code><br>
<code> echo "you used the --test option"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> --help|-h)</code><br>
<code> echo "Usage:"</code><br>
<code> echo " myprog.sh [--test|--help|--version]"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> --version|-v)</code><br>
<code> echo "myprog.sh version 0.0.1"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> -*)</code><br>
<code> echo "No such option $1"</code><br>
<code> echo "Usage:"</code><br>
<code> echo " myprog.sh [--test|--help|--version]"</code><br>
<code> exit 1</code><br>
<code> ;;</code><br>
<code>esac</code><br>
<code> </code><br>
<code>echo "You typed \"$1\" on the command-line"</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Above you can see that we are trying to process the first argument to a program.
It can be one of several options, so using <TT>
<FONT COLOR="#0000ff">if</FONT></TT> statements will result
in a long program. The <TT>
<FONT COLOR="#0000ff">case</FONT></TT> statement allows us to specify several
possible statement blocks depending on the value of a variable. Note how each
statement block is separated by <TT>
<FONT COLOR="#0000ff">;;</FONT></TT>. The strings before the <TT>
<FONT COLOR="#0000ff">)</FONT></TT>
are glob expression matches. The first successful match causes that block to
be executed. The <TT>
<FONT COLOR="#0000ff">|</FONT></TT> symbol enables us to enter several possible glob
expressions.
<P>
<H1><A NAME="SECTION001070000000000000000">
7.7 Using Functions: the <TT>
<FONT COLOR="#0000ff">function</FONT></TT> Keyword</A>
</H1>
<P>
<A NAME="sec:functions"></A>
<P>
So far, our programs execute mostly from top to bottom. Often, code needs to
be repeated, but it is considered bad programming practice to repeat groups
of statements that have the same functionality. Function definitions provide
a way to group statement blocks into one. A function
groups a list of commands and assigns it a name. For example:
<P>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>function usage ()</code><br>
<code>{</code><br>
<code> echo "Usage:"</code><br>
<code> echo " myprog.sh [--test|--help|--version]"</code><br>
<code>}</code><br>
<code> </code><br>
<code>case $1 in</code><br>
<code> --test|-t)</code><br>
<code> echo "you used the --test option"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> --help|-h)</code><br>
<code> usage</code><br>
<code> ;;</code><br>
<code> --version|-v)</code><br>
<code> echo "myprog.sh version 0.0.2"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> -*)</code><br>
<code> echo "Error: no such option $1"</code><br>
<code> usage</code><br>
<code> exit 1</code><br>
<code> ;;</code><br>
<code>esac</code><br>
<code> </code><br>
<code>echo "You typed \"$1\" on the command-line"</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Wherever the <TT>
<FONT COLOR="#0000ff">usage</FONT></TT> keyword appears, it is effectively substituted
for the two lines inside the <TT>
<FONT COLOR="#0000ff">{</FONT></TT> and <TT>
<FONT COLOR="#0000ff">}</FONT></TT>. There are obvious
advantages to this approach: if you would like to change the program <I>usage</I>
description, you only need to change it in one place in the code. Good programs
use functions so liberally that they never have more than 50 lines of program
code in a row.
<P>
<H1><A NAME="SECTION001080000000000000000">
7.8 Properly Processing Command-Line Arguments: the <TT>
<FONT COLOR="#0000ff">shift</FONT></TT> Keyword</A>
</H1>
<P>
Most programs we have seen can take many command-line arguments, sometimes in any order.
Here is how we can make our own shell scripts with this functionality. The command-line
arguments can be reached with <TT>
<FONT COLOR="#0000ff">$1</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$2</FONT></TT>, etc. The script,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>echo "The first argument is: $1, second argument is: $2, third argument is: $3"</code><br>
</FONT></TD></TR></TABLE><P>
can be run with
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>myfile.sh dogs cats birds</code><br>
</FONT></TD></TR></TABLE><P>
and prints
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>The first argument is: dogs, second argument is: cats, third argument is: birds</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Now we need to loop through each argument and decide what to do with it. A script
like
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in $1 $2 $3 $4 ; do</code><br>
<code> <statments></code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
doesn't give us much flexibilty. The <TT>
<FONT COLOR="#0000ff">shift</FONT></TT> keyword is meant to make
things easier. It shifts up all the arguments by one place so that <TT>
<FONT COLOR="#0000ff">$1</FONT></TT>
gets the value of <TT>
<FONT COLOR="#0000ff">$2</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$2</FONT></TT> gets the value of <TT>
<FONT COLOR="#0000ff">$3</FONT></TT>,
and so on. (<TT>
<FONT COLOR="#0000ff">!=</FONT></TT> tests that the <TT>
<FONT COLOR="#0000ff">"$1"</FONT></TT> is not
equal to <TT>
<FONT COLOR="#0000ff">""</FONT></TT>, that is, whether it is empty and is hence past
the last argument.) Try
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>while test "$1" != "" ; do</code><br>
<code> echo $1</code><br>
<code> shift</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
and run the program with lots of arguments.
<P>
Now we can put any sort of condition
statements within the loop to process the arguments in turn:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>15</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>20</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>25</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>30</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>#!/bin/sh</code><br>
<code> </code><br>
<code>function usage ()</code><br>
<code>{</code><br>
<code> echo "Usage:"</code><br>
<code> echo " myprog.sh [--test|--help|--version] [--echo <text>]"</code><br>
<code>}</code><br>
<code> </code><br>
<code>while test "$1" != "" ; do</code><br>
<code> case $1 in</code><br>
<code> --echo|-e)</code><br>
<code> echo "$2"</code><br>
<code> shift</code><br>
<code> ;;</code><br>
<code> --test|-t)</code><br>
<code> echo "you used the --test option"</code><br>
<code> ;;</code><br>
<code> --help|-h)</code><br>
<code> usage</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> --version|-v)</code><br>
<code> echo "myprog.sh version 0.0.3"</code><br>
<code> exit 0</code><br>
<code> ;;</code><br>
<code> -*)</code><br>
<code> echo "Error: no such option $1"</code><br>
<code> usage</code><br>
<code> exit 1</code><br>
<code> ;;</code><br>
<code> esac</code><br>
<code> shift</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<TT>
<FONT COLOR="#0000ff">myprog.sh</FONT></TT> can now run with multiple arguments on the command-line.
<P>
<H1><A NAME="SECTION001090000000000000000">
7.9 More on Command-Line Arguments: <TT>
<FONT COLOR="#0000ff">$@</FONT></TT> and <TT>
<FONT COLOR="#0000ff">$0</FONT></TT></A>
</H1>
<P>
Whereas <TT>
<FONT COLOR="#0000ff">$1</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$2</FONT></TT>, <TT>
<FONT COLOR="#0000ff">$3</FONT></TT>, etc. expand to the individual
arguments passed to the program, <TT>
<FONT COLOR="#0000ff">$@</FONT></TT> expands to <I>all</I> arguments.
This behavior is useful for passing all remaining arguments onto a second command. For
instance,
<P>
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>if test "$1" = "--special" ; then</code><br>
<code> shift</code><br>
<code> myprog2.sh "$@"</code><br>
<code>fi</code><br>
</FONT></TD></TR></TABLE><P>
<TT>
<FONT COLOR="#0000ff">$0</FONT></TT> means the name of the program itself and not any command-line argument.
It is the command used to invoke the current program. In the above cases, it
is <TT>
<FONT COLOR="#0000ff">./myprog.sh</FONT></TT>. Note that <TT>
<FONT COLOR="#0000ff">$0</FONT></TT> is immune to <TT>
<FONT COLOR="#0000ff">shift</FONT></TT> operations.
<P>
<H1><A NAME="SECTION0010100000000000000000">
7.10 Single Forward Quote Notation</A>
</H1>
<P>
Single forward quotes <TT>
<FONT COLOR="#0000ff"> ' </FONT></TT> <I>protect</I> the enclosed text from
the shell. In other words, you can place any odd characters inside forward quotes,
and the shell will treat them literally and reproduce your text exactly. For
instance, you may want to <TT>
<FONT COLOR="#0000ff">echo</FONT></TT> an actual <TT>
<FONT COLOR="#0000ff">$</FONT></TT> to the screen to
produce an output like <TT>
<FONT COLOR="#0000ff">costs $1000</FONT></TT>. You can use <TT>
<FONT COLOR="#0000ff">echo 'costs
$1000'</FONT></TT> instead of <TT>
<FONT COLOR="#0000ff">echo "costs $1000"</FONT></TT>.
<P>
<H1><A NAME="SECTION0010110000000000000000">
7.11 Double-Quote Notation</A>
</H1>
<P>
Double quotes <TT>
<FONT COLOR="#0000ff"> " </FONT></TT> have the opposite sense of single quotes.
They allow <I>all</I> shell interpretations to take place inside them. The
reason they are used at all is only to group text containing whitespace into
a single word, because the shell will usually break up text along whitespace
boundaries. Try,
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in "henry john mary sue" ; do</code><br>
<code> echo "$i is a person"</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
compared to
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>for i in henry john mary sue ; do</code><br>
<code> echo $i is a person</code><br>
<code>done</code><br>
</FONT></TD></TR></TABLE><P>
<P>
<H1><A NAME="SECTION0010120000000000000000">
7.12 Backward-Quote Substitution</A>
</H1>
<P>
<A NAME="sec:backquote"></A>
<P>
Backward quotes <TT>
<FONT COLOR="#0000ff"> ` </FONT></TT> have a special meaning to the shell. When a command
is inside backward quotes it means that the command should be run and its <I>output</I>
substituted in place of the backquotes. Take, for example, the <TT>
<FONT COLOR="#0000ff">cat</FONT></TT> command.
Create a small file, <TT>
<FONT COLOR="#0000ff">to_be_catted</FONT></TT>, with only the text <TT>
<FONT COLOR="#0000ff">daisy</FONT></TT>
inside it. Create a shell script
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>X=`cat to_be_catted`</code><br>
<code>echo $X</code><br>
</FONT></TD></TR></TABLE><P>
<P>
The value of <TT>
<FONT COLOR="#0000ff">X</FONT></TT> is set to the output of the <TT>
<FONT COLOR="#0000ff">cat</FONT></TT> command, which in this
case is the word <TT>
<FONT COLOR="#0000ff">daisy</FONT></TT>. This is a powerful tool. Consider the <TT>
<FONT COLOR="#0000ff">expr</FONT></TT>
command:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>X=`expr 100 + 50 '*' 3`</code><br>
<code>echo $X</code><br>
</FONT></TD></TR></TABLE><P>
<P>
Hence we can use <TT>
<FONT COLOR="#0000ff">expr</FONT></TT> and backquotes to do mathematics inside our shell
script. Here is a function to calculate factorials. Note how we enclose the <TT>
<FONT COLOR="#0000ff">*</FONT></TT>
in forward quotes. They prevent the shell from expanding the <TT>
<FONT COLOR="#0000ff">*</FONT></TT> into matching file names:
<P><TABLE nowrap="1" width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD valign="top" class="source" width="2%"><FONT color=red>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>5</code></font><code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<code> </code><br>
<font size="-1"><code>10</code></font><code> </code><br>
</FONT></TD><TD valign="top" class="source" bgcolor="#FFE0C0"><FONT color=blue>
<code>function factorial ()</code><br>
<code>{</code><br>
<code> N=$1</code><br>
<code> A=1</code><br>
<code> while test $N -gt 0 ; do</code><br>
<code> A=`expr $A '*' $N`</code><br>
<code> N=`expr $N - 1`</code><br>
<code> done</code><br>
<code> echo $A</code><br>
<code>}</code><br>
</FONT></TD></TR></TABLE><P>
<P>
We can see that the square braces used further above can actually suffice for
most of the times where we would like to use <TT>
<FONT COLOR="#0000ff">expr</FONT></TT>. (However, <TT>
<FONT COLOR="#0000ff">$[]</FONT></TT>
notation is an extension of the GNU shells and is not
a standard feature on all varients of U<SMALL>NIX</SMALL>.) We can now run <TT>
<FONT COLOR="#0000ff">factorial
20</FONT></TT> and see the output. If we want to assign the output to a variable,
we can do this with <TT>
<FONT COLOR="#0000ff">X=`factorial 20`</FONT></TT>.
<P>
Note that another notation which gives the effect of a backward quote is
<TT>
<FONT COLOR="#0000ff">$(</FONT></TT><I>command</I><TT>
<FONT COLOR="#0000ff">)</FONT></TT>, which is identical to <TT>
<FONT COLOR="#0000ff">`</FONT></TT><I>command</I><TT>
<FONT COLOR="#0000ff">`</FONT></TT>.
Here, I will always use the older backward quote style.
<P>
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html1851"
HREF="node11.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html1847"
HREF="rute.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html1841"
HREF="node9.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html1849"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html1852"
HREF="node11.html">8. Streams and sed</A>
<B> Up:</B> <A NAME="tex2html1848"
HREF="rute.html">rute</A>
<B> Previous:</B> <A NAME="tex2html1842"
HREF="node9.html">6. Editing Text Files</A>
  <B> <A NAME="tex2html1850"
HREF="node1.html">Contents</A></B>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|