实现某种操做:老是 测试(经过判断条件来执行) 前提是否知足linux
/tmp/test
10shell
逻辑运算:
布尔运算有两种状态:真,假express
与、或、非、异或编程
与运算: 操做符 && 特色:有一个操做数为假时,结果必为假。两个操做数同时真时,才为真
真,假执行与运算:
真 && 真 = 真
真 && 假 = 假
假 && 真 = 假
假 && 假 = 假bash
或运算: 操做符 ||
真,假执行或运算 特色:有一个操做数为真时,结果必为真。两个操做数同时假时,才为假
真 || 真 = 真
真 || 假 = 真
假 || 真 = 真
假 || 假 = 假
---------------------------------------------------------------------
[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为真仍是为假 都会进行 && 的运算的
---------------------------------------------------------------------测试
非运算:
真,假ui
异或运算:
相同为假,不一样则为真this
命令都有其状态返回值:是有两种状态的
成功:0,真
失败:1-255, 假lua
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 ]]spa
比较运算:
>, <, >=, <=, ==, !=
测试类型:根据比较时的操做数的类型
整型测试:整数比较
字符测试:字符串比较
文件测试:判断文件的存在性及属性等
注意:比较运算一般只在同一种类型间进行
整型测试操做符:
-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 [n]
[root@linux_basic scripts]#type exit
exit is a shell builtin
[root@linux_basic scripts]#help exit
exit: exit [n]
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:表示假
若是条件包含比较之意,则必须使用