您的位置:首页 > 运维架构 > Shell

6.2bash编程入门之逻辑运算和条件判断

2016-04-22 00:00 621 查看
实现某种操作:总是 测试(通过判断条件来执行) 前提是否满足

/tmp/test
10

逻辑运算:
布尔运算有两种状态:真,假

与、或、非、异或

与运算: 操作符 && 特点:有一个操作数为假时,结果必为假。两个操作数同时真时,才为真
真,假执行与运算:
真 && 真 = 真
真 && 假 = 假
假 && 真 = 假
假 && 假 = 假

或运算: 操作符 ||
真,假执行或运算 特点:有一个操作数为真时,结果必为真。两个操作数同时假时,才为假
真 || 真 = 真
真 || 假 = 真
假 || 真 = 真
假 || 假 = 假
---------------------------------------------------------------------
[root@localhost ~]# which --skip-alias ls &> /dev/null || [ $? -ne 0 ] && echo "No such command."
No such command.
[root@localhost ~]# which --skip-alias fdjfs &> /dev/null || [ $? -ne 0 ] && echo "No such command."
No such command.
which --skip-alias ls &> /dev/null || [ $? -ne 0 ] || [ $? -ne 0 ] && echo "No such command."
0 || # = 0
&& echo "No such command."
0 && 0 = 0

which --skip-alias fdjfs &> /dev/null || [ $? -ne 0 ] && echo "No such command."
# || 0 = 0
&& echo "No such command."
0 && 0 = 0
所以这里不论which --skip-alias fdjfs &> /dev/null为真还是为假 都会进行 && 的运算的
---------------------------------------------------------------------

非运算:
真,假

异或运算:
相同为假,不同则为真

命令都有其状态返回值:是有两种状态的
成功:0,真
失败:1-255, 假

bash条件测试:
命令执行成功与否即为条件测试
test EXPR
[root@linux_basic scripts]#type test
test is a shell builtin
[root@linux_basic scripts]#help test
test: test [expr]
Evaluate conditional expression.

Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators as well, and numeric comparison operators.
两端和中间是要有空白符的
[ EXPR ]
[[ EXPR ]]

比较运算:
>, <, >=, <=, ==, !=

测试类型:根据比较时的操作数的类型
整型测试:整数比较
字符测试:字符串比较
文件测试:判断文件的存在性及属性等

注意:比较运算通常只在同一种类型间进行

整型测试操作符:
-gt: 例如 [ $num1 -gt $num2 ] 大于
-lt: 小于
-ge: 大于等于
-le: 小于等于
-eq: 等于
-ne: 不等于

字符串测试:
双目
>: [[ "$str1" > "$str2" ]]
<:
>=
<=
==
!=
=~ 判断左边的字符串是否能够被右边的模式所匹配,通常用于[[]];
[[ "$vopt" = ~ pattern ]],一般做行首、行尾锚定,但模式不要加引号

单目:
-n String: 是否不空,不空则为真,空则为假
-z String: 是否为空,空则为真,不空则假

过程式编程:
顺序
选择
循环:for

选择:if和case

if: 三种使用格式
单分支的if语句:
if 测试条件; then
选择分支
fi
表示条件测试状态返回值为真,则执行选择分支;
!命令取反
if ! id $username &> /dev/null; then 只需要状态值,不需要返回值
useradd $username
fi

练习:写一个脚本,接受一个参数,这个参数是用户名;如果此用户存在,则显示其ID号;
自定义shell进程的状态返回值:
exit

[root@linux_basic scripts]#type exit
exit is a shell builtin
[root@linux_basic scripts]#help exit
exit: exit

Exit the shell. 退出shell

Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.

双分支的if语句:
if 测试条件; then
选择分支1
else
选择分支2
fi

两个分支仅执行其中之一。

练习:通过命令行传递两个整数参数给脚本,脚本可以返回其大者。

练习:通过命令行传递任意个整数给脚本,脚本可以返回其大者。

练习:通过命令行给定一个文件路径,而后判断:
如果此文件中存在空白行,则显示其空白行的总数;
否则,则显示无空白行;

if grep "^[[:space]]*$" $1 &> /dev/null; then 只取状态结果
echo "$1 has $(grep "^[[:space]]*$" $1 | wc -l) blank lines."
else
echo "No blank lines"
fi

注意:如果把命令执行成功与否当作条件,则if语句后必须只跟命令本身,而不能引用。

if [ $(grep "^[[:space:]]*$" $1 | wc -l) -lt 1 ]

多分支的if语句:有多个测试条件
if 条件1; then
分支1
elif 条件2; then
分支2
elif 条件3; then
分支3
...
else
分支n
fi

练习:传递一个参数给脚本:
如果参数为quit,则显示说你要退出;
如果参数为yes,则显示说你要继续
其它任意参数,则说无法识别;

练习:传递一个用户名给脚本:
如果此用户的id号为0,则显示说这是管理员
如果此用户的id号大于等于500,则显示说这是普通用户
否则,则说这是系统用户;

#!/bin/bash
#
if [ $# -lt 1 ]; then 1.必须得参数大于1判断。
echo "Usage: `basename $0` username"
exit 1
fi

if ! id -u $1 &> /dev/null; then 2.执行条件必须存在判断。
echo "Usage: `basename $0` username"
echo "No this user $1."
exit 2
fi

if [ $(id -u $1) -eq 0 ]; then
echo "Admin"
elif [ $(id -u $1) -ge 500 ]; then
echo "Common user."
else
echo "System user."
fi

if 测试条件; then
测试条件:在bash中是命令 (test EXPR, [ EXPR ] ) 或由 [[ EXPR ]]
if 命令;
在bash运行至if时,其后的命令会被执行,其状态结果则作为判断标准:
0:表示真
1-255:表示假

如果条件包含比较之意,则必须使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: