-------------------------分割线-----------------------
if 的几种用法:
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi
[root@OBird shell]# touch 1.txt
[root@OBird shell]# if [ -f 1.txt ]; then echo ok; fi #判断是否存在1.txt
ok
[root@OBird shell]# if [ -r 1.txt ]; then echo ok; fi # -r 判断文件是否可读
ok
[root@OBird shell]# if [ -d /tmp/ ]; then echo ok; fi #判断是否存在 目录
ok
[root@OBird shell]# vim if2.sh
#!/bin/bash
read -p "please input a number:" n
m=`echo $n|sed 's/[0-9]//g'`
if [ -n "$m" ] # -n 表示判断变量是否不为空
then
echo "the character you input is not a number,please retry."
else
echo $n
fi
[root@OBird shell]# sh if2.sh
please input a number:iiiiiiiiii
the character you input is not a number,please retry.
[root@OBird shell]# sh if2.sh
please input a number:90909090
90909090
[root@OBird shell]# sh -x if2.sh #实例分解
+ read -p 'please input a number:' n
please input a number:oiu00909
++ echo oiu00909
++ sed 's/[0-9]//g'
+ m=oiu
+ '[' -n oiu ']'
+ echo 'the character you input is not a number,please retry.'
the character you input is not a number,please retry.
[root@OBird shell]# sh -x if2.sh #实例分解
+ read -p 'please input a number:' n
please input a number:9909090909
++ sed 's/[0-9]//g'
++ echo 9909090909
+ m=
+ '[' -n '' ']'
+ echo 9909090909
9909090909
[root@OBird shell]# vim if2.sh
#!/bin/bash
read -p "please input a number:" n
m=`echo $n|sed 's/[0-9]//g'`
if [ -z "$m" ] # -z 表示判断变量是否 为空,与-n 相反。
then
echo $n
else
echo "the character you input is not a number,please retry."
fi
[root@OBird shell]# sh -x if2.sh
+ read -p 'please input a number:' n
please input a number:90909090
++ sed 's/[0-9]//g'
++ echo 90909090
+ m=
+ '[' -z '' ']'
+ echo 90909090
90909090
[root@OBird shell]# sh -x if2.sh
+ read -p 'please input a number:' n
please input a number:jki0000
++ sed 's/[0-9]//g'
++ echo jki0000
+ m=jki
+ '[' -z jki ']'
+ echo 'the character you input is not a number,please retry.'
the character you input is not a number,please retry.
---------------------分割线-------------------------------
case 的用法:不是太常用。在启动脚本中用的比较多。
---------------------分割线-------------------------------
for循环:
[root@OBird shell]# vim for.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done
[root@OBird shell]# sh for.sh
1
2
3
4
5
6
7
8
9
10
[root@OBird shell]# vim for.sh
#!/bin/bash
sum=0
for i in `seq 1 10`
do
sum=$[$sum+$i]
done
echo $sum
~
[root@OBird shell]# sh -x for.sh
+ sum=0
++ seq 1 10
+ for i in '`seq 1 10`'
+ sum=1
+ for i in '`seq 1 10`'
+ sum=3
+ for i in '`seq 1 10`'
+ sum=6
+ for i in '`seq 1 10`'
+ sum=10
+ for i in '`seq 1 10`'
+ sum=15
+ for i in '`seq 1 10`'
+ sum=21
+ for i in '`seq 1 10`'
+ sum=28
+ for i in '`seq 1 10`'
+ sum=36
+ for i in '`seq 1 10`'
+ sum=45
+ for i in '`seq 1 10`'
+ sum=55
+ echo 55
55
---------------------分割线-------------------------------
while 循环
[root@OBird ~]# vim while.sh
#!/bin/bash
n=0
while [ $n -le 10 ]
do
echo $n
n=$[$n+1]
done
~
[root@OBird ~]# sh -x while.sh #测试结果
+ n=0
+ '[' 0 -le 10 ']'
+ echo 0
0
+ n=1
+ '[' 1 -le 10 ']'
+ echo 1
1
+ n=2
+ '[' 2 -le 10 ']'
+ echo 2
2
+ n=3
+ '[' 3 -le 10 ']'
+ echo 3
3
+ n=4
+ '[' 4 -le 10 ']'
+ echo 4
4
+ n=5
+ '[' 5 -le 10 ']'
+ echo 5
5
+ n=6
+ '[' 6 -le 10 ']'
+ echo 6
6
+ n=7
+ '[' 7 -le 10 ']'
+ echo 7
7
+ n=8
+ '[' 8 -le 10 ']'
+ echo 8
8
+ n=9
+ '[' 9 -le 10 ']'
+ echo 9
9
+ n=10
+ '[' 10 -le 10 ']'
+ echo 10
10
+ n=11
+ '[' 11 -le 10 ']'
---------------------分割线-------------------------------
shell 中断继续退出:
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
break
fi
echo $i
done
~
[root@OBird shell]# sh -x for2.sh
++ seq 1 10
+ for i in '`seq 1 10`'
+ echo 1
1
+ '[' 1 -eq 4 ']'
+ echo 1
1
+ for i in '`seq 1 10`'
+ echo 2
2
+ '[' 2 -eq 4 ']'
+ echo 2
2
+ for i in '`seq 1 10`'
+ echo 3
3
+ '[' 3 -eq 4 ']'
+ echo 3
3
+ for i in '`seq 1 10`'
+ echo 4
4
+ '[' 4 -eq 4 ']'
+ break #退出整个循环
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
continue #结束本次循环
fi
echo $i
done
echo "for done"
~
[root@OBird shell]# sh for2.sh
1
1
2
2
3
3
4
5
5
6
6
7
7
8
8
9
9
10
10
for done
[root@OBird shell]# vim for2.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
if [ $i -eq 4 ]
then
exit
fi
echo $i
done
echo "for done"
[root@OBird shell]# sh for2.sh
1
1
2
2
3
3
4
#此处无for done ,直接退出整个shell