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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Tests</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="Basics"
HREF="part2.html"><LINK
REL="PREVIOUS"
TITLE="Exit and Exit Status"
HREF="exit-status.html"><LINK
REL="NEXT"
TITLE="File test operators"
HREF="fto.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="CHAPTER"
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="exit-status.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="fto.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="TESTS"
></A
>Chapter 7. Tests</H1
><P
><A
NAME="IFTHEN"
></A
></P
><P
>Every reasonably complete programming language can test
for a condition, then act according to the result of the
test. Bash has the <A
HREF="tests.html#TTESTREF"
>test</A
>
command, various <A
HREF="tests.html#DBLBRACKETS"
>bracket</A
>
and <A
HREF="tests.html#DBLPARENSTST"
>parenthesis</A
> operators,
and the <B
CLASS="COMMAND"
>if/then</B
> construct.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="TESTCONSTRUCTS"
></A
>7.1. Test Constructs</H1
><P
><A
NAME="TESTCONSTRUCTS1"
></A
></P
><UL
><LI
><P
>An <B
CLASS="COMMAND"
>if/then</B
> construct tests whether the
<A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
> of a list
of commands is <SPAN
CLASS="RETURNVALUE"
>0</SPAN
> (since 0 means
<SPAN
CLASS="QUOTE"
>"success"</SPAN
> by UNIX convention), and if so, executes
one or more commands.</P
></LI
><LI
><P
>There exists a dedicated command called <B
CLASS="COMMAND"
> [</B
> (<A
HREF="special-chars.html#LEFTBRACKET"
>left bracket</A
>
special character). It is a synonym for <B
CLASS="COMMAND"
>test</B
>,
and a <A
HREF="internal.html#BUILTINREF"
>builtin</A
> for efficiency
reasons. This command considers its arguments as comparison
expressions or file tests and returns an exit status corresponding
to the result of the comparison (0 for true, 1 for false).</P
></LI
><LI
><P
>With version 2.02, Bash introduced the <A
HREF="tests.html#DBLBRACKETS"
>[[ ... ]]</A
> <I
CLASS="FIRSTTERM"
>extended
test command</I
>, which performs comparisons
in a manner more familiar to programmers from other
languages. Note that <B
CLASS="COMMAND"
>[[</B
> is a <A
HREF="internal.html#KEYWORDREF"
>keyword</A
>, not a command.</P
><P
>Bash sees <TT
CLASS="USERINPUT"
><B
>[[ $a -lt $b ]]</B
></TT
> as a
single element, which returns an exit status.</P
></LI
><LI
><P
><A
NAME="DBLPARENSTST"
></A
></P
><P
>The <A
HREF="dblparens.html"
>(( ... ))</A
> and <A
HREF="internal.html#LETREF"
>let ...</A
> constructs return an
<A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
>,
<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>according to whether the arithmetic expressions they
evaluate expand to a non-zero value</I
></SPAN
>. These
<A
HREF="arithexp.html#ARITHEXPREF"
>arithmetic-expansion</A
>
constructs may therefore be used to perform <A
HREF="comparison-ops.html#ICOMPARISON1"
>arithmetic comparisons</A
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 (( 0 && 1 )) # Logical AND
2 echo $? # 1 ***
3 # And so ...
4 let "num = (( 0 && 1 ))"
5 echo $num # 0
6 # But ...
7 let "num = (( 0 && 1 ))"
8 echo $? # 1 ***
9
10
11 (( 200 || 11 )) # Logical OR
12 echo $? # 0 ***
13 # ...
14 let "num = (( 200 || 11 ))"
15 echo $num # 1
16 let "num = (( 200 || 11 ))"
17 echo $? # 0 ***
18
19
20 (( 200 | 11 )) # Bitwise OR
21 echo $? # 0 ***
22 # ...
23 let "num = (( 200 | 11 ))"
24 echo $num # 203
25 let "num = (( 200 | 11 ))"
26 echo $? # 0 ***
27
28 # The "let" construct returns the same exit status
29 #+ as the double-parentheses arithmetic expansion.</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="ARXS"
></A
>Again, note that the
<I
CLASS="FIRSTTERM"
>exit status</I
> of an arithmetic expression
is <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> an error value.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 var=-2 && (( var+=2 ))
2 echo $? # 1
3
4 var=-2 && (( var+=2 )) && echo $var
5 # Will not echo $var!</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
></LI
><LI
><P
><A
NAME="IFGREPREF"
></A
></P
><P
>An <B
CLASS="COMMAND"
>if</B
> can test any command, not just
conditions enclosed within brackets.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if cmp a b &> /dev/null # Suppress output.
2 then echo "Files a and b are identical."
3 else echo "Files a and b differ."
4 fi
5
6 # The very useful "if-grep" construct:
7 # -----------------------------------
8 if grep -q Bash file
9 then echo "File contains at least one occurrence of Bash."
10 fi
11
12 word=Linux
13 letter_sequence=inu
14 if echo "$word" | grep -q "$letter_sequence"
15 # The "-q" option to grep suppresses output.
16 then
17 echo "$letter_sequence found in $word"
18 else
19 echo "$letter_sequence not found in $word"
20 fi
21
22
23 if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED
24 then echo "Command succeeded."
25 else echo "Command failed."
26 fi</PRE
></TD
></TR
></TABLE
>
</P
></LI
><LI
><P
><SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>These last two examples
courtesy of Stphane Chazelas.</I
></SPAN
></P
></LI
></UL
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX10"
></A
><P
><B
>Example 7-1. What is truth?</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # Tip:
4 # If you're unsure how a certain condition might evaluate,
5 #+ test it in an if-test.
6
7 echo
8
9 echo "Testing \"0\""
10 if [ 0 ] # zero
11 then
12 echo "0 is true."
13 else # Or else ...
14 echo "0 is false."
15 fi # 0 is true.
16
17 echo
18
19 echo "Testing \"1\""
20 if [ 1 ] # one
21 then
22 echo "1 is true."
23 else
24 echo "1 is false."
25 fi # 1 is true.
26
27 echo
28
29 echo "Testing \"-1\""
30 if [ -1 ] # minus one
31 then
32 echo "-1 is true."
33 else
34 echo "-1 is false."
35 fi # -1 is true.
36
37 echo
38
39 echo "Testing \"NULL\""
40 if [ ] # NULL (empty condition)
41 then
42 echo "NULL is true."
43 else
44 echo "NULL is false."
45 fi # NULL is false.
46
47 echo
48
49 echo "Testing \"xyz\""
50 if [ xyz ] # string
51 then
52 echo "Random string is true."
53 else
54 echo "Random string is false."
55 fi # Random string is true.
56
57 echo
58
59 echo "Testing \"\$xyz\""
60 if [ $xyz ] # Tests if $xyz is null, but...
61 # it's only an uninitialized variable.
62 then
63 echo "Uninitialized variable is true."
64 else
65 echo "Uninitialized variable is false."
66 fi # Uninitialized variable is false.
67
68 echo
69
70 echo "Testing \"-n \$xyz\""
71 if [ -n "$xyz" ] # More pedantically correct.
72 then
73 echo "Uninitialized variable is true."
74 else
75 echo "Uninitialized variable is false."
76 fi # Uninitialized variable is false.
77
78 echo
79
80
81 xyz= # Initialized, but set to null value.
82
83 echo "Testing \"-n \$xyz\""
84 if [ -n "$xyz" ]
85 then
86 echo "Null variable is true."
87 else
88 echo "Null variable is false."
89 fi # Null variable is false.
90
91
92 echo
93
94
95 # When is "false" true?
96
97 echo "Testing \"false\""
98 if [ "false" ] # It seems that "false" is just a string ...
99 then
100 echo "\"false\" is true." #+ and it tests true.
101 else
102 echo "\"false\" is false."
103 fi # "false" is true.
104
105 echo
106
107 echo "Testing \"\$false\"" # Again, uninitialized variable.
108 if [ "$false" ]
109 then
110 echo "\"\$false\" is true."
111 else
112 echo "\"\$false\" is false."
113 fi # "$false" is false.
114 # Now, we get the expected result.
115
116 # What would happen if we tested the uninitialized variable "$true"?
117
118 echo
119
120 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="FORMALPARA"
><P
><B
>Exercise. </B
>Explain the behavior of <A
HREF="tests.html#EX10"
>Example 7-1</A
>, above.</P
></DIV
><P
><A
NAME="ELSEREF"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if [ condition-true ]
2 then
3 command 1
4 command 2
5 ...
6 else # Or else ...
7 # Adds default code block executing if original condition tests false.
8 command 3
9 command 4
10 ...
11 fi</PRE
></TD
></TR
></TABLE
>
</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
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
>When <I
CLASS="FIRSTTERM"
>if</I
> and <I
CLASS="FIRSTTERM"
>then</I
>
are on same line in a condition test, a semicolon must
terminate the <I
CLASS="FIRSTTERM"
>if</I
> statement. Both
<I
CLASS="FIRSTTERM"
>if</I
> and <I
CLASS="FIRSTTERM"
>then</I
>
are <A
HREF="internal.html#KEYWORDREF"
>keywords</A
>. Keywords (or
commands) begin statements, and before a new statement on the
same line begins, the old one must terminate.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if [ -x "$filename" ]; then</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="ELIFREF1"
></A
>Else if and elif</B
></P
><DL
><DT
><SPAN
CLASS="TOKEN"
>elif</SPAN
></DT
><DD
><P
><TT
CLASS="USERINPUT"
><B
>elif</B
></TT
> is a contraction
for <I
CLASS="FIRSTTERM"
>else if</I
>. The effect is to nest an
inner <SPAN
CLASS="TOKEN"
>if/then</SPAN
> construct within an outer
one.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 if [ condition1 ]
2 then
3 command1
4 command2
5 command3
6 elif [ condition2 ]
7 # Same as else if
8 then
9 command4
10 command5
11 else
12 default-command
13 fi</PRE
></TD
></TR
></TABLE
>
</P
></DD
></DL
></DIV
><P
>
<A
NAME="IFREF2"
></A
>
The <TT
CLASS="USERINPUT"
><B
>if test condition-true</B
></TT
> construct is the
exact equivalent of <TT
CLASS="USERINPUT"
><B
>if [ condition-true ]</B
></TT
>.
As it happens, the left bracket, <B
CLASS="COMMAND"
>[</B
> , is a
<I
CLASS="FIRSTTERM"
>token</I
>
<A
NAME="AEN3140"
HREF="#FTN.AEN3140"
>[1]</A
>
which invokes the <B
CLASS="COMMAND"
>test</B
> command. The closing
right bracket, <B
CLASS="COMMAND"
>]</B
> , in an if/test should not
therefore be strictly necessary, however newer versions of Bash
require it.</P
><P
><A
NAME="TTESTREF"
></A
></P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
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 <B
CLASS="COMMAND"
>test</B
> command is a Bash <A
HREF="internal.html#BUILTINREF"
>builtin</A
> which tests file
types and compares strings. Therefore, in a Bash script,
<B
CLASS="COMMAND"
>test</B
> does <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> call
the external <TT
CLASS="FILENAME"
>/usr/bin/test</TT
> binary,
which is part of the <I
CLASS="FIRSTTERM"
>sh-utils</I
>
package. Likewise, <B
CLASS="COMMAND"
>[</B
> does not call
<TT
CLASS="FILENAME"
>/usr/bin/[</TT
>, which is linked to
<TT
CLASS="FILENAME"
>/usr/bin/test</TT
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>type test</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>test is a shell builtin</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>type '['</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>[ is a shell builtin</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>type '[['</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>[[ is a shell keyword</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>type ']]'</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>]] is a shell keyword</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>type ']'</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>bash: type: ]: not found</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="USRBINTEST"
></A
></P
><P
>If, for some reason, you wish to use
<TT
CLASS="FILENAME"
>/usr/bin/test</TT
> in a Bash script,
then specify it by full pathname.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX11"
></A
><P
><B
>Example 7-2. Equivalence of <I
CLASS="FIRSTTERM"
>test</I
>,
<TT
CLASS="FILENAME"
>/usr/bin/test</TT
>, <SPAN
CLASS="TOKEN"
>[ ]</SPAN
>,
and <TT
CLASS="FILENAME"
>/usr/bin/[</TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 echo
4
5 if test -z "$1"
6 then
7 echo "No command-line arguments."
8 else
9 echo "First command-line argument is $1."
10 fi
11
12 echo
13
14 if /usr/bin/test -z "$1" # Equivalent to "test" builtin.
15 # ^^^^^^^^^^^^^ # Specifying full pathname.
16 then
17 echo "No command-line arguments."
18 else
19 echo "First command-line argument is $1."
20 fi
21
22 echo
23
24 if [ -z "$1" ] # Functionally identical to above code blocks.
25 # if [ -z "$1" should work, but...
26 #+ Bash responds to a missing close-bracket with an error message.
27 then
28 echo "No command-line arguments."
29 else
30 echo "First command-line argument is $1."
31 fi
32
33 echo
34
35
36 if /usr/bin/[ -z "$1" ] # Again, functionally identical to above.
37 # if /usr/bin/[ -z "$1" # Works, but gives an error message.
38 # # Note:
39 # This has been fixed in Bash, version 3.x.
40 then
41 echo "No command-line arguments."
42 else
43 echo "First command-line argument is $1."
44 fi
45
46 echo
47
48 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN3206"
></A
><P
><A
NAME="DBLBRACKETS"
></A
>The <SPAN
CLASS="TOKEN"
>[[ ]]</SPAN
> construct
is the more versatile Bash version of <SPAN
CLASS="TOKEN"
>[ ]</SPAN
>. This
is the <I
CLASS="FIRSTTERM"
>extended test command</I
>, adopted from
<I
CLASS="FIRSTTERM"
>ksh88</I
>.</P
><P
>* * *</P
><P
>No filename expansion or word splitting takes place
between <SPAN
CLASS="TOKEN"
>[[</SPAN
> and <SPAN
CLASS="TOKEN"
>]]</SPAN
>, but there is
parameter expansion and command substitution.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 file=/etc/passwd
2
3 if [[ -e $file ]]
4 then
5 echo "Password file exists."
6 fi</PRE
></TD
></TR
></TABLE
>
</P
><P
>Using the <B
CLASS="COMMAND"
>[[ ... ]]</B
> test construct,
rather than <B
CLASS="COMMAND"
>[ ... ]</B
> can prevent many
logic errors in scripts. For example, the <SPAN
CLASS="TOKEN"
>&&</SPAN
>,
<SPAN
CLASS="TOKEN"
>||</SPAN
>, <SPAN
CLASS="TOKEN"
><</SPAN
>, and <SPAN
CLASS="TOKEN"
>></SPAN
>
operators work within a <SPAN
CLASS="TOKEN"
>[[ ]]</SPAN
> test, despite
giving an error within a <SPAN
CLASS="TOKEN"
>[ ]</SPAN
> construct.</P
><P
><A
NAME="DBLBRAEV"
></A
></P
><P
><I
CLASS="FIRSTTERM"
>Arithmetic evaluation</I
> of octal /
hexadecimal constants takes place automatically within a
<SPAN
CLASS="TOKEN"
>[[ ... ]]</SPAN
> construct.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # [[ Octal and hexadecimal evaluation ]]
2 # Thank you, Moritz Gronbach, for pointing this out.
3
4
5 decimal=15
6 octal=017 # = 15 (decimal)
7 hex=0x0f # = 15 (decimal)
8
9 if [ "$decimal" -eq "$octal" ]
10 then
11 echo "$decimal equals $octal"
12 else
13 echo "$decimal is not equal to $octal" # 15 is not equal to 017
14 fi # Doesn't evaluate within [ single brackets ]!
15
16
17 if [[ "$decimal" -eq "$octal" ]]
18 then
19 echo "$decimal equals $octal" # 15 equals 017
20 else
21 echo "$decimal is not equal to $octal"
22 fi # Evaluates within [[ double brackets ]]!
23
24 if [[ "$decimal" -eq "$hex" ]]
25 then
26 echo "$decimal equals $hex" # 15 equals 0x0f
27 else
28 echo "$decimal is not equal to $hex"
29 fi # [[ $hexadecimal ]] also evaluates!</PRE
></TD
></TR
></TABLE
>
</P
></DIV
></TD
></TR
></TABLE
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
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
>Following an <B
CLASS="COMMAND"
>if</B
>, neither the
<B
CLASS="COMMAND"
>test</B
> command nor the test brackets ( [ ] or [[ ]] )
are strictly necessary.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 dir=/home/bozo
2
3 if cd "$dir" 2>/dev/null; then # "2>/dev/null" hides error message.
4 echo "Now in $dir."
5 else
6 echo "Can't change to $dir."
7 fi</PRE
></TD
></TR
></TABLE
>
The "if COMMAND" construct returns the exit status of COMMAND.</P
><P
>Similarly, a condition within test brackets may stand alone
without an <B
CLASS="COMMAND"
>if</B
>, when used in combination
with a <A
HREF="list-cons.html#LISTCONSREF"
>list construct</A
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 var1=20
2 var2=22
3 [ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
4
5 home=/home/bozo
6 [ -d "$home" ] || echo "$home directory does not exist."</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
><P
><A
NAME="DBLPRX"
></A
>The <A
HREF="dblparens.html"
>(( ))
construct</A
> expands and evaluates an arithmetic
expression. If the expression evaluates as zero, it returns
an <A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
> of
<SPAN
CLASS="RETURNVALUE"
>1</SPAN
>, or <SPAN
CLASS="QUOTE"
>"false"</SPAN
>. A non-zero
expression returns an exit status of <SPAN
CLASS="RETURNVALUE"
>0</SPAN
>,
or <SPAN
CLASS="QUOTE"
>"true"</SPAN
>. This is in marked contrast to using
the <B
CLASS="COMMAND"
>test</B
> and <SPAN
CLASS="TOKEN"
>[ ]</SPAN
> constructs
previously discussed.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="ARITHTESTS"
></A
><P
><B
>Example 7-3. Arithmetic Tests using <SPAN
CLASS="TOKEN"
>(( ))</SPAN
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # arith-tests.sh
3 # Arithmetic tests.
4
5 # The (( ... )) construct evaluates and tests numerical expressions.
6 # Exit status opposite from [ ... ] construct!
7
8 (( 0 ))
9 echo "Exit status of \"(( 0 ))\" is $?." # 1
10
11 (( 1 ))
12 echo "Exit status of \"(( 1 ))\" is $?." # 0
13
14 (( 5 > 4 )) # true
15 echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
16
17 (( 5 > 9 )) # false
18 echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
19
20 (( 5 == 5 )) # true
21 echo "Exit status of \"(( 5 == 5 ))\" is $?." # 0
22 # (( 5 = 5 )) gives an error message.
23
24 (( 5 - 5 )) # 0
25 echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
26
27 (( 5 / 4 )) # Division o.k.
28 echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
29
30 (( 1 / 2 )) # Division result < 1.
31 echo "Exit status of \"(( 1 / 2 ))\" is $?." # Rounded off to 0.
32 # 1
33
34 (( 1 / 0 )) 2>/dev/null # Illegal division by 0.
35 # ^^^^^^^^^^^
36 echo "Exit status of \"(( 1 / 0 ))\" is $?." # 1
37
38 # What effect does the "2>/dev/null" have?
39 # What would happen if it were removed?
40 # Try removing it, then rerunning the script.
41
42 # ======================================= #
43
44 # (( ... )) also useful in an if-then test.
45
46 var1=5
47 var2=4
48
49 if (( var1 > var2 ))
50 then #^ ^ Note: Not $var1, $var2. Why?
51 echo "$var1 is greater than $var2"
52 fi # 5 is greater than 4
53
54 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN3140"
HREF="tests.html#AEN3140"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><A
NAME="TOKENREF"
></A
>A
<I
CLASS="FIRSTTERM"
>token</I
> is a symbol or short
string with a special meaning attached to it (a <A
HREF="regexp.html#METAMEANINGREF"
>meta-meaning</A
>). In Bash,
certain tokens, such as <B
CLASS="COMMAND"
>[</B
> and <A
HREF="special-chars.html#DOTREF"
>. (dot-command)</A
>, may expand to
<I
CLASS="FIRSTTERM"
>keywords</I
> and commands.</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="exit-status.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="fto.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Exit and Exit Status</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part2.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>File test operators</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|