1、if结构
- if [ condition ] ;then …fi;
- if [ condition ]; then …else…fi;
- if [ condition ];then …elif [ conditon ]; then …fi;
- 简单的逻辑判断可以用&&||代替
[root@VM_0_2_centos home]
相等
[root@VM_0_2_centos home]
不相等
[root@VM_0_2_centos home]
大于
[root@VM_0_2_centos home]
a.txt
[root@VM_0_2_centos home]
is file
[root@VM_0_2_centos home]
[root@VM_0_2_centos home]
is file
2、for循环
- for循环
- for ((c1;c2;c3));do…done;
[root@VM_0_2_centos home]
0
1
2
3
4
- for 遍历循环
- for i in ${array[*]};do…done;
[root@VM_0_2_centos home]
1
2
3
4
5
[root@VM_0_2_centos /]
bin
boot
...
3、while循环
- while循环
- i=0;while [ condition ];do…;done
[root@VM_0_2_centos /]
0
1
2
[root@VM_0_2_centos home]
hello world
hello java
hello python
hello ios
hello andriod
4、退出控制
- return 函数退出
- exit 脚本退出
- break 退出当前循坏,默认为1
- break 2 退出2层循环
- continue 跳出当前循坏,进入下一次循环
- continue 2 跳到上层循环的下次循环
[root@VM_0_2_centos home]
for((i=0;i<5;i++));
do
[[ $i -eq 3 ]] && exit
echo $i
done
[root@VM_0_2_centos home]
0
1
2
[root@VM_0_2_centos home]
0
1
2
[root@VM_0_2_centos home]
0
1
2
4
5、shell运行环境概念
- bash是一个进程
- bash下还可以再重新启动一个新shell,这个shell是sub shell,原shell会复制自身给它
- 在子shell中定义的变量,会随的shell消亡而消亡
- () 子shell中运行
- {} 当前shell执行
- $$ 当前脚本执行的PID
- & 后台执行
- $! 运行在后台的最后一个作业的PID(即进程ID)