~ubuntu-branches/ubuntu/trusty/abs-guide/trusty-proposed

« back to all changes in this revision

Viewing changes to HTML/loops.html

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2014-01-01 12:26:22 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20140101122622-n04ky7hk3mt1x13l
Tags: 6.6-1
* New upstream release; thanks to Sébastien Villemot for the report;
  Closes: #733155
* debian/control
  - bump Standards-Version to 3.9.5 (no changes needed)
  - use packaging repository canonical URLs

Show diffs side-by-side

added added

removed removed

Lines of Context:
154
154
>
155
155
 
156
156
          <A
157
 
NAME="AEN6529"
158
 
HREF="#FTN.AEN6529"
 
157
NAME="AEN6557"
 
158
HREF="#FTN.AEN6557"
159
159
>[1]</A
160
160
>
161
161
 
525
525
><HR></DIV
526
526
><P
527
527
><A
 
528
NAME="PARAMLI2"
 
529
></A
 
530
></P
 
531
><P
 
532
>The <TT
 
533
CLASS="USERINPUT"
 
534
><B
 
535
>[list]</B
 
536
></TT
 
537
> in a
 
538
              <I
 
539
CLASS="FIRSTTERM"
 
540
>for loop</I
 
541
> may be parameterized.</P
 
542
><DIV
 
543
CLASS="EXAMPLE"
 
544
><HR><A
 
545
NAME="FILEINFO01"
 
546
></A
 
547
><P
 
548
><B
 
549
>Example 11-4. Operating on a parameterized file list</B
 
550
></P
 
551
><TABLE
 
552
BORDER="0"
 
553
BGCOLOR="#E0E0E0"
 
554
WIDTH="90%"
 
555
><TR
 
556
><TD
 
557
><PRE
 
558
CLASS="PROGRAMLISTING"
 
559
>   1&nbsp;#!/bin/bash
 
560
   2&nbsp;
 
561
   3&nbsp;filename="*txt"
 
562
   4&nbsp;
 
563
   5&nbsp;for file in $filename
 
564
   6&nbsp;do
 
565
   7&nbsp; echo "Contents of $file"
 
566
   8&nbsp; echo "---"
 
567
   9&nbsp; cat "$file"
 
568
  10&nbsp; echo
 
569
  11&nbsp;done</PRE
 
570
></TD
 
571
></TR
 
572
></TABLE
 
573
><HR></DIV
 
574
><P
 
575
><A
528
576
NAME="LIGLOB"
529
577
></A
530
578
></P
558
606
></A
559
607
><P
560
608
><B
561
 
>Example 11-4. Operating on files with a <I
 
609
>Example 11-5. Operating on files with a <I
562
610
CLASS="FIRSTTERM"
563
611
>for</I
564
612
> loop</B
572
620
><PRE
573
621
CLASS="PROGRAMLISTING"
574
622
>   1&nbsp;#!/bin/bash
575
 
   2&nbsp;# list-glob.sh: Generating [list] in a for-loop, using "globbing"
576
 
   3&nbsp;
577
 
   4&nbsp;echo
578
 
   5&nbsp;
579
 
   6&nbsp;for file in *
580
 
   7&nbsp;#           ^  Bash performs filename expansion
581
 
   8&nbsp;#+             on expressions that globbing recognizes.
582
 
   9&nbsp;do
583
 
  10&nbsp;  ls -l "$file"  # Lists all files in $PWD (current directory).
584
 
  11&nbsp;  #  Recall that the wild card character "*" matches every filename,
585
 
  12&nbsp;  #+ however, in "globbing," it doesn't match dot-files.
586
 
  13&nbsp;
587
 
  14&nbsp;  #  If the pattern matches no file, it is expanded to itself.
588
 
  15&nbsp;  #  To prevent this, set the nullglob option
589
 
  16&nbsp;  #+   (shopt -s nullglob).
590
 
  17&nbsp;  #  Thanks, S.C.
591
 
  18&nbsp;done
592
 
  19&nbsp;
593
 
  20&nbsp;echo; echo
594
 
  21&nbsp;
595
 
  22&nbsp;for file in [jx]*
596
 
  23&nbsp;do
597
 
  24&nbsp;  rm -f $file    # Removes only files beginning with "j" or "x" in $PWD.
598
 
  25&nbsp;  echo "Removed file \"$file\"".
599
 
  26&nbsp;done
600
 
  27&nbsp;
601
 
  28&nbsp;echo
602
 
  29&nbsp;
603
 
  30&nbsp;exit 0</PRE
 
623
   2&nbsp;# list-glob.sh: Generating [list] in a for-loop, using "globbing" ...
 
624
   3&nbsp;# Globbing = filename expansion.
 
625
   4&nbsp;
 
626
   5&nbsp;echo
 
627
   6&nbsp;
 
628
   7&nbsp;for file in *
 
629
   8&nbsp;#           ^  Bash performs filename expansion
 
630
   9&nbsp;#+             on expressions that globbing recognizes.
 
631
  10&nbsp;do
 
632
  11&nbsp;  ls -l "$file"  # Lists all files in $PWD (current directory).
 
633
  12&nbsp;  #  Recall that the wild card character "*" matches every filename,
 
634
  13&nbsp;  #+ however, in "globbing," it doesn't match dot-files.
 
635
  14&nbsp;
 
636
  15&nbsp;  #  If the pattern matches no file, it is expanded to itself.
 
637
  16&nbsp;  #  To prevent this, set the nullglob option
 
638
  17&nbsp;  #+   (shopt -s nullglob).
 
639
  18&nbsp;  #  Thanks, S.C.
 
640
  19&nbsp;done
 
641
  20&nbsp;
 
642
  21&nbsp;echo; echo
 
643
  22&nbsp;
 
644
  23&nbsp;for file in [jx]*
 
645
  24&nbsp;do
 
646
  25&nbsp;  rm -f $file    # Removes only files beginning with "j" or "x" in $PWD.
 
647
  26&nbsp;  echo "Removed file \"$file\"".
 
648
  27&nbsp;done
 
649
  28&nbsp;
 
650
  29&nbsp;echo
 
651
  30&nbsp;
 
652
  31&nbsp;exit 0</PRE
604
653
></TD
605
654
></TR
606
655
></TABLE
642
691
></A
643
692
><P
644
693
><B
645
 
>Example 11-5. Missing <TT
 
694
>Example 11-6. Missing <TT
646
695
CLASS="USERINPUT"
647
696
><B
648
697
>in [list]</B
706
755
>,
707
756
              <A
708
757
HREF="loops.html#SYMLINKS"
709
 
>Example 11-10</A
 
758
>Example 11-11</A
710
759
> and <A
711
760
HREF="mathc.html#BASE"
712
761
>Example 16-48</A
718
767
></A
719
768
><P
720
769
><B
721
 
>Example 11-6. Generating the <TT
 
770
>Example 11-7. Generating the <TT
722
771
CLASS="USERINPUT"
723
772
><B
724
773
>[list]</B
769
818
></A
770
819
><P
771
820
><B
772
 
>Example 11-7. A <I
 
821
>Example 11-8. A <I
773
822
CLASS="FIRSTTERM"
774
823
>grep</I
775
824
> replacement
835
884
></A
836
885
><P
837
886
><B
838
 
>Example 11-8. Listing all users on the system</B
 
887
>Example 11-9. Listing all users on the system</B
839
888
></P
840
889
><TABLE
841
890
BORDER="0"
854
903
   7&nbsp;for name in $(awk 'BEGIN{FS=":"}{print $1}' &#60; "$PASSWORD_FILE" )
855
904
   8&nbsp;# Field separator = :    ^^^^^^
856
905
   9&nbsp;# Print first field              ^^^^^^^^
857
 
  10&nbsp;# Get input from password file               ^^^^^^^^^^^^^^^^^
 
906
  10&nbsp;# Get input from password file  /etc/passwd  ^^^^^^^^^^^^^^^^^
858
907
  11&nbsp;do
859
908
  12&nbsp;  echo "USER #$n = $name"
860
909
  13&nbsp;  let "n += 1"
865
914
  18&nbsp;# USER #2 = bin
866
915
  19&nbsp;# USER #3 = daemon
867
916
  20&nbsp;# ...
868
 
  21&nbsp;# USER #30 = bozo
 
917
  21&nbsp;# USER #33 = bozo
869
918
  22&nbsp;
870
919
  23&nbsp;exit $?
871
920
  24&nbsp;
873
922
  26&nbsp;#  ----------
874
923
  27&nbsp;#  How is it that an ordinary user, or a script run by same,
875
924
  28&nbsp;#+ can read /etc/passwd? (Hint: Check the /etc/passwd file permissions.)
876
 
  29&nbsp;#  Isn't this a security hole? Why or why not?</PRE
 
925
  29&nbsp;#  Is this a security hole? Why or why not?</PRE
877
926
></TD
878
927
></TR
879
928
></TABLE
893
942
></A
894
943
><P
895
944
><B
896
 
>Example 11-9. Checking all the binaries in a directory for
 
945
>Example 11-10. Checking all the binaries in a directory for
897
946
              authorship</B
898
947
></P
899
948
><TABLE
989
1038
></A
990
1039
><P
991
1040
><B
992
 
>Example 11-10. Listing the <I
 
1041
>Example 11-11. Listing the <I
993
1042
CLASS="FIRSTTERM"
994
1043
>symbolic
995
1044
                links</I
1087
1136
></A
1088
1137
><P
1089
1138
><B
1090
 
>Example 11-11. Symbolic links in a directory, saved to a file</B
 
1139
>Example 11-12. Symbolic links in a directory, saved to a file</B
1091
1140
></P
1092
1141
><TABLE
1093
1142
BORDER="0"
1100
1149
>   1&nbsp;#!/bin/bash
1101
1150
   2&nbsp;# symlinks.sh: Lists symbolic links in a directory.
1102
1151
   3&nbsp;
1103
 
   4&nbsp;OUTFILE=symlinks.list                         # save file
 
1152
   4&nbsp;OUTFILE=symlinks.list                         # save-file
1104
1153
   5&nbsp;
1105
1154
   6&nbsp;directory=${1-`pwd`}
1106
1155
   7&nbsp;#  Defaults to current working directory,
1116
1165
  17&nbsp;done | sort &#62;&#62; "$OUTFILE"                     # stdout of loop
1117
1166
  18&nbsp;#           ^^^^^^^^^^^^^                       redirected to save file.
1118
1167
  19&nbsp;
1119
 
  20&nbsp;exit 0</PRE
 
1168
  20&nbsp;# echo "Output file = $OUTFILE"
 
1169
  21&nbsp;
 
1170
  22&nbsp;exit $?</PRE
1120
1171
></TD
1121
1172
></TR
1122
1173
></TABLE
1143
1194
></A
1144
1195
><P
1145
1196
><B
1146
 
>Example 11-12. A C-style <I
 
1197
>Example 11-13. A C-style <I
1147
1198
CLASS="FIRSTTERM"
1148
1199
>for</I
1149
1200
> loop</B
1248
1299
></A
1249
1300
><P
1250
1301
><B
1251
 
>Example 11-13. Using <I
 
1302
>Example 11-14. Using <I
1252
1303
CLASS="FIRSTTERM"
1253
1304
>efax</I
1254
1305
> in batch mode</B
1307
1358
></TR
1308
1359
></TABLE
1309
1360
><HR></DIV
 
1361
><DIV
 
1362
CLASS="NOTE"
 
1363
><TABLE
 
1364
CLASS="NOTE"
 
1365
WIDTH="90%"
 
1366
BORDER="0"
 
1367
><TR
 
1368
><TD
 
1369
WIDTH="25"
 
1370
ALIGN="CENTER"
 
1371
VALIGN="TOP"
 
1372
><IMG
 
1373
SRC="common/note.png"
 
1374
HSPACE="5"
 
1375
ALT="Note"></TD
 
1376
><TD
 
1377
ALIGN="LEFT"
 
1378
VALIGN="TOP"
 
1379
><P
 
1380
><A
 
1381
NAME="NODODONE"
 
1382
></A
 
1383
>The
 
1384
            <A
 
1385
HREF="internal.html#KEYWORDREF"
 
1386
>keywords</A
 
1387
>
 
1388
            <B
 
1389
CLASS="COMMAND"
 
1390
>do</B
 
1391
> and <B
 
1392
CLASS="COMMAND"
 
1393
>done</B
 
1394
> delineate
 
1395
            the <I
 
1396
CLASS="FIRSTTERM"
 
1397
>for-loop</I
 
1398
> command block. However,
 
1399
            these may, in certain contexts, be omitted by framing the
 
1400
            command block within <A
 
1401
HREF="special-chars.html#CODEBLOCKREF"
 
1402
>curly
 
1403
            brackets</A
 
1404
>
 
1405
 
 
1406
            <TABLE
 
1407
BORDER="0"
 
1408
BGCOLOR="#E0E0E0"
 
1409
WIDTH="90%"
 
1410
><TR
 
1411
><TD
 
1412
><PRE
 
1413
CLASS="PROGRAMLISTING"
 
1414
>   1&nbsp;for((n=1; n&#60;=10; n++)) 
 
1415
   2&nbsp;# No do!
 
1416
   3&nbsp;{
 
1417
   4&nbsp;  echo -n "* $n *"
 
1418
   5&nbsp;}
 
1419
   6&nbsp;# No done!
 
1420
   7&nbsp;
 
1421
   8&nbsp;
 
1422
   9&nbsp;# Outputs:
 
1423
  10&nbsp;# * 1 ** 2 ** 3 ** 4 ** 5 ** 6 ** 7 ** 8 ** 9 ** 10 *
 
1424
  11&nbsp;# And, echo $? returns 0, so Bash does not register an error.
 
1425
  12&nbsp;
 
1426
  13&nbsp;
 
1427
  14&nbsp;echo
 
1428
  15&nbsp;
 
1429
  16&nbsp;
 
1430
  17&nbsp;#  But, note that in a classic for-loop:    for n in [list] ...
 
1431
  18&nbsp;#+ a terminal semicolon is required.
 
1432
  19&nbsp;
 
1433
  20&nbsp;for n in 1 2 3
 
1434
  21&nbsp;{  echo -n "$n "; }
 
1435
  22&nbsp;#               ^
 
1436
  23&nbsp;
 
1437
  24&nbsp;
 
1438
  25&nbsp;# Thank you, YongYe, for pointing this out.</PRE
 
1439
></TD
 
1440
></TR
 
1441
></TABLE
 
1442
>
 
1443
             </P
 
1444
></TD
 
1445
></TR
 
1446
></TABLE
 
1447
></DIV
1310
1448
></DD
1311
1449
><DT
1312
1450
><A
1439
1577
></A
1440
1578
><P
1441
1579
><B
1442
 
>Example 11-14. Simple <I
 
1580
>Example 11-15. Simple <I
1443
1581
CLASS="FIRSTTERM"
1444
1582
>while</I
1445
1583
> loop</B
1483
1621
></A
1484
1622
><P
1485
1623
><B
1486
 
>Example 11-15. Another <I
 
1624
>Example 11-16. Another <I
1487
1625
CLASS="FIRSTTERM"
1488
1626
>while</I
1489
1627
> loop</B
1535
1673
></A
1536
1674
><P
1537
1675
><B
1538
 
>Example 11-16. <I
 
1676
>Example 11-17. <I
1539
1677
CLASS="FIRSTTERM"
1540
1678
>while</I
1541
1679
> loop with multiple conditions</B
1557
1695
   7&nbsp;      echo
1558
1696
   8&nbsp;      previous=$var1
1559
1697
   9&nbsp;      [ "$var1" != end ] # Keeps track of what $var1 was previously.
1560
 
  10&nbsp;      # Four conditions on "while", but only last one controls loop.
 
1698
  10&nbsp;      # Four conditions on *while*, but only the final one controls loop.
1561
1699
  11&nbsp;      # The *last* exit status is the one that counts.
1562
1700
  12&nbsp;do
1563
1701
  13&nbsp;echo "Input variable #1 (end to exit) "
1598
1736
></A
1599
1737
><P
1600
1738
><B
1601
 
>Example 11-17. C-style syntax in a <I
 
1739
>Example 11-18. C-style syntax in a <I
1602
1740
CLASS="FIRSTTERM"
1603
1741
>while</I
1604
1742
> loop</B
1714
1852
><DIV
1715
1853
CLASS="SIDEBAR"
1716
1854
><A
1717
 
NAME="AEN6808"
 
1855
NAME="AEN6853"
1718
1856
></A
1719
1857
><P
1720
1858
><A
1942
2080
></A
1943
2081
><P
1944
2082
><B
1945
 
>Example 11-18. <I
 
2083
>Example 11-19. <I
1946
2084
CLASS="FIRSTTERM"
1947
2085
>until</I
1948
2086
> loop</B
1969
2107
  12&nbsp;  echo
1970
2108
  13&nbsp;done  
1971
2109
  14&nbsp;
1972
 
  15&nbsp;# ------------------------------------------- #
 
2110
  15&nbsp;#                     ---                        #
1973
2111
  16&nbsp;
1974
2112
  17&nbsp;#  As with "for" and "while" loops,
1975
2113
  18&nbsp;#+ an "until" loop permits C-like test constructs.
2044
2182
VALIGN="TOP"
2045
2183
WIDTH="5%"
2046
2184
><A
2047
 
NAME="FTN.AEN6529"
2048
 
HREF="loops.html#AEN6529"
 
2185
NAME="FTN.AEN6557"
 
2186
HREF="loops.html#AEN6557"
2049
2187
>[1]</A
2050
2188
></TD
2051
2189
><TD