码迷,mamicode.com
首页 > 其他好文 > 详细

bash循环,判断语句总结

时间:2015-09-15 14:58:46      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:bash 循环 判断

bash基本的逻辑判断

for:遍历说有列表的元素

while: 只要判断体为真就一直循环

until: 只要判断体为假就一直循环

if:条件判断语句


在详述这四个命令前,先看下一些基本知识

bash的算数运算

[root@centos ~]# a=5
[root@centos ~]# b=7
[root@centos ~]# echo $a+$b
5+7

上面例子说明a,b是字符不会根据符号自动做运算.

如果要做运算需要$[]括起来, 但是结果是整数

[root@centos ~]# echo $[$a+$b]
12
[root@centos ~]# echo $[$a*$b]
35
[root@centos ~]# echo $[$b/$a]
1


可以用bc命令输出小数

[root@centos ~]# echo $b/$a | bc -l
1.40000000000000000000

数值的自加自减, 用let命令后面加++, 就表示值自加1, --表示值自减1

[root@centos ~]# echo $a
5
[root@centos ~]# let a++
[root@centos ~]# echo $a
6

明确定义变量为整型

[root@centos ~]# declare -i a=100
[root@centos ~]# echo $a
100
[root@centos ~]# a=50
[root@centos ~]# echo $a
50
[root@centos ~]# a=test
[root@centos ~]# echo $a
0

由上例可以看出整型变量给赋值字符串,一律为0


布尔运算: 用在关系的判断上和语句if, while,until一起用

与运算

真&&真 = 真

真&&假 = 假

假&&真 = 假

假&&假 = 假

或运算

真||真 = 真

真||假 = 真

假||真 = 真

假||假 = 假


整型条件测试

-gt 大于

-ge 大于等于

-lt 小于

-le 小于等于

-eq 等于

-ne 不等于

格式 [ 数字1 -gt 数字2 ] 

数字1大于数字2的运算, 注意数字和[]之间有个空格


字符串条件测试

>: 大于比较ascii码

<: 小于

>=: 大于等于

<=: 小于等于

==: 等于

!=: 不等于

-n: 不空为真, 空为假

-z: 不空为假, 空为真

[root@centos ~]# a=test
[root@centos ~]# if [ $a == test ]; then echo "success"; fi
success
[root@centos ~]# if [ -n $a ]; then echo "success"; fi
success

注意:一定要加中括号, 且有空格.


组合条件测试

与运算 [ 条件1 -a 条件2 ] 或者 条件1 && 条件2,  前者条件1或2 都不用再有中括号了, 后者条件1,2要有中括号

[root@centos ~]# a=1
[root@centos ~]# b=1
[root@centos ~]# if [ $a -eq 1 ] && [ $b -eq 1 ] ; then echo "success"; fi
success
[root@centos ~]# if [ $a -eq 1 -a  $b -eq 1 ] ; then echo "success"; fi
success

或运算 [ 条件1 -o 条件2 ] 或者 条件1 || 条件2

[root@centos ~]# a=1
[root@centos ~]# b=2
[root@centos ~]# if [ $a -eq 1 -a  $b -eq 1 ] ; then echo "success"; fi
[root@centos ~]# if [ $a -eq 1 -o  $b -eq 1 ] ; then echo "success"; fi
success

非运算 [ -not 条件 ] 或者 ! 条件, !和条件中间要有空格, !可以放在中括号内或外

[root@centos ~]# if ! [ $a -eq 2 ] ; then echo "success"; fi
success
[root@centos ~]# if [ ! $a -eq 2 ] ; then echo "success"; fi
success


文件条件测试

-a, -e 文件: 存在未真, 不存在为假

-f 文件: 普通文件为真, 否则为假

-d: 目录为真, 否则为假

-L/-h: 符号链接为真, 否则为假

-b:块设备为真

-c: 字符设备为真

-S: 套接文件为真

-p: 设备管道为真



下面详细说明这个四个命令的语法以及一些示例

for 变量名 in 列表; do

    循环体

done

变量名:相当于定义从列表中定义变量, 前面不用加$

列表: 用空格或换行符做分隔的字符串. 可以用{1..10} 或 `seq 1 10‘ 表示从1 到10 默认是加1

创建user1到user5的用户

[root@centos ~]# for i in {6..8}; do useradd user$i; done
[root@centos ~]# tail /etc/passwd
user6:x:505:505::/home/user6:/bin/bash
user7:x:506:506::/home/user7:/bin/bash
user8:x:507:507::/home/user8:/bin/bash

if

1

if 测试条件; then

        执行语句

fi

#user1存在则打印user1 exists
[root@centos ~]# if id user1 &> /dev/null; then echo "user1 exists"; fi
user1 exists


2. 

if 测试条件; then

    执行语句1

else

    执行语句2

fi

两个分支选其一

#user10存在和不存在打印不同语句
[root@centos ~]# if id user10 &> /dev/null; then echo "user10 exists"; else echo "user10 does not exist"; fi
user10 does not exist

3. 

if 条件1; then

    分支1

eif 条件2; then

    分支2

eif 条件3 then

    分支3

...

else

    分支n

fi

多分支选择

[root@centos ~]# a=1
[root@centos ~]# if [ $a -eq 1 ]; then echo "a=1"; elif [ $a -eq 2 ]; then echo "a=2"; else echo " a != 1 or a !=2 "; fi
a=1
[root@centos ~]# a=2
[root@centos ~]# if [ $a -eq 1 ]; then echo "a=1"; elif [ $a -eq 2 ]; then echo "a=2"; else echo " a != 1 or a !=2 "; fi
a=2
[root@centos ~]# a=3
[root@centos ~]# if [ $a -eq 1 ]; then echo "a=1"; elif [ $a -eq 2 ]; then echo "a=2"; else echo " a != 1 or a !=2 "; fi
 a != 1 or a !=2


while和until循环

while 测试条件; do

    循环体

done

测试条件为真, 一直循环, 为假则跳出

[root@centos ~]# count=1
[root@centos ~]# while [ $count -le 10 ]; do echo $count; let count++; done
1
2
3
4
5
6
7
8
9
10


until 测试条件; do

    循环体

done

测试条件为假则一直循环, 为真则跳出



bash循环,判断语句总结

标签:bash 循环 判断

原文地址:http://jzrobbie.blog.51cto.com/6535329/1694839

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!