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
- 20.5 shell脚本中的逻辑判断
- 20.6 文件目录属性判断
- 20.7 if特殊用法
- 20.8/20.9 case判断
# 20.5 Shell脚本中的逻辑判断
- 很多脚本可以直接用命令执行,比如之前的那个
```
[root@aming-01 ~]# for i in `seq 1 5`;do echo $i;done
1
2
3
4
5
[root@aming-01 ~]# for i in `seq 1 5`
do
echo $i
done
1
2
3
4
5
[root@aming-01 ~]
```
- 格式1:if 条件 ; then 语句; fi
- 下面来写个脚本
- a=5 ,如果a 大于3 就 打印 ok
- 如果写成一行就 下面这样
```
[root@aming-01 ~]# a=5
[root@aming-01 ~]# if [ $a -gt 3 ]
then
echo ok
fi
ok
[root@aming-01 ~]
 
[root@aming-01 ~]# if [ $a -gt 3 ];then echo ok; fi
ok
[root@aming-01 ~]
 
```
- 用脚本的形式编辑下,就是
```
[root@aming-01 shell]# vi if1.sh
 
#!/bin/bash
a=5
if [ $a -gt 3 ]
then 
    echo ok 
fi
~                                                                                         
~                                                                                         
~                                                                                         
                                                                                        
~                                                                                         
:wq
 
[root@aming-01 shell]# sh if1.sh
ok
[root@aming-01 shell]
 
```
- 格式2:if 条件; then 语句; else 语句; fi
- 如果满足条件怎么样,不满足又会怎么样
```
[root@aming-01 shell]# cp if1.sh if2.sh
[root@aming-01 shell]# vi if2.sh
 
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
    echo ok
else
    echo not ok
fi
                                                                                         
~                                                                                         
~                                                                                         
~                                                                                         
:wq
 
[root@aming-01 shell]# sh -x if2.sh
+ a=1
'[' 1 -gt 3 ']'
echo not ok
not ok
[root@aming-01 shell]
 
```
- 格式3:if …; then … ;elif …; then …; else …; fi
- -gt 指的是大于  -lt指的是小于
```
[root@aming-01 shell]# cp if2.sh if3.sh
[root@aming-01 shell]# vi if3.sh
 
#!/bin/bash
a=5
if [ $a -gt 1 ]
then
    echo ">1"
elif [$a -lt 6 ]
then 
    echo "<6 && >1" 
else
    echo not ok
fi
~                                                                                         
                                                                                        
~                                                                                         
~                                                                                         
:wq
[root@aming-01 shell]# vi if3.sh
 
[root@aming-01 shell]# cat if3.sh
#!/bin/bash
a=5
if [ $a -gt 1 ]
then
    echo ">1"
elif [$a -lt 6 ]
then 
    echo "<6 && >1" 
else
    echo not ok
fi
[root@aming-01 shell]
 
[root@aming-01 shell]# sh -x if3.sh
+ a=5
'[' 5 -gt 1 ']'
echo '>1'
>1
[root@aming-01 shell]
```
- 改下
```
[root@aming-01 shell]# vi if3.sh
 
#!/bin/bash
a=5
if [ $a -gt 4 ]
then
    echo ">1"
elif [ $a -lt 6 ]
then
    echo "<6 && >1"
else
    echo not ok
fi
~                                                                                         
                                                                                       
~                                                                                         
:wq
[root@aming-01 shell]# sh -x if3.sh
+ a=5
'[' 5 -gt 4 ']'
echo '>1'
>1
[root@aming-01 shell]
```
if 判断的三种格式
```
[root@aming-01 shell]# if ...;then ...; fi^C
[root@aming-01 shell]# if ...;then ...;else ...;fi^C
[root@aming-01 shell]# if ...;thn ...;elif ...then ...;else ...;fi^C
[root@aming-01 shell]
```
- 逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
 
-    gt 大于        lt 小于         eq 等于
-   -ge 大于等于   -le小于等于     -ne 不等于
```
[root@aming-01 shell]# gt lt eq   -ne -ge -le ^C
```
- 可以使用 && || 结合多个条件         &&并且   ||或者
-  if [ $a -gt 5 ] && [ $a -lt 10 ]; then
-  if [ $b -gt 5 ] || [ $b -lt 3 ]; then
 
 
 
 
 
 
 
 
 
 
# 20.6 文件目录属性判断
- shell中 我们通常和文件、目录打交道,所以对文件目录的判断很重要
if 判断文件、目录属性
- [ -f file ]判断是否是普通文件,且存在
- 举例
```
Last login: Tue Nov 21 19:49:55 2017
[root@aming-01 ~]# vi file1.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
    echo $f exist
else 
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                         
~                                                                                         
:wq
[root@aming-01 ~]# vi file1.sh
[root@aming-01 ~]# sh -x file1.sh
+ f=/tmp/aminglinux
'[' -f /tmp/aminglinux ']'
touch /tmp/aminglinux
[root@aming-01 ~]
 
```
- 解释:首先f回去判断这个文件到底存在与否,很明显不存在,于是就创建了一个/tmp/aminglinux 文件
- 那我们再次执行这个脚本,很明显这个文件已经存在了,所以echo  /tmp/aminglinux  exist
```
[root@aming-01 ~]# sh -x file1.sh
+ f=/tmp/aminglinux
'[' -f /tmp/aminglinux ']'
echo /tmp/aminglinux exist
/tmp/aminglinux exist
[root@aming-01 ~]
```
- 这里把这个脚本移到shell/目录下面来,以为脚本文件我们统一放在/shell 目录里
```
[root@aming-01 ~]# ls
aming.txt  anaconda-ks.cfg  file1.sh  shell  zabbix-release-3.2-1.el7.noarch.rpm
[root@aming-01 ~]# cd shell/
[root@aming-01 shell]# mv /root/file1.sh .
[root@aming-01 shell]# ls
01.sh  file1.sh  if1.sh  if2.sh  if3.sh
```
-  [ -d file ] 判断是否是目录,且存在
```
[root@aming-01 shell]# cp file1.sh file2.sh
[root@aming-01 shell]# vi file2.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then
    echo $f exist
else
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                        
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh -x file2.sh
+ f=/tmp/aminglinux
'[' -d /tmp/aminglinux ']'
touch /tmp/aminglinux
[root@aming-01 shell]#
```
- 很明显他不适目录,不适目录就touch 一个目录
 
 
- 还有一种情况,我不管它是文件还是目录,我仅仅是想知道这个文件或者目录到底存在不存在
- [ -e file ] 判断文件或目录,是否存在
```
[root@aming-01 shell]# vi file2.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ]
then
    echo $f exist
else
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                        
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh -x file2.sh
+ f=/tmp/aminglinux
'[' -e /tmp/aminglinux ']'
echo /tmp/aminglinux exist
/tmp/aminglinux exist
[root@aming-01 shell]
```
- 判断这个目录或者文件是否存在,不存在就创建一个目录或者文件
- 如果这个文件存在touch会是摸一下 文件或者目录,改变文件和目录的 三个time值 Atime  Ctime  Mtime
 
-  [ -r file ] 判断文件是否可读
 
```
[root@aming-01 shell]# vi file2.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
    echo $f readable
fi
~                                                                                         
                                                                                          
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh
 
[root@aming-01 shell]# sh file2.sh
/tmp/aminglinux readable
[root@aming-01 shell]
```
- 显示这个文件是可读的
  
- [ -w file ] 判断文件是否可写
```
[root@aming-01 shell]# vi file2.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then
    echo $f writeable
fi
~                                                                                         
~                                                                                         
~                                                                                         
                                                                                         
~                                                                                         
:wq
 
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# !sh
sh file2.sh
/tmp/aminglinux writeable
[root@aming-01 shell]
 
```
- 文件可写
- 可读可写可执行是针对当前用户root 来说的
```
[root@aming-01 shell]# ls -l /tmp/aminglinux
-rw-r--r-- 1 root root 0 11月 21 20:09 /tmp/aminglinux
[root@aming-01 shell]
```
-  [ -x file ] 判断文件是否可执行
```
[root@aming-01 shell]# vi file2.sh
 
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
    echo $f exeable
fi
~                                                                                         
                                                                                         
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh file2.sh
[root@aming-01 shell]
```
- 因为它不可执行,所以没有任何的输出
- 因为我们根本就没有定义else
```
[root@aming-01 shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
    echo $f exeable
fi
[root@aming-01 shell]
```
- 这样用的比较多
- 常用案例
并且
```
f="/tmp/aminglinux"
[ -f $f ] && rm -f $f     //前一条命令执行成功才会继续执行之后的命令
 
等同于下面的表达方式
if [ -f $f ]     
then
      rm -rf $f
fi
```
 
- 或者
```
f="/tmp/aminglinux"
[ -f $f ] || touch $f    //前面命令不成功时,执行后面的命令
if [ ! -f $f ]        //  “!”表示了如果这条命令不成功,就往下执行 (!表示取反)
then
      touch $f
fi
```
 
 
 
 
 
 
 
 
# 20.7 if特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
```
[root@aming-01 shell]# vi if4.sh
 
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ $n -gt 100 ]
then
    echo alsdflljk
fi
                                                                                        
~                                                                                         
:wq
[root@aming-01 shell]# vi if4.sh
[root@aming-01 shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc/tmp/lalal: 没有那个文件或目录
+ n=
'[' -gt 100 ']'
if4.sh: 第 3 行:[: -gt: 期待一元表达式
[root@aming-01 shell]#
```
- 当变量没有值的时候 会报错,为了让这个脚本更加严谨,应该做一些判断
if [ -z "$a" ] 这里表示当变量a的值为空时 就echo error 并且 exit
```
[root@aming-01 shell]# vi if4.sh
 
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
    echo error
    exit
elif  [ $n -gt 100 ]
then
    echo alsdflljk
fi
~                                                                                         
                                                                                          
:wq
 
 
[root@aming-01 shell]# vi if4.sh
[root@aming-01 shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc/tmp/lalal: 没有那个文件或目录
+ n=
'[' -z '' ']'
echo error
error
exit
[root@aming-01 shell]
 
```
- 再改进一下,这样它就不再报错了
```
[root@aming-01 shell]# !vi
vi if4.sh
 
#!/bin/bash
if [ ! -f /tmp/lalal ]
then
   echo "/tmp/lalal not exist."
   exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
    echo error
    exit
elif  [ $n -gt 100 ]
then
    echo alsdflljk
fi
~                                                                                         
                                                                                         
~                                                                                         
:wq
 
[root@aming-01 shell]# sh if4.sh
/tmp/lalal not exist.
[root@aming-01 shell]
 
```
-  if [ -n "$a" ] 表示当变量a的值不为空
-  用变量的时候 要用双引号引起来,如果是一个文件,那就不用了
-  - 这个 [ -n "$a" ]  是可以判断文件的,判断一个文件的内容是否不为空,条件成立
```
[root@aming-01 shell]# ls
01.sh  file1.sh  file2.sh  if1.sh  if2.sh  if3.sh  if4.sh
[root@aming-01 shell]# if [ -n 01.sh ]; then echo ok;fi
ok
[root@aming-01 shell]
```
- 同样可以判断变量
- 解释:当变量b 不为空,那么echo $b, 否则 当变量b为空,那么echo b is null,这里变量b 是为空的
- -n 和 -z 用法,如果不是变量,不需要双引号“”;因为这两个是针对文件执行的;
```
[root@aming-01 shell]# echo $b
 
[root@aming-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
[root@aming-01 shell]
```
- 比如判断系统的用户里面是否有user1这个用户
```
[root@aming-01 ~]# useradd user1 
[root@aming-01 ~]# cd shell
[root@aming-01 shell]# grep -w 'user1' /etc/passwd
user1:x:1011:1011::/home/user1:/bin/bash
[root@aming-01 shell]
```
- 表示如果/etc/passwd中含有'user1'的字符 时会怎么样
-  -w 更加精准的匹配,只匹配‘’内的单词
-  如果/etc/passwd 里包含user1 ,那么就说 user1 exist  user1 存在
```
[root@aming-01 shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";fi
user1:x:1011:1011::/home/user1:/bin/bash
user1 exist
[root@aming-01 shell]
```
- 只不过这个判断的过程 是会输出 过滤的语句出来,把这个结果给输出出来,grep -q 仅仅是做一个过滤,但是不会吧过滤的内容显示出来
```
[root@aming-01 shell]# if grep -wq 'user1' /etc/passwd;then echo "user1 exist";fi
user1 exist
[root@aming-01 shell]
```
- 反过来怎么表示呢,如果这个user1不存在
- 如果user1 不存在,加个! 表示取反的意思,那么useradd user1
- 只不过这个操作过程不成立的,因为user1本身就存在了 后面这个命令不生效的then "useradd user1"
```
[root@aming-01 shell]# if ! grep -wq 'user1' /etc/passwd;then "useradd user1";fi
[root@aming-01 shell]
```
-  if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then
 
 [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
 
 
 
 
 
 
 
# 20.8 case判断(上)
```
- 格式 case  变量名 in 
                     value1)
                          command
                          ;;
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac
``` 
- 在case程序中,可以在条件中使用|,表示或的意思, 比如 
```
2|3) 
    command
    ;;
```
- shell脚本案例:
```
[root@aming-01 shell]# vi case.sh
 
 
                                                                                        
~                                                                                         
"case.sh" [New File]
```
- 加入以下脚本
```
[root@aming-01 shell]# vi case.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
 
then
    echo "Please input a number."
    exit 1
fi
 
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
fi
 
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else 
    tag=0
fi
 
case $tag in
    1)
        echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac
 
```
- 脚本命令解释
read -p "Please input a number: " n
if [ -z "$n" ]   
read -p 的作用,n作为你要捕获一个变量的名字,用户输入什么数值,最终这个n 这个变量就会赋值什么东西
```
[root@aming-01 shell]# read -p "Please input a number: " n
Please input a number: 
Please input a number; kdkdk
[root@aming-01 shell]# echo $n
kdkdk
[root@aming-01 shell]
[root@aming-01 shell]# echo $n
123
[root@aming-01 shell]
```
if [ -z "$n" ] 表示当用户没有输入东西的时候  为空的时候  if [ -z "$n" ] 表示为空的时候,说明用户没有输入,那奥告诉他 echo "Please input a number." 请输入一个数字
- 这里的 exit 1  ,这个1 表示 当一个命令一个语句运行完之后 都会有一个返回的值,echo $? 输出结果为0 表示命令 语句是正确的,如果是1 那就是错误 这里的exit 1 就是那个echo $?的输出的值
 
-  n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" 
- 这里的变量判断的时你输入的字符串 是不是纯数字,万一输入的时1a1 或者 纯字母aaa呢,都需要去判断下,如果不是那就要做一个操作,把里面的数字做一个清空
- 当你的变量不为空时,if [ -n "$n1" ] 表示$n1不为空时,继续提示让你输入一个纯数字
```
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
```
 
 
 
 
 
 
 
 
 
# 20.9 case判断(下)
```
fi
 
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
```
- 当你输入的时数字的时候,数字满足[ $n -ge 0 ] 大于等于0  并且 [ $n -lt 60 ]小于60,那么作为标记1  tag1 是个变量
 
- 如果不是上述情况
```
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
```
- [ $n -ge 60 ]是大于等于60 并且 [ $n -lt 80 ]小于80 那么作为标签2 
- 同理
```
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
```
- 如果n满足[ $n -ge 80 ] 大于等于80 并且  [ $n -lt 90 ] 小于90 为标签3
 
- 同理
```
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
```
- 如果n [ $n -ge 90 ]大于等于90 并且  [ $n -le 100 ]小于等于100 ,为标签4
- 同理
 
```
else 
    tag=0
```
- 除此之外 标记 标签0
case格式
```
case $tag in
    1)
    echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
```
- 满足标签1 输出 not ok
- 满足标签2 输出 ok
- 满足标签3 输出 ook
- 满足标签4 输出 oook
- 其他不再这个范围内 输出  "The number range is 0-100." 数字范围为0-100
- 下面来测试下脚本 分别输入不同的数字 或者 其他来测试下
- 比如数字101,提示数字范围在0-100
```
[root@aming-01 shell]# sh case.sh
Please input a number: 101
The number range is 0-100.
[root@aming-01 shell]
```
- 来看下它的执行过程
```
[root@aming-01 shell]# sh -x case.sh
read -p 'Please input a number: ' n
Please input a number: 101
'[' -z 101 ']'
++ echo 101
++ sed 's/[0-9]//g'
+ n1=
'[' -n '' ']'
'[' 101 -lt 60 ']' 
'[' 101 -ge 60 ']'
'[' 101 -lt 80 ']'
'[' 101 -ge 80 ']'
'[' 101 -lt 90 ']'
'[' 101 -ge 90 ']'
'[' 101 -le 100 ']'
+ tag=0
case $tag in
echo 'The number range is 0-100.'
The number range is 0-100.
[root@aming-01 shell]
```
- 首先判断101这个变量存在不存在,存在继续往下走
- 之后做一个判断,存在把数字清空,最后n1为空 
- 判断它是否不为空,不为空那就正常,正常的话那就往下走,做一个判断
- 101是否小于60 
- 101是否大于等于60
- 101是否小于80
- 101是否大于等于80
- 101是否小于90
- 101是否大于等于90
- 101是否小于等于100
- 这些条件都不满足,所以tag= 0 所以提示  'The number range is 0-100.'数字范围在0-100
 
- 如果输入78 输出ok
```
[root@aming-01 shell]# sh -x case.sh
read -p 'Please input a number: ' n
Please input a number: 78
'[' -z 78 ']'
++ echo 78
++ sed 's/[0-9]//g'
+ n1=
'[' -n '' ']'
'[' 78 -lt 60 ']'
'[' 78 -ge 60 ']'
'[' 78 -lt 80 ']'
+ tag=2
case $tag in
echo ok
ok
[root@aming-01 shell]
```
- 如果输入88 ,输出ook
```
[root@aming-01 shell]# sh -x case.sh
read -p 'Please input a number: ' n
Please input a number: 88
'[' -z 88 ']'
++ echo 88
++ sed 's/[0-9]//g'
+ n1=
'[' -n '' ']'
'[' 88 -lt 60 ']'
'[' 88 -ge 60 ']'
'[' 88 -lt 80 ']'
'[' 88 -ge 80 ']'
'[' 88 -lt 90 ']'
+ tag=3
case $tag in
echo ook
ook
[root@aming-01 shell]
```
- 如果输入一个不是数字的,提示输入一个数字
```
[root@aming-01 shell]# sh -x case.sh
read -p 'Please input a number: ' n
Please input a number: dkkddkdk112
'[' -z dkkddkdk112 ']'
++ echo dkkddkdk112
++ sed 's/[0-9]//g'
+ n1=dkkddkdk
'[' -n dkkddkdk ']'
echo 'Please input a number.'
Please input a number.
exit 1
[root@aming-01 shell]
 
```