-
Notifications
You must be signed in to change notification settings - Fork 24
/
6667489.html
1255 lines (1196 loc) · 63.6 KB
/
6667489.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>shell编程 if语句 - hainan16的专栏 - 博客频道 - CSDN.NET</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="description" content="if 语句格式if 条件then Commandelse Commandfi 别忘了这个结尾If语句忘了结尾fitest.sh: line 14: syntax e">
<script src="6667489_files/logAdvanced.js" async="" charset="utf-8"></script><script src="6667489_files/log.js" async="" charset="utf-8"></script><script src="6667489_files/ga.js" async="" type="text/javascript"></script><script src="6667489_files/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="6667489_files/ad.js"></script>
<link rel="Stylesheet" type="text/css" href="6667489_files/style.css">
<link id="RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="http://blog.csdn.net/hainan16/rss/list">
<link rel="shortcut icon" href="http://blog.csdn.net/favicon.ico">
<link type="text/css" rel="stylesheet" href="6667489_files/default.css">
<link href="6667489_files/style_002.css" rel="stylesheet" type="text/css"></head>
<body>
<script src="6667489_files/pub_topnav_2011.js" type="text/javascript"></script><link rel="stylesheet" type="text/css" href="6667489_files/navtop_2012.css">
<div class="nav_top_2011">
<p><span style="color:red;">您还未登录!</span>|<a href="https://passport.csdn.net/account/login" target="_top">登录</a>|<a href="https://passport.csdn.net/account/register" target="_top">注册</a>|<a href="https://passport.csdn.net/help/faq" target="_blank">帮助</a></p><ul>
<li><a href="http://www.csdn.net/" target="_blank" style="color: red;">首页</a></li>
<li><a href="http://news.csdn.net/" target="_blank">业界</a></li>
<li><a href="http://mobile.csdn.net/" target="_blank">移动</a></li>
<li><a href="http://cloud.csdn.net/" target="_blank">云计算</a></li>
<li><a href="http://sd.csdn.net/" target="_blank">研发</a></li>
<li><a href="http://bbs.csdn.net/" target="_blank">论坛</a></li>
<li><a href="http://blog.csdn.net/" target="_blank">博客</a></li>
<li><a href="http://download.csdn.net/" target="_blank">下载</a></li>
<li class="more"><h2 id="topnav_btnmore" onclick="showNavMore(this)"><a href="javascript:void(0);">更多</a></h2>
<div id="topnav_hidlist">
<iframe name="nav_frm" src="6667489_files/a_002.html" frameborder="no" height="178" scrolling="no" width="92"></iframe>
</div>
</li>
</ul>
</div>
<div id="container">
<div id="header">
<div class="header">
<div id="blog_title">
<h1><a href="http://blog.csdn.net/hainan16">hainan16的专栏</a></h1>
<h2></h2>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div>
<div id="navigator">
<div class="navigator_bg"></div>
<div class="navigator">
<ul>
<li id="btnContents"><a href="http://blog.csdn.net/hainan16?viewmode=contents"><span><img src="6667489_files/ico_list.gif">目录视图</span></a></li>
<li id="btnView"><a href="http://blog.csdn.net/hainan16?viewmode=list"><span><img src="6667489_files/ico_summary.gif">摘要视图</span></a></li>
<li id="btnRss"><a href="http://blog.csdn.net/hainan16/rss/list"><span><img src="6667489_files/ico_rss.gif">订阅</span></a></li>
</ul>
</div>
</div>
<script type="text/javascript">
var username = "hainan16";
var _blogger = username;
var blog_address = "http://blog.csdn.net/hainan16";
var static_host = "http://static.blog.csdn.net";
var currentUserName = "";
</script>
<div id="body">
<div id="main">
<div class="main">
<div class="notice">
<a href="http://surveies.csdn.net/survey/comein/590" target="_blank">
<font color="red">CSDN博客第三方发布工具有奖调查
</font></a>
<a href="http://blog.csdn.net/blogdevteam/article/details/9226601" target="_blank"><font color="blue">2013年7月微软MVP当选名单揭晓
</font></a>
<a href="http://blog.csdn.net/csdnproduct/article/details/9226265" target="_blank">
<font color="red">公告:CSDN博客频道新功能正式上线!
</font></a>
<a href="http://blog.csdn.net/blogdevteam/article/details/9136613" target="_blank">
<font color="red">CSDN博客第二期云计算最佳博主评选
</font></a>
<a href="http://blog.csdn.net/blogdevteam/article/details/9126517" target="_blank">
<font color="blue">2013年10月微软MVP申请开始
</font></a>
</div>
<div id="article_details" class="details">
<div class="article_title">
<span class="ico ico_type_Original"></span>
<h3>
<span class="link_title"><a href="http://blog.csdn.net/hainan16/article/details/6667489">
shell编程 if语句
</a></span>
</h3>
</div>
<div class="article_manage">
<span class="link_categories">
分类:
<a href="http://blog.csdn.net/hainan16/article/category/861739">shell 编程</a>
</span>
<span class="link_postdate">2011-08-07 20:03</span>
<span class="link_view" title="阅读次数">7089人阅读</span>
<span class="link_comments" title="评论次数"><a href="#comments">评论</a>(2)</span>
<span class="link_collect"><a href="javascript:void(0);" onclick="javascript:collectArticle('shell编程 if语句','6667489');return false;" title="收藏">收藏</a></span>
<span class="link_report"><a href="#report" onclick="javascript:report(6667489,2);return false;" title="举报">举报</a></span>
</div>
<div class="tag2box"><a href="http://blog.csdn.net/tag/details.html?tag=shell" target="_blank">shell</a><a href="http://blog.csdn.net/tag/details.html?tag=%e7%bc%96%e7%a8%8b" target="_blank">编程</a><a href="http://blog.csdn.net/tag/details.html?tag=integer" target="_blank">integer</a><a href="http://blog.csdn.net/tag/details.html?tag=file" target="_blank">file</a><a href="http://blog.csdn.net/tag/details.html?tag=string" target="_blank">string</a><a href="http://blog.csdn.net/tag/details.html?tag=input" target="_blank">input</a></div>
<div id="article_content" class="article_content">
<span style="font-weight:bold"><span style="font-weight:bold"></span>if 语句格式</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if <wbr> 条件<br>
then<br>
<wbr>Command<br>
else<br>
<wbr>Command<br>
<span style="font-weight:bold">fi <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 别忘了这个结尾</span><br>
</td>
</tr>
<tr>
<td>If语句忘了结尾fi<br>
test.sh: line 14: syntax error: <span style="font-weight:bold">unexpected end of fi</span></td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">if 的三种条件表达式</span><br>
<table style="width:100%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if<br>
command<br>
then<br>
<br>
if<br>
<wbr>函数<br>
then<br>
</td>
<td> <wbr>命令执行成功,等于返回0 (比如grep ,找到匹配)<br>
执行失败,返回非0 (grep,没找到匹配)</td>
</tr>
<tr>
<td>if [ expression_r_r_r <wbr> ]<br>
then <wbr><br>
</td>
<td> <wbr>表达式结果为真,则返回0,if把0值引向then</td>
</tr>
<tr>
<td>if test expression_r_r_r<br>
then</td>
<td> <wbr>表达式结果为假,则返回非0,if把非0值引向then</td>
</tr>
</tbody>
</table>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr><br>
<wbr> <wbr> <span style="font-weight:bold">[ ] && <wbr> ——快捷if</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors"</td>
</tr>
<tr>
<td> <wbr> <wbr> <wbr><span style="font-weight:bold">&& 可以理解为then</span><br>
<wbr> <wbr> <wbr> 如果左边的表达式为真则执行右边的语句<br>
</td>
</tr>
</tbody>
</table>
<wbr><br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">shell的if与c语言if的功能上的区别</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td> <wbr>shell if</td>
<td> <wbr> <wbr> <wbr> c语言if</td>
</tr>
<tr>
<td><span style="font-weight:bold">0为真</span>,走then</td>
<td> <wbr>正好相反,非0走then</td>
</tr>
<tr>
<td> <wbr>不支持整数变量直接if<br>
必须:if [ i –ne 0 ]<br>
<br>
但支持字符串变量直接if<br>
if [ str ] 如果字符串非0<br>
</td>
<td> <wbr>支持变量直接if<br>
if (i )</td>
</tr>
</tbody>
</table>
<br>
<wbr><br>
=================================以command作为if 条件===================================<br>
<wbr> <wbr><br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">以多条command或者函数作为if 条件</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>echo –n “input:”<br>
read user<br>
<br>
if<br>
多条指令,这些命令之间相当于<span style="font-weight:bold">“and”(与)</span><br>
grep $user /etc/passwd >/tmp/null <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
who -u | grep $user<br>
then <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 上边的指令<span style="color:#ff00; font-weight:bold">都</span>执行成功,返回值$?为0,<span style="color:#ff00; font-weight:bold">0为真</span>,运行then<br>
<wbr>echo "$user has logged"<br>
else <wbr> <wbr> <wbr> <wbr> 指令执行失败,$?为1,运行else <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
<wbr>echo "$user has not logged"<br>
fi <wbr> <wbr> <wbr><br>
</td>
</tr>
<tr>
<td># sh test.sh<br>
input : macg<br>
macg <wbr> <wbr> <wbr> <wbr> pts/0 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> May 15 15:55 <wbr> <wbr> . <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 2075 (192.168.1.100)<br>
macg has logged<br>
<wbr> <wbr> <wbr><br>
# sh test.sh<br>
input : ddd<br>
ddd has not logged <wbr> <wbr><br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">以函数作为if条件 <wbr> (函数就相当于command,函数的优点是其return值可以自定义)</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if<br>
以函数作为if条件,<br>
getyn<br>
then <wbr> <wbr> 函数reture值<span style="font-weight:bold">0为真</span>,走then<br>
echo " your answer is yes"<br>
else <wbr> 函数return值非0为假,走else<br>
echo "your anser is no"<br>
fi <wbr> <wbr><br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr> <wbr> <span style="font-weight:bold">if command <wbr> 等价于 command+if $?</span><br>
<table style="width:100%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>$ vi testsh.sh<br>
#!/bin/sh<br>
<br>
<span style="font-weight:bold">if</span><br style="font-weight:bold">
<span style="font-weight:bold">cat 111-tmp.txt | grep ting1</span><br style="font-weight:bold">
<span style="font-weight:bold">then</span><br>
echo found<br>
else<br>
echo "no found"<br>
fi</td>
<td> <wbr>$ vi testsh.sh<br>
#!/bin/sh<br>
<br>
<span style="font-weight:bold">cat 111-tmp.txt | grep ting1</span><br>
<br>
<span style="font-weight:bold">if [ $? -eq 0 ]</span><br>
<span style="font-weight:bold">then</span><br>
<span style="font-weight:bold">echo $?</span><br>
echo found<br>
else<br>
<span style="font-weight:bold">echo $?</span><br>
echo "no found"<br>
fi</td>
</tr>
<tr>
<td>$ sh testsh.sh<br>
no found <wbr> <wbr><br>
</td>
<td>$ sh testsh.sh<br>
1<br>
no found</td>
</tr>
<tr>
<td style="vertical-align:top">$ vi 111-tmp.txt<br>
that is 222file<br>
thisting1 is 111file<br>
<br>
$ sh testsh.sh<br>
thisting1 is 111file<br>
found</td>
<td style="vertical-align:top">$ vi 111-tmp.txt<br>
that is 222file<br>
thisting1 is 111file<br>
<br>
$ sh testsh.sh<br>
thisting1 is 111file<br>
0<br>
found</td>
</tr>
</tbody>
</table>
<br>
<wbr> <wbr> <wbr><br>
========================================以条件表达式作为 if条件=============================<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">传统if 从句子——以条件表达式作为 if条件</span><br>
if [ 条件表达式 ]<br>
then<br>
<wbr>command<br>
<wbr>command<br>
<wbr>command<br>
else<br>
<wbr>command<br>
<wbr>command<br>
fi<br>
<wbr> <wbr> <wbr><br>
<span style="font-weight:bold"> <wbr> <wbr> 条件表达式</span><br>
<ul>
<li>文件表达式 </li></ul>
<span style="font-weight:bold">if [ -f <wbr> file ] <wbr> <wbr> <wbr> 如果文件存在</span><br style="font-weight:bold">
<span style="font-weight:bold">if [ -d ... <wbr> <wbr> ] <wbr> <wbr> <wbr> 如果目录存在</span><br style="font-weight:bold">
<span style="font-weight:bold">if [ -s file <wbr> ] <wbr> <wbr> <wbr> 如果文件存在且非空 <wbr></span><br>
if [ -r file <wbr> ] <wbr> <wbr> <wbr> 如果文件存在且可读<br>
if [ -w file <wbr> ] <wbr> <wbr> 如果文件存在且可写<br>
if [ -x file <wbr> ] <wbr> <wbr> 如果文件存在且可执行 <wbr> <wbr><br>
<ul>
<li>整数变量表达式 </li></ul>
<span style="font-weight:bold">if [ int1 -eq int2 ] <wbr> <wbr> <wbr> 如果int1等于int2 <wbr> <wbr> <wbr></span><br style="font-weight:bold">
<span style="font-weight:bold">if [ int1 -ne int2 ] <wbr> <wbr> <wbr> 如果不等于 <wbr> <wbr></span> <wbr><br>
if [ int1 -ge int2 ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果>=<br>
if [ int1 -gt int2 ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果><br>
if [ int1 -le int2 ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果<=<br>
if [ int1 -lt int2 ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果<<br>
<wbr> <wbr> <wbr><br>
<ul>
<li> <wbr> <wbr> 字符串变量表达式 </li></ul>
If <wbr> [ $a = $b ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果string1等于string2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><span style="font-weight:bold">字符串允许使用赋值号做等号</span><br>
if <wbr> [ $string1 != <wbr> $string2 ] <wbr> <wbr> 如果string1不等于string2 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
<span style="font-weight:bold">if <wbr> [ -n $string <wbr> ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果string 非空(非0),返回0(true)</span> <wbr><br>
if <wbr> [ -z $string <wbr> ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果string 为空<br>
<span style="font-weight:bold">if <wbr> [ $sting ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果string 非空,返回0 (和-n类似) <wbr></span> <wbr> <wbr><br>
<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">条件表达式引用变量要带$</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if [ a = b ] ;then <wbr> <wbr> <wbr> <wbr><br>
echo equal<br>
else<br>
echo no equal<br>
fi<br>
</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input a:<br>
5<br>
input b:<br>
5<br>
<span style="font-weight:bold">no equal <wbr> (等于表达式没比较$a和$b,而是比较和a和b,自然a!=b)</span></td>
</tr>
</tbody>
</table>
改正:<br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if [ $a = $b ] ;then <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
echo equal<br>
else<br>
echo no equal<br>
fi<br>
</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input a:<br>
5<br>
input b:<br>
5<br>
equal</td>
</tr>
</tbody>
</table>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
<wbr> <wbr><span style="font-weight:bold">-eq <wbr> -ne <wbr> -lt <wbr> -nt只能用于整数,不适用于字符串,字符串等于用赋值号=</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ vi test.sh<br>
echo -n "input your choice:"<br>
read var<br>
if <wbr> [ $var -eq "yes" ]<br>
then<br>
echo $var<br>
fi<br>
[macg@machome ~]$ sh -x test.sh<br>
input your choice:<br>
y<br>
<span style="font-weight:bold">test.sh: line 3: test: y: integer expression_r_r_r expected</span><br style="font-weight:bold">
<span style="font-weight:bold"> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 期望整数形式,即-eq不支持字符串</span></td>
</tr>
</tbody>
</table>
<br>
<br>
<br>
<wbr> <wbr><span style="font-weight:bold"> <wbr> =放在别的地方是赋值,放在if [ ] 里就是字符串等于,<span style="color:#ff00">shell里面没有==的,那是c语言的等于</span></span><br>
<br>
<br>
<wbr> <wbr> <span style="font-weight:bold">无空格的字符串,可以加" ",也可以不加</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ vi test.sh<br>
echo "input a:"<br>
read a<br>
echo "input is $a"<br>
if [ $a = 123 ] ; then<br>
echo equal123<br>
fi</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input a:<br>
123<br>
input is 123<br>
equal123 <wbr><br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr><span style="font-weight:bold"> <wbr> <wbr> = 作为等于时,其两边都必须加空格,否则失效</span><br>
等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)<br>
<table style="width:100%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ vi test.sh<br>
<br>
echo "input your choice:"<br>
read var<br>
if [ <span style="font-weight:bold">$var="yes"</span> ]<br>
then<br>
echo $var<br>
echo "input is correct"<br>
else<br>
echo $var<br>
echo "input error"<br>
fi</td>
<td>[macg@machome ~]$ vi test.sh<br>
<br>
echo "input your choice:"<br>
read var<br>
if [ <span style="font-weight:bold">$var = "yes"</span> ] <wbr> <wbr> 在等号两边加空格<br>
then<br>
echo $var<br>
echo "input is correct"<br>
else<br>
echo $var<br>
echo "input error"<br>
fi</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
y<br>
y<br>
input is correct<br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
n <wbr> <wbr> <wbr> <wbr><br>
n<br>
input is correct <wbr><br>
输错了也走then,都走then,为什么?<br>
<span style="color:#ff00; font-weight:bold">因为if把$var="yes"连读成一个变量,而此变量为空,返回1,则走else</span></td>
<td> <wbr>[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
y<br>
y<br>
input error<br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
no <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
no<br>
input error<br>
一切正常<br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr> <wbr> <wbr> <wbr><span style="font-weight:bold">If <wbr> [ <wbr> $ANS <wbr> ] <wbr> <wbr> <wbr> <wbr> 等价于 <wbr> if [ -n $ANS ]</span><br>
<wbr> <wbr> <wbr> <wbr> <wbr> 如果字符串变量非空(then) , 空(else)<br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>echo "input your choice:"<br>
read ANS<br>
<br>
if [ $ANS ]<br>
then<br>
echo no empty<br>
else<br>
echo empth<br>
fi <wbr><br>
</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input your choice: <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 回车<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
empth <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><span style="font-weight:bold">说明“回车”就是空串</span><br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
34<br>
no empty <wbr><br>
</td>
</tr>
</tbody>
</table>
<br>
<wbr><br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">整数条件表达式,大于,小于</span>,s<span style="font-weight:bold">hell里没有> 和< ,会被当作尖括号,只有-ge,-gt,-le,lt</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ vi test.sh<br>
<br>
echo "input a:"<br>
read a<br>
if <wbr> [ $a -ge 100 ] ; then<br>
echo 3bit<br>
else<br>
echo 2bit<br>
fi<br>
</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input a:<br>
123<br>
3bit<br>
[macg@machome ~]$ sh test.sh<br>
input a:<br>
20<br>
2bit</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr><span style="font-weight:bold">整数操作符号-ge,-gt,-le,-lt, 别忘了加-</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>if <wbr> test $a <wbr> <span style="font-weight:bold">ge</span> 100 ; then<br>
<br>
[macg@machome ~]$ sh test.sh<br>
test.sh: line 4: test: ge: binary operator expected<br>
</td>
</tr>
<tr>
<td>if <wbr> test $a -ge 100 ; then<br>
<br>
[macg@machome ~]$ sh test.sh<br>
input a:<br>
123<br>
3bit<br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
============================逻辑表达式=========================================<br>
<span style="font-weight:bold"><br>
</span><span style="font-weight:bold"> <wbr> <wbr> <wbr> 逻辑非 ! <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 条件表达式的相反</span><br>
if [ ! 表达式 ]<br>
if [ ! -d $num ] <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果不存在目录$num<br>
<br>
<br>
<span style="font-weight:bold"> <wbr> <wbr> <wbr> 逻辑与 –a</span> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><span style="font-weight:bold">条件表达式的并列</span><br>
if [ 表达式1 <wbr> –a <wbr> 表达式2 ]<br>
<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">逻辑或 -o <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></span> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><span style="font-weight:bold">条件表达式的或</span><br>
if [ 表达式1 <wbr> –o 表达式2 ]<br>
<br>
<wbr> <wbr> <span style="font-weight:bold"><br>
<wbr> <wbr> 逻辑表达式</span><br>
<ul>
<li> <wbr> <wbr> <wbr> 表达式与前面的= <wbr> != -d –f –x -ne -eq -lt等合用 </li></ul>
<ul>
<li> <wbr> <wbr> <wbr> 逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列 </li></ul>
if [ -z "$JHHOME" -a -d $HOME/$num ]<br>
<ul>
<li> <wbr> <wbr> <wbr> 注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了 </li></ul>
<br>
<br>
<wbr> <wbr><span style="font-weight:bold">最常见的赋值形式,赋值前对=两边的变量都进行评测</span><br>
左边测变量是否为空,右边测目录(值)是否存在(值是否有效)<br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td> <wbr></td>
</tr>
<tr>
<td>[macg@mac-home ~]$ vi test.sh<br>
:<br>
echo "input the num:"<br>
read num<br>
echo "input is $num"<br>
<br>
<span style="font-weight:bold">if [ -z "$JHHOME" -a -d $HOME/$num ] <wbr> <wbr></span> 如果变量$JHHOME为空,且$HOME/$num目录存在<br>
<span style="font-weight:bold">then</span><br style="font-weight:bold">
<span style="font-weight:bold">JHHOME=$HOME/$num <wbr> <wbr> <wbr></span> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 则赋值<br>
fi<br>
<br>
echo "JHHOME is $JHHOME" <wbr> <wbr><br>
</td>
</tr>
<tr>
<td style="vertical-align:top">-----------------------<br>
[macg@mac-home ~]$ sh test.sh<br>
input the num:<br>
ppp<br>
input is ppp<br>
JHHOME is<br>
<br>
目录<span style="font-weight:bold">-d $HOME/$num <wbr> <wbr></span> 不存在,所以$JHHOME没被then赋值</td>
</tr>
<tr>
<td style="vertical-align:top">[macg@mac-home ~]$ mkdir ppp<br>
[macg@mac-home ~]$ sh test.sh<br>
input the num:<br>
ppp<br>
input is ppp<br>
JHHOME is /home/macg/ppp</td>
</tr>
</tbody>
</table>
<br>
<br>
<wbr><span style="font-weight:bold"> <wbr> <wbr> 一个-o的例子,其中却揭示了”=”必须两边留空格的问题</span><br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>echo "input your choice:"<br>
read ANS<br>
<br>
if [ <span style="font-weight:bold">$ANS="Yes"</span> -o $ANS="yes" -o $ANS="y" -o $ANS="Y" ]<br>
then<br>
ANS="y"<br>
else<br>
ANS="n"<br>
fi<br>
<br>
echo $ANS</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
n<br>
y<br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
no<br>
y<br>
为什么输入不是yes,结果仍是y(走then)<br>
因为=被连读了,成了变量<span style="font-weight:bold">$ANS="Yes",而变量又为空,所以走else了</span></td>
</tr>
</tbody>
</table>
<br>
<table style="width:80%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ vi test.sh<br>
<br>
echo "input your choice:"<br>
read ANS <wbr> <wbr> <wbr> echo "input your choice:"<br>
read ANS<br>
<br>
if [ <span style="font-weight:bold">$ANS = "Yes"</span> -o $ANS = "yes" -o $ANS = "y" -o $ANS = "Y" ]<br>
then<br>
ANS="y"<br>
else<br>
ANS="n"<br>
fi<br>
<br>
echo $ANS<br>
</td>
</tr>
<tr>
<td>[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
no<br>
n<br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
yes<br>
y<br>
[macg@machome ~]$ sh test.sh<br>
input your choice:<br>
y<br>
y</td>
</tr>
</tbody>
</table>
<br>
<br>
===================以 <wbr> test 条件表达式作为if条件===================================<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">if test $num -eq 0 <wbr> <wbr> <wbr> <wbr> <wbr> 等价于 <wbr> <wbr> if [ $num –eq 0 ]</span><br>
<br>
<wbr> <wbr><span style="font-weight:bold"> <wbr> test <wbr> 表达式,没有 [ <wbr> ]</span><br>
if test $num -eq 0 <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr><br>
then<br>
echo "try again"<br>
else<br>
echo "good"<br>
fi<br>
<br>
<span style="font-weight:bold"> <wbr> <wbr> <wbr> man test</span><br>
<table style="width:100%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td>[macg@machome ~]$ man test<br>
[(1) <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> User Commands <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
[(1)<br>
<br>
SYNOPSIS<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> test EXPRESSION<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> [ EXPRESSION ]<br>
<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> [-n] STRING<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> the length of STRING is nonzero <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -n和直接$str都是非0条件<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -z STRING<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> the length of STRING is zero<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> STRING1 = STRING2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> the strings are equal<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> STRING1 != STRING2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> the strings are not equal<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -eq INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is equal to INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -ge INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is greater than or equal to INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -gt INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is greater than INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -le INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is less than or equal to INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -lt INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is less than INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 -ne INTEGER2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> INTEGER1 is not equal to INTEGER2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE1 -nt FILE2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE1 is newer (modification date) than FILE2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE1 -ot FILE2<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE1 is older than FILE2<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -b FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is block special<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -c FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is character special<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -d FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a directory<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -e FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr>
文件存在<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -f FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a regular file <wbr> <wbr> <wbr> <wbr> 文件存在且是普通文件<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -h FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a symbolic link (same as -L)<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -L FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a symbolic link (same as -h)<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -G FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is owned by the effective group ID<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -O FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is owned by the effective user ID<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -p FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a named pipe<br>
<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -s FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and has a size greater than zero<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -S FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is a socket<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -w FILE<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> FILE exists and is writable<br>
<br>
<wbr> <wbr> <wbr> <wbr> <wbr> <wbr> -x FILE<br>
FILE exists and is executable<br>
<wbr><br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
======================if简化语句=================================<br>
<br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">最常用的简化if语句</span><br>
<table style="width:100%" border="1" cellpadding="3" cellspacing="1">
<tbody>
<tr>
<td> <wbr> <wbr> <span style="font-weight:bold">&& 如果是“前面”,则“后面”</span><br>
[ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid <wbr> <wbr> <wbr> 检查 文件是否存在,如果存在就删掉<br>
</td>
</tr>
<tr>
<td> <wbr> <wbr> <span style="font-weight:bold">|| <wbr> <wbr> 如果不是“前面”,则后面</span><br>
[ -f /usr/sbin/dhcpd ] || exit 0 <wbr> <wbr> <wbr> 检验文件是否存在,如果存在就退出<br>
</td>
</tr>
</tbody>
</table>
<wbr><br>
<wbr> <wbr> <wbr> <span style="font-weight:bold">用简化 if 和$1,$2,$3来检测参数,不合理就调用help</span><br>
<span style="font-weight:bold">[ -z "$1" ] && help <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果第一个参数不存在(-z <wbr> 字符串长度为0 )</span><br>
[ "$1" = "-h" ] && help <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> 如果第一个参数是-h,就显示help<br>
<br>
<br>
<span style="font-weight:bold">例子</span><br>
#!/bin/sh<br>
<br>
[ -f "/etc/sysconfig/network-scripts/ifcfg-eth0" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0<br>
cp ifcfg-eth0.bridge /etc/sysconfig/network-scripts/ifcfg-eth0<br>
<br>
[ -f "/etc/sysconfig/network-scripts/ifcfg-eth1" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth1<br>
cp ifcfg-eth1.bridge /etc/sysconfig/network-scripts/ifcfg-eth1<br>
<br>
[ -f "/etc/sysconfig/network-scripts/ifcfg-eth0:1" ] && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:1<br>
<br>
</div>
<div class="share_buttons" id="sharePanel"><span>分享到:</span> <a name="sina" class="share_sina" title="分享到新浪微博"></a><a name="qq" class="share_qq" title="分享到腾讯微博"></a></div>
<!--192.168.100.34-->
<div class="article_next_prev">
<li class="prev_article"><span>上一篇:</span><a href="http://blog.csdn.net/hainan16/article/details/6667487">shell编程 case语句</a></li>
<li class="next_article"><span>下一篇:</span><a href="http://blog.csdn.net/hainan16/article/details/6705917">linux 下安装qt</a></li>
</div>
</div>
<div id="ad_cen">
<script type="text/javascript">BAIDU_CLB_SLOT_ID = "117306";</script>
<script type="text/javascript" src="6667489_files/o.js"></script><script charset="utf-8" src="6667489_files/ecom.js"></script><div id="baidu_clb_slot_117306"><iframe id="baidu_clb_slot_iframe_117306" src="6667489_files/a_002.html" onload="BAIDU_CLB_renderFrame('117306')" vspace="0" hspace="0" allowtransparency="true" marginheight="0" marginwidth="0" style="border: 0; vertical-align: bottom; margin: 0; display: block;" frameborder="0" height="90" scrolling="no" width="728"></iframe></div>
</div>
<script type="text/javascript">
//new Ad(4, 'ad_cen');
</script>
<div id="comment_title" class="panel_head">查看评论<a name="comments"></a></div>
<div id="comment_list"><dl class="comment_item comment_topic" id="comment_item_2538227"><dt class="comment_head" floor="2">2楼 <span class="user"><a class="username" href="http://blog.csdn.net/SSSUUNNYY" target="_blank">SSSUUNNYY</a> <span class="ptime">2013-02-26 10:47发表</span> <a href="#reply" class="cmt_btn reply" title="回复">[回复]</a> <span class="comment_manage" style="display:none;" commentid="2538227" username="SSSUUNNYY"> <a href="#quote" class="cmt_btn quote" title="引用">[引用]</a> <a href="#report" class="cmt_btn report" title="举报">[举报]</a></span></span></dt><dd class="comment_userface"><a href="http://blog.csdn.net/SSSUUNNYY" target="_blank"><img src="6667489_files/3_sssuunnyy.gif" height="40" width="40"></a></dd><dd class="comment_body">顶</dd></dl><dl class="comment_item comment_topic" id="comment_item_2513471"><dt class="comment_head" floor="1">1楼 <span class="user"><a class="username" href="http://blog.csdn.net/wangwei5281528" target="_blank">wangwei5281528</a> <span class="ptime">2013-01-23 13:32发表</span> <a href="#reply" class="cmt_btn reply" title="回复">[回复]</a> <span class="comment_manage" style="display:none;" commentid="2513471" username="wangwei5281528"> <a href="#quote" class="cmt_btn quote" title="引用">[引用]</a> <a href="#report" class="cmt_btn report" title="举报">[举报]</a></span></span></dt><dd class="comment_userface"><a href="http://blog.csdn.net/wangwei5281528" target="_blank"><img src="6667489_files/3_wangwei5281528.jpg" height="40" width="40"></a></dd><dd class="comment_body">支持一下!!</dd></dl><div class="clear"></div></div>
<div style="display: none;" id="comment_bar"></div>
<div id="comment_form"><div class="guest_link">您还没有登录,请<a href="javascript:void(0);" onclick="javascript:csdn.showLogin(function (dat) {js_logined(dat.data.userName);});">[登录]</a>或<a href="http://passport.csdn.net/account/register?from=http%3A%2F%2Fblog.csdn.net%2Fhainan16%2Farticle%2Fdetails%2F6667489">[注册]</a></div></div>
<div class="announce">* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场<a name="reply"></a><a name="quote"></a></div>
<script type="text/javascript">
var fileName = '6667489';
var commentscount = 2;
var islock = false
</script>
<script type="text/javascript" src="6667489_files/comment.js"></script>
<div id="ad_bot"><iframe src="6667489_files/ad.html" style="border-width: 0px; overflow: hidden; width: 732px; height: 170px;" id="ad_frm_0" frameborder="0" scrolling="no"></iframe></div>
<script type="text/javascript">
new Ad(5, 'ad_bot');
</script>
<div id="report_dialog"></div>
<div id="d-top" style="">
<a id="d-top-a" href="#" title="回到顶部">
<img src="6667489_files/top.png" alt="TOP"></a>
</div>
<script type="text/javascript">
$(function(){
var d_top=$('#d-top');
document.onscroll=function(){
var scrTop=(document.body.scrollTop||document.documentElement.scrollTop);
if(scrTop>500){
d_top.show();
}else{
d_top.hide();
}
}
$('#d-top-a').click(function(){
scrollTo(0,0);
this.blur();
return false;
});
});
</script>
<div class="clear"></div>
</div>
</div>
<div id="side">
<div class="side">
<div id="panel_Profile" class="panel">
<ul class="panel_head"><span>个人资料</span></ul>
<ul class="panel_body profile">
<div id="blog_userface">
<a href="http://my.csdn.net/hainan16" target="_blank">
<img src="6667489_files/1_hainan16.gif" title="访问我的空间" style="max-width:90%">
</a>
<br>
<span><a href="http://my.csdn.net/hainan16" class="user_name" target="_blank">hainan16</a></span>
</div>
<div class="interact">
<a href="javascript:void(0);" class="attent" id="span_add_follow" title="[加关注]"></a>
<a href="javascript:void(0);" class="letter" onclick="loginto(1)" title="[发私信]"></a>
</div>
<div id="blog_medal">
<div id="bms_box"></div></div>
<ul id="blog_rank">
<li>访问:<span>33308次</span></li>
<li>积分:<span>807分</span></li>
<li>排名:<span>第14132名</span></li>
</ul>
<ul id="blog_statistics">
<li>原创:<span>48篇</span></li>
<li>转载:<span>16篇</span></li>
<li>译文:<span>0篇</span></li>
<li>评论:<span>16条</span></li>
</ul>
</ul>
</div>
<div class="panel" id="panel_Search">
<ul class="panel_head"><span>文章搜索</span></ul>
<ul class="panel_body">
<form id="frmSearch" action="http://so.csdn.net/search" class="form_search" target="_blank">
<span><input id="inputSearch" class="blogsearch" title="请输入关键字" type="text"></span>
<input id="btnSubmit" value="搜索" title="search in blog" type="submit">
<input name="q" id="inputQ" type="hidden">
<input name="t" value="blog" type="hidden">
<a id="btnSearchBlog" target="_blank"></a>
</form>