File: moreadv.html

package info (click to toggle)
abs-guide 10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,952 kB
  • sloc: sh: 14,129; makefile: 81
file content (1738 lines) | stat: -rw-r--r-- 33,954 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
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
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Complex Commands</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="External Filters, Programs and Commands"
HREF="external.html"><LINK
REL="PREVIOUS"
TITLE="External Filters, Programs and Commands"
HREF="external.html"><LINK
REL="NEXT"
TITLE="Time / Date Commands"
HREF="timedate.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="external.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 16. External Filters, Programs and Commands</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="timedate.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="MOREADV"
></A
>16.2. Complex Commands</H1
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="CCLISTING1"
></A
>Commands for more advanced users</B
></P
><DL
><DT
><A
NAME="FINDREF"
></A
><B
CLASS="COMMAND"
>find</B
></DT
><DD
><P
><A
NAME="FINDREF0"
></A
></P
><P
>-exec <TT
CLASS="REPLACEABLE"
><I
>COMMAND</I
></TT
> \;</P
><P
>Carries out <TT
CLASS="REPLACEABLE"
><I
>COMMAND</I
></TT
> on
	      each file that <B
CLASS="COMMAND"
>find</B
> matches.  The
	      command sequence terminates with <SPAN
CLASS="TOKEN"
>;</SPAN
> (the
	      <SPAN
CLASS="QUOTE"
>";"</SPAN
> is <A
HREF="escapingsection.html#ESCP"
>escaped</A
> to
	      make certain the shell passes it to <B
CLASS="COMMAND"
>find</B
>
	      literally, without interpreting it as a special character).</P
><P
>	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>find ~/ -name '*.txt'</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>/home/bozo/.kde/share/apps/karm/karmdata.txt
 /home/bozo/misc/irmeyc.txt
 /home/bozo/test-scripts/1.txt</TT
>
 	      </PRE
></TD
></TR
></TABLE
>
	  </P
><P
><A
NAME="CURLYBRACKETSREF"
></A
></P
><P
>If <TT
CLASS="REPLACEABLE"
><I
>COMMAND</I
></TT
> contains
	      <SPAN
CLASS="TOKEN"
>{}</SPAN
>, then <B
CLASS="COMMAND"
>find</B
>
	      substitutes the full path name of the selected file for
	      <SPAN
CLASS="QUOTE"
>"{}"</SPAN
>.</P
><P
>          <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;find ~/ -name 'core*' -exec rm {} \;
   2&nbsp;# Removes all core dump files from user's home directory.</PRE
></TD
></TR
></TABLE
>
	  </P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;find /home/bozo/projects -mtime -1
   2&nbsp;#                               ^   Note minus sign!
   3&nbsp;#  Lists all files in /home/bozo/projects directory tree
   4&nbsp;#+ that were modified within the last day (current_day - 1).
   5&nbsp;#
   6&nbsp;find /home/bozo/projects -mtime 1
   7&nbsp;#  Same as above, but modified *exactly* one day ago.
   8&nbsp;#
   9&nbsp;#  mtime = last modification time of the target file
  10&nbsp;#  ctime = last status change time (via 'chmod' or otherwise)
  11&nbsp;#  atime = last access time
  12&nbsp;
  13&nbsp;DIR=/home/bozo/junk_files
  14&nbsp;find "$DIR" -type f -atime +5 -exec rm {} \;
  15&nbsp;#                          ^           ^^
  16&nbsp;#  Curly brackets are placeholder for the path name output by "find."
  17&nbsp;#
  18&nbsp;#  Deletes all files in "/home/bozo/junk_files"
  19&nbsp;#+ that have not been accessed in *at least* 5 days (plus sign ... +5).
  20&nbsp;#
  21&nbsp;#  "-type filetype", where
  22&nbsp;#  f = regular file
  23&nbsp;#  d = directory
  24&nbsp;#  l = symbolic link, etc.
  25&nbsp;#
  26&nbsp;#  (The 'find' manpage and info page have complete option listings.)</PRE
></TD
></TR
></TABLE
>
          </P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;
   2&nbsp;
   3&nbsp;# Finds all IP addresses (xxx.xxx.xxx.xxx) in /etc directory files.
   4&nbsp;# There a few extraneous hits. Can they be filtered out?
   5&nbsp;
   6&nbsp;# Possibly by:
   7&nbsp;
   8&nbsp;find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \
   9&nbsp;| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'
  10&nbsp;#
  11&nbsp;#  [:digit:] is one of the character classes
  12&nbsp;#+ introduced with the POSIX 1003.2 standard. 
  13&nbsp;
  14&nbsp;# Thanks, Stphane Chazelas. </PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The <TT
CLASS="OPTION"
>-exec</TT
> option to
	      <B
CLASS="COMMAND"
>find</B
> should not be confused with the <A
HREF="internal.html#EXECREF"
>exec</A
> shell builtin.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX57"
></A
><P
><B
>Example 16-3. <I
CLASS="FIRSTTERM"
>Badname</I
>, eliminate file names
		in current directory containing bad characters and <A
HREF="special-chars.html#WHITESPACEREF"
>whitespace</A
>.</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# badname.sh
   3&nbsp;# Delete filenames in current directory containing bad characters.
   4&nbsp;
   5&nbsp;for filename in *
   6&nbsp;do
   7&nbsp;  badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\&#60;\&#62;\&#38;\*\|\$]/p`
   8&nbsp;# badname=`echo "$filename" | sed -n '/[+{;"\=?~()&#60;&#62;&#38;*|$]/p'`  also works.
   9&nbsp;# Deletes files containing these nasties:     + { ; " \ = ? ~ ( ) &#60; &#62; &#38; * | $
  10&nbsp;#
  11&nbsp;  rm $badname 2&#62;/dev/null
  12&nbsp;#             ^^^^^^^^^^^ Error messages deep-sixed.
  13&nbsp;done
  14&nbsp;
  15&nbsp;# Now, take care of files containing all manner of whitespace.
  16&nbsp;find . -name "* *" -exec rm -f {} \;
  17&nbsp;# The path name of the file that _find_ finds replaces the "{}".
  18&nbsp;# The '\' ensures that the ';' is interpreted literally, as end of command.
  19&nbsp;
  20&nbsp;exit 0
  21&nbsp;
  22&nbsp;#---------------------------------------------------------------------
  23&nbsp;# Commands below this line will not execute because of _exit_ command.
  24&nbsp;
  25&nbsp;# An alternative to the above script:
  26&nbsp;find . -name '*[+{;"\\=?~()&#60;&#62;&#38;*|$ ]*' -maxdepth 0 \
  27&nbsp;-exec rm -f '{}' \;
  28&nbsp;#  The "-maxdepth 0" option ensures that _find_ will not search
  29&nbsp;#+ subdirectories below $PWD.
  30&nbsp;
  31&nbsp;# (Thanks, S.C.)</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="IDELETE"
></A
><P
><B
>Example 16-4. Deleting a file by its <I
CLASS="FIRSTTERM"
>inode</I
>
	        number</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# idelete.sh: Deleting a file by its inode number.
   3&nbsp;
   4&nbsp;#  This is useful when a filename starts with an illegal character,
   5&nbsp;#+ such as ? or -.
   6&nbsp;
   7&nbsp;ARGCOUNT=1                      # Filename arg must be passed to script.
   8&nbsp;E_WRONGARGS=70
   9&nbsp;E_FILE_NOT_EXIST=71
  10&nbsp;E_CHANGED_MIND=72
  11&nbsp;
  12&nbsp;if [ $# -ne "$ARGCOUNT" ]
  13&nbsp;then
  14&nbsp;  echo "Usage: `basename $0` filename"
  15&nbsp;  exit $E_WRONGARGS
  16&nbsp;fi  
  17&nbsp;
  18&nbsp;if [ ! -e "$1" ]
  19&nbsp;then
  20&nbsp;  echo "File \""$1"\" does not exist."
  21&nbsp;  exit $E_FILE_NOT_EXIST
  22&nbsp;fi  
  23&nbsp;
  24&nbsp;inum=`ls -i | grep "$1" | awk '{print $1}'`
  25&nbsp;# inum = inode (index node) number of file
  26&nbsp;# -----------------------------------------------------------------------
  27&nbsp;# Every file has an inode, a record that holds its physical address info.
  28&nbsp;# -----------------------------------------------------------------------
  29&nbsp;
  30&nbsp;echo; echo -n "Are you absolutely sure you want to delete \"$1\" (y/n)? "
  31&nbsp;# The '-v' option to 'rm' also asks this.
  32&nbsp;read answer
  33&nbsp;case "$answer" in
  34&nbsp;[nN]) echo "Changed your mind, huh?"
  35&nbsp;      exit $E_CHANGED_MIND
  36&nbsp;      ;;
  37&nbsp;*)    echo "Deleting file \"$1\".";;
  38&nbsp;esac
  39&nbsp;
  40&nbsp;find . -inum $inum -exec rm {} \;
  41&nbsp;#                           ^^
  42&nbsp;#        Curly brackets are placeholder
  43&nbsp;#+       for text output by "find."
  44&nbsp;echo "File "\"$1"\" deleted!"
  45&nbsp;
  46&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>The <B
CLASS="COMMAND"
>find</B
> command also works
	      without the <TT
CLASS="OPTION"
>-exec</TT
> option.</P
><P
>	    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;#  Find suid root files.
   3&nbsp;#  A strange suid file might indicate a security hole,
   4&nbsp;#+ or even a system intrusion.
   5&nbsp;
   6&nbsp;directory="/usr/sbin"
   7&nbsp;# Might also try /sbin, /bin, /usr/bin, /usr/local/bin, etc.
   8&nbsp;permissions="+4000"  # suid root (dangerous!)
   9&nbsp;
  10&nbsp;
  11&nbsp;for file in $( find "$directory" -perm "$permissions" )
  12&nbsp;do
  13&nbsp;  ls -ltF --author "$file"
  14&nbsp;done</PRE
></TD
></TR
></TABLE
>
	    </P
><P
>See <A
HREF="filearchiv.html#EX48"
>Example 16-30</A
>, <A
HREF="special-chars.html#EX58"
>Example 3-4</A
>,
	      and <A
HREF="loops.html#FINDSTRING"
>Example 11-10</A
> for scripts using
	      <B
CLASS="COMMAND"
>find</B
>. Its <A
HREF="external.html#MANREF"
>manpage</A
> provides more detail
	      on this complex and powerful command.</P
></DD
><DT
><A
NAME="XARGSREF"
></A
><B
CLASS="COMMAND"
>xargs</B
></DT
><DD
><P
>A filter for feeding arguments to a command, and also
	      a tool for assembling the commands themselves. It breaks
	      a data stream into small enough chunks for filters and
	      commands to process.  Consider it as a powerful replacement
	      for <A
HREF="commandsub.html#BACKQUOTESREF"
>backquotes</A
>.
	      In situations where <A
HREF="commandsub.html#COMMANDSUBREF"
>command
	      substitution</A
> fails with a <SPAN
CLASS="ERRORNAME"
>too
	      many arguments</SPAN
> error,
	      substituting <B
CLASS="COMMAND"
>xargs</B
> often
	      works.
	        <A
NAME="AEN10465"
HREF="#FTN.AEN10465"
>[1]</A
>
	      Normally, <B
CLASS="COMMAND"
>xargs</B
> reads from
	      <TT
CLASS="FILENAME"
>stdin</TT
> or from a pipe, but it can also
	      be given the output of a file.</P
><P
>The default command for <B
CLASS="COMMAND"
>xargs</B
> is
	      <A
HREF="internal.html#ECHOREF"
>echo</A
>. This means that input
	      piped to <B
CLASS="COMMAND"
>xargs</B
> may have linefeeds and
	      other whitespace characters stripped out.</P
><P
>	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls -l</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>total 0
 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file1
 -rw-rw-r--    1 bozo  bozo         0 Jan 29 23:58 file2</TT
>
 
 
 
 <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls -l | xargs</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bozo 0 Jan...</TT
>
 
 
 
 <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>find ~/mail -type f | xargs grep "Linux"</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>./misc:User-Agent: slrn/0.9.8.1 (Linux)
 ./sent-mail-jul-2005: hosted by the Linux Documentation Project.
 ./sent-mail-jul-2005: (Linux Documentation Project Site, rtf version)
 ./sent-mail-jul-2005: Subject: Criticism of Bozo's Windows/Linux article
 ./sent-mail-jul-2005: while mentioning that the Linux ext2/ext3 filesystem
 . . .</TT
>
 	      </PRE
></TD
></TR
></TABLE
>
	      </P
><P
><TT
CLASS="USERINPUT"
><B
>ls | xargs -p -l gzip</B
></TT
> <A
HREF="filearchiv.html#GZIPREF"
>gzips</A
> every file in current
	      directory, one at a time, prompting before each
	      operation.</P
><P
><A
NAME="XARGSONEATATIME"
></A
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Note that <I
CLASS="FIRSTTERM"
>xargs</I
> processes the
	      arguments passed to it sequentially, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>one at
	      a time</I
></SPAN
>.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>find /usr/bin | xargs file</B
></TT
>
 <TT
CLASS="COMPUTEROUTPUT"
>/usr/bin:          directory
 /usr/bin/foomatic-ppd-options:          perl script text executable
 . . .</TT
>
 	      </PRE
></TD
></TR
></TABLE
>
	      </P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="XARGSLIMARGS"
></A
></P
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>An interesting <I
CLASS="FIRSTTERM"
>xargs</I
>
	      option is <TT
CLASS="OPTION"
>-n <TT
CLASS="REPLACEABLE"
><I
>NN</I
></TT
></TT
>,
	      which limits to <TT
CLASS="REPLACEABLE"
><I
>NN</I
></TT
> the number
	      of arguments passed.</P
><P
><TT
CLASS="USERINPUT"
><B
>ls | xargs -n 8 echo</B
></TT
> lists the files in the
	      current directory in <TT
CLASS="LITERAL"
>8</TT
> columns.</P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="XARGSWS"
></A
></P
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Another useful option is
	      <TT
CLASS="OPTION"
>-0</TT
>, in combination with <TT
CLASS="USERINPUT"
><B
>find
	      -print0</B
></TT
> or <TT
CLASS="USERINPUT"
><B
>grep -lZ</B
></TT
>. This
	      allows handling arguments containing whitespace or
	      quotes.</P
><P
>	    <TT
CLASS="USERINPUT"
><B
>find / -type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f</B
></TT
>
	    </P
><P
>	    <TT
CLASS="USERINPUT"
><B
>grep -rliwZ GUI / | xargs -0 rm -f</B
></TT
>
	    </P
><P
>Either of the above will remove any file containing <SPAN
CLASS="QUOTE"
>"GUI"</SPAN
>.
	      <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>(Thanks, S.C.)</I
></SPAN
></P
><P
>Or:
	      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;cat /proc/"$pid"/"$OPTION" | xargs -0 echo
   2&nbsp;#  Formats output:         ^^^^^^^^^^^^^^^
   3&nbsp;#  From Han Holl's fixup of "get-commandline.sh"
   4&nbsp;#+ script in "/dev and /proc" chapter.</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="XARGSMULTIPROCESS"
></A
></P
><P
>The <TT
CLASS="OPTION"
>-P</TT
> option to
		      <I
CLASS="FIRSTTERM"
>xargs</I
> permits running
		      processes in parallel. This speeds up execution
		      in a machine with a multicore CPU.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;ls *gif | xargs -t -n1 -P2 gif2png
   4&nbsp;# Converts all the gif images in current directory to png.
   5&nbsp;
   6&nbsp;# Options:
   7&nbsp;# =======
   8&nbsp;# -t    Print command to stderr.
   9&nbsp;# -n1   At most 1 argument per command line.
  10&nbsp;# -P2   Run up to 2 processes simultaneously.
  11&nbsp;
  12&nbsp;# Thank you, Roberto Polli, for the inspiration.</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX41"
></A
><P
><B
>Example 16-5. Logfile: Using <I
CLASS="FIRSTTERM"
>xargs</I
> to monitor system log</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Generates a log file in current directory
   4&nbsp;# from the tail end of /var/log/messages.
   5&nbsp;
   6&nbsp;# Note: /var/log/messages must be world readable
   7&nbsp;# if this script invoked by an ordinary user.
   8&nbsp;#         #root chmod 644 /var/log/messages
   9&nbsp;
  10&nbsp;LINES=5
  11&nbsp;
  12&nbsp;( date; uname -a ) &#62;&#62;logfile
  13&nbsp;# Time and machine name
  14&nbsp;echo ---------------------------------------------------------- &#62;&#62;logfile
  15&nbsp;tail -n $LINES /var/log/messages | xargs | fmt -s &#62;&#62;logfile
  16&nbsp;echo &#62;&#62;logfile
  17&nbsp;echo &#62;&#62;logfile
  18&nbsp;
  19&nbsp;exit 0
  20&nbsp;
  21&nbsp;#  Note:
  22&nbsp;#  ----
  23&nbsp;#  As Frank Wang points out,
  24&nbsp;#+ unmatched quotes (either single or double quotes) in the source file
  25&nbsp;#+ may give xargs indigestion.
  26&nbsp;#
  27&nbsp;#  He suggests the following substitution for line 15:
  28&nbsp;#  tail -n $LINES /var/log/messages | tr -d "\"'" | xargs | fmt -s &#62;&#62;logfile
  29&nbsp;
  30&nbsp;
  31&nbsp;
  32&nbsp;#  Exercise:
  33&nbsp;#  --------
  34&nbsp;#  Modify this script to track changes in /var/log/messages at intervals
  35&nbsp;#+ of 20 minutes.
  36&nbsp;#  Hint: Use the "watch" command. </PRE
></TD
></TR
></TABLE
><HR></DIV
><P
><A
NAME="XARGSCURLYREF"
></A
></P
><P
><A
HREF="moreadv.html#CURLYBRACKETSREF"
>As in
	      <B
CLASS="COMMAND"
>find</B
></A
>, a curly bracket
	      pair serves as a placeholder for replacement text.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX42"
></A
><P
><B
>Example 16-6. Copying files in current directory to another</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# copydir.sh
   3&nbsp;
   4&nbsp;#  Copy (verbose) all files in current directory ($PWD)
   5&nbsp;#+ to directory specified on command-line.
   6&nbsp;
   7&nbsp;E_NOARGS=85
   8&nbsp;
   9&nbsp;if [ -z "$1" ]   # Exit if no argument given.
  10&nbsp;then
  11&nbsp;  echo "Usage: `basename $0` directory-to-copy-to"
  12&nbsp;  exit $E_NOARGS
  13&nbsp;fi  
  14&nbsp;
  15&nbsp;ls . | xargs -i -t cp ./{} $1
  16&nbsp;#            ^^ ^^      ^^
  17&nbsp;#  -t is "verbose" (output command-line to stderr) option.
  18&nbsp;#  -i is "replace strings" option.
  19&nbsp;#  {} is a placeholder for output text.
  20&nbsp;#  This is similar to the use of a curly-bracket pair in "find."
  21&nbsp;#
  22&nbsp;#  List the files in current directory (ls .),
  23&nbsp;#+ pass the output of "ls" as arguments to "xargs" (-i -t options),
  24&nbsp;#+ then copy (cp) these arguments ({}) to new directory ($1).  
  25&nbsp;#
  26&nbsp;#  The net result is the exact equivalent of
  27&nbsp;#+   cp * $1
  28&nbsp;#+ unless any of the filenames has embedded "whitespace" characters.
  29&nbsp;
  30&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="KILLBYNAME"
></A
><P
><B
>Example 16-7. Killing processes by name</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# kill-byname.sh: Killing processes by name.
   3&nbsp;# Compare this script with kill-process.sh.
   4&nbsp;
   5&nbsp;#  For instance,
   6&nbsp;#+ try "./kill-byname.sh xterm" --
   7&nbsp;#+ and watch all the xterms on your desktop disappear.
   8&nbsp;
   9&nbsp;#  Warning:
  10&nbsp;#  -------
  11&nbsp;#  This is a fairly dangerous script.
  12&nbsp;#  Running it carelessly (especially as root)
  13&nbsp;#+ can cause data loss and other undesirable effects.
  14&nbsp;
  15&nbsp;E_BADARGS=66
  16&nbsp;
  17&nbsp;if test -z "$1"  # No command-line arg supplied?
  18&nbsp;then
  19&nbsp;  echo "Usage: `basename $0` Process(es)_to_kill"
  20&nbsp;  exit $E_BADARGS
  21&nbsp;fi
  22&nbsp;
  23&nbsp;
  24&nbsp;PROCESS_NAME="$1"
  25&nbsp;ps ax | grep "$PROCESS_NAME" | awk '{print $1}' | xargs -i kill {} 2&#38;&#62;/dev/null
  26&nbsp;#                                                       ^^      ^^
  27&nbsp;
  28&nbsp;# ---------------------------------------------------------------
  29&nbsp;# Notes:
  30&nbsp;# -i is the "replace strings" option to xargs.
  31&nbsp;# The curly brackets are the placeholder for the replacement.
  32&nbsp;# 2&#38;&#62;/dev/null suppresses unwanted error messages.
  33&nbsp;#
  34&nbsp;# Can  grep "$PROCESS_NAME" be replaced by pidof "$PROCESS_NAME"?
  35&nbsp;# ---------------------------------------------------------------
  36&nbsp;
  37&nbsp;exit $?
  38&nbsp;
  39&nbsp;#  The "killall" command has the same effect as this script,
  40&nbsp;#+ but using it is not quite as educational.</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="WF2"
></A
><P
><B
>Example 16-8. Word frequency analysis using
	      <I
CLASS="FIRSTTERM"
>xargs</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# wf2.sh: Crude word frequency analysis on a text file.
   3&nbsp;
   4&nbsp;# Uses 'xargs' to decompose lines of text into single words.
   5&nbsp;# Compare this example to the "wf.sh" script later on.
   6&nbsp;
   7&nbsp;
   8&nbsp;# Check for input file on command-line.
   9&nbsp;ARGS=1
  10&nbsp;E_BADARGS=85
  11&nbsp;E_NOFILE=86
  12&nbsp;
  13&nbsp;if [ $# -ne "$ARGS" ]
  14&nbsp;# Correct number of arguments passed to script?
  15&nbsp;then
  16&nbsp;  echo "Usage: `basename $0` filename"
  17&nbsp;  exit $E_BADARGS
  18&nbsp;fi
  19&nbsp;
  20&nbsp;if [ ! -f "$1" ]       # Does file exist?
  21&nbsp;then
  22&nbsp;  echo "File \"$1\" does not exist."
  23&nbsp;  exit $E_NOFILE
  24&nbsp;fi
  25&nbsp;
  26&nbsp;
  27&nbsp;
  28&nbsp;#####################################################
  29&nbsp;cat "$1" | xargs -n1 | \
  30&nbsp;#  List the file, one word per line. 
  31&nbsp;tr A-Z a-z | \
  32&nbsp;#  Shift characters to lowercase.
  33&nbsp;sed -e 's/\.//g'  -e 's/\,//g' -e 's/ /\
  34&nbsp;/g' | \
  35&nbsp;#  Filter out periods and commas, and
  36&nbsp;#+ change space between words to linefeed,
  37&nbsp;sort | uniq -c | sort -nr
  38&nbsp;#  Finally remove duplicates, prefix occurrence count
  39&nbsp;#+ and sort numerically.
  40&nbsp;#####################################################
  41&nbsp;
  42&nbsp;#  This does the same job as the "wf.sh" example,
  43&nbsp;#+ but a bit more ponderously, and it runs more slowly (why?).
  44&nbsp;
  45&nbsp;exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="EXPRREF"
></A
><TT
CLASS="USERINPUT"
><B
>expr</B
></TT
></DT
><DD
><P
>All-purpose expression evaluator:
	      Concatenates and evaluates the arguments according
	      to the operation given (arguments must be separated
	      by spaces). Operations may be arithmetic, comparison,
	      string, or logical.</P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="USERINPUT"
><B
>expr 3 + 5</B
></TT
></DT
><DD
><P
>returns <TT
CLASS="LITERAL"
>8</TT
></P
></DD
><DT
><TT
CLASS="USERINPUT"
><B
>expr 5 % 3</B
></TT
></DT
><DD
><P
>returns 2</P
></DD
><DT
><TT
CLASS="USERINPUT"
><B
>expr 1 / 0</B
></TT
></DT
><DD
><P
>returns the error message, <SPAN
CLASS="ERRORCODE"
>expr: division by
		    zero</SPAN
></P
><P
>Illegal arithmetic operations not allowed.</P
></DD
><DT
><TT
CLASS="USERINPUT"
><B
>expr 5 \* 3</B
></TT
></DT
><DD
><P
>returns 15</P
><P
>The multiplication operator
		  must be escaped when used in an arithmetic expression
		  with <B
CLASS="COMMAND"
>expr</B
>.</P
></DD
><DT
><TT
CLASS="USERINPUT"
><B
>y=`expr $y + 1`</B
></TT
></DT
><DD
><P
>Increment a variable, with the same effect
		    as <TT
CLASS="USERINPUT"
><B
>let y=y+1</B
></TT
> and
		    <TT
CLASS="USERINPUT"
><B
>y=$(($y+1))</B
></TT
>. This is an
		    example of <A
HREF="arithexp.html#ARITHEXPREF"
>arithmetic
		    expansion</A
>.</P
></DD
><DT
><A
NAME="EXPEXTRSUB"
></A
><TT
CLASS="USERINPUT"
><B
>z=`expr substr
		$string $position $length`</B
></TT
></DT
><DD
><P
>Extract substring of $length characters, starting
		    at $position.</P
></DD
></DL
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX45"
></A
><P
><B
>Example 16-9. Using <I
CLASS="FIRSTTERM"
>expr</I
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;# Demonstrating some of the uses of 'expr'
   4&nbsp;# =======================================
   5&nbsp;
   6&nbsp;echo
   7&nbsp;
   8&nbsp;# Arithmetic Operators
   9&nbsp;# ---------- ---------
  10&nbsp;
  11&nbsp;echo "Arithmetic Operators"
  12&nbsp;echo
  13&nbsp;a=`expr 5 + 3`
  14&nbsp;echo "5 + 3 = $a"
  15&nbsp;
  16&nbsp;a=`expr $a + 1`
  17&nbsp;echo
  18&nbsp;echo "a + 1 = $a"
  19&nbsp;echo "(incrementing a variable)"
  20&nbsp;
  21&nbsp;a=`expr 5 % 3`
  22&nbsp;# modulo
  23&nbsp;echo
  24&nbsp;echo "5 mod 3 = $a"
  25&nbsp;
  26&nbsp;echo
  27&nbsp;echo
  28&nbsp;
  29&nbsp;# Logical Operators
  30&nbsp;# ------- ---------
  31&nbsp;
  32&nbsp;#  Returns 1 if true, 0 if false,
  33&nbsp;#+ opposite of normal Bash convention.
  34&nbsp;
  35&nbsp;echo "Logical Operators"
  36&nbsp;echo
  37&nbsp;
  38&nbsp;x=24
  39&nbsp;y=25
  40&nbsp;b=`expr $x = $y`         # Test equality.
  41&nbsp;echo "b = $b"            # 0  ( $x -ne $y )
  42&nbsp;echo
  43&nbsp;
  44&nbsp;a=3
  45&nbsp;b=`expr $a \&#62; 10`
  46&nbsp;echo 'b=`expr $a \&#62; 10`, therefore...'
  47&nbsp;echo "If a &#62; 10, b = 0 (false)"
  48&nbsp;echo "b = $b"            # 0  ( 3 ! -gt 10 )
  49&nbsp;echo
  50&nbsp;
  51&nbsp;b=`expr $a \&#60; 10`
  52&nbsp;echo "If a &#60; 10, b = 1 (true)"
  53&nbsp;echo "b = $b"            # 1  ( 3 -lt 10 )
  54&nbsp;echo
  55&nbsp;# Note escaping of operators.
  56&nbsp;
  57&nbsp;b=`expr $a \&#60;= 3`
  58&nbsp;echo "If a &#60;= 3, b = 1 (true)"
  59&nbsp;echo "b = $b"            # 1  ( 3 -le 3 )
  60&nbsp;# There is also a "\&#62;=" operator (greater than or equal to).
  61&nbsp;
  62&nbsp;
  63&nbsp;echo
  64&nbsp;echo
  65&nbsp;
  66&nbsp;
  67&nbsp;
  68&nbsp;# String Operators
  69&nbsp;# ------ ---------
  70&nbsp;
  71&nbsp;echo "String Operators"
  72&nbsp;echo
  73&nbsp;
  74&nbsp;a=1234zipper43231
  75&nbsp;echo "The string being operated upon is \"$a\"."
  76&nbsp;
  77&nbsp;# length: length of string
  78&nbsp;b=`expr length $a`
  79&nbsp;echo "Length of \"$a\" is $b."
  80&nbsp;
  81&nbsp;# index: position of first character in substring
  82&nbsp;#        that matches a character in string
  83&nbsp;b=`expr index $a 23`
  84&nbsp;echo "Numerical position of first \"2\" in \"$a\" is \"$b\"."
  85&nbsp;
  86&nbsp;# substr: extract substring, starting position &#38; length specified
  87&nbsp;b=`expr substr $a 2 6`
  88&nbsp;echo "Substring of \"$a\", starting at position 2,\
  89&nbsp;and 6 chars long is \"$b\"."
  90&nbsp;
  91&nbsp;
  92&nbsp;#  The default behavior of the 'match' operations is to
  93&nbsp;#+ search for the specified match at the BEGINNING of the string.
  94&nbsp;#
  95&nbsp;#       Using Regular Expressions ...
  96&nbsp;b=`expr match "$a" '[0-9]*'`               #  Numerical count.
  97&nbsp;echo Number of digits at the beginning of \"$a\" is $b.
  98&nbsp;b=`expr match "$a" '\([0-9]*\)'`           #  Note that escaped parentheses
  99&nbsp;#                   ==      ==             #+ trigger substring match.
 100&nbsp;echo "The digits at the beginning of \"$a\" are \"$b\"."
 101&nbsp;
 102&nbsp;echo
 103&nbsp;
 104&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="IMPORTANT"
><TABLE
CLASS="IMPORTANT"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/important.png"
HSPACE="5"
ALT="Important"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The <A
HREF="special-chars.html#NULLREF"
>:
	      (<I
CLASS="FIRSTTERM"
>null</I
>)</A
> operator
	      can substitute for <B
CLASS="COMMAND"
>match</B
>. For example,
	      <TT
CLASS="USERINPUT"
><B
>b=`expr $a : [0-9]*`</B
></TT
> is the
	      exact equivalent of <TT
CLASS="USERINPUT"
><B
>b=`expr match $a
	      [0-9]*`</B
></TT
> in the above listing.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;
   3&nbsp;echo
   4&nbsp;echo "String operations using \"expr \$string : \" construct"
   5&nbsp;echo "==================================================="
   6&nbsp;echo
   7&nbsp;
   8&nbsp;a=1234zipper5FLIPPER43231
   9&nbsp;
  10&nbsp;echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"."
  11&nbsp;#     Escaped parentheses grouping operator.            ==  ==
  12&nbsp;
  13&nbsp;#       ***************************
  14&nbsp;#+          Escaped parentheses
  15&nbsp;#+           match a substring
  16&nbsp;#       ***************************
  17&nbsp;
  18&nbsp;
  19&nbsp;#  If no escaped parentheses ...
  20&nbsp;#+ then 'expr' converts the string operand to an integer.
  21&nbsp;
  22&nbsp;echo "Length of \"$a\" is `expr "$a" : '.*'`."   # Length of string
  23&nbsp;
  24&nbsp;echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`."
  25&nbsp;
  26&nbsp;# ------------------------------------------------------------------------- #
  27&nbsp;
  28&nbsp;echo
  29&nbsp;
  30&nbsp;echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`."
  31&nbsp;#                                                             ==      ==
  32&nbsp;echo "The first 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`."
  33&nbsp;#         =====                                          ==       ==
  34&nbsp;# Again, escaped parentheses force a substring match.
  35&nbsp;#
  36&nbsp;echo "The last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`."
  37&nbsp;#         ====                  end of string operator  ^^
  38&nbsp;#  (In fact, means skip over one or more of any characters until specified
  39&nbsp;#+  substring found.)
  40&nbsp;
  41&nbsp;echo
  42&nbsp;
  43&nbsp;exit 0</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
></DD
></DL
></DIV
><P
>The above script illustrates how
	      <B
CLASS="COMMAND"
>expr</B
> uses the <I
CLASS="FIRSTTERM"
>escaped
	      parentheses -- \( ... \) --</I
> grouping operator
	      in tandem with <A
HREF="regexp.html#REGEXREF"
>regular
	      expression</A
> parsing to match a substring.
	      Here is a another example, this time from <SPAN
CLASS="QUOTE"
>"real
	      life."</SPAN
>

	        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;# Strip the whitespace from the beginning and end.
   2&nbsp;LRFDATE=`expr "$LRFDATE" : '[[:space:]]*\(.*\)[[:space:]]*$'`
   3&nbsp;
   4&nbsp;#  From Peter Knowles' "booklistgen.sh" script
   5&nbsp;#+ for converting files to Sony Librie/PRS-50X format.
   6&nbsp;#  (http://booklistgensh.peterknowles.com)</PRE
></TD
></TR
></TABLE
>

	      </P
><P
><A
HREF="wrapper.html#PERLREF"
>Perl</A
>,
	      <A
HREF="sedawk.html#SEDREF"
>sed</A
>, and <A
HREF="awk.html#AWKREF"
>awk</A
> have far superior string
	      parsing facilities. A short <B
CLASS="COMMAND"
>sed</B
> or
	      <B
CLASS="COMMAND"
>awk</B
> <SPAN
CLASS="QUOTE"
>"subroutine"</SPAN
> within
	      a script (see <A
HREF="wrapper.html"
>Section 36.2</A
>) is an attractive
	      alternative to <B
CLASS="COMMAND"
>expr</B
>.</P
><P
>See <A
HREF="manipulatingvars.html#STRING-MANIPULATION"
>Section 10.1</A
> for more on
              using <B
CLASS="COMMAND"
>expr</B
> in string operations.</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN10465"
HREF="moreadv.html#AEN10465"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>And even when <I
CLASS="FIRSTTERM"
>xargs</I
> is
		not strictly necessary, it can speed up execution of a command
		involving <A
HREF="timedate.html#BATCHPROCREF"
>batch-processing</A
> of multiple
		files.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="external.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="timedate.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>External Filters, Programs and Commands</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="external.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Time / Date Commands</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>