一、
WEB
应用软件安装
1.1 nginx
WEB
中间件
1.2 nginx
下载
1.3 nginx
源码包安装
[root
@localhost
~
]
# wget
http://nginx.org/download/nginx-1.17.9.tar.gz
1
[root
@localhost
~
]
# ls
nginx-1
.17.9
.tar.gz
1
2
解决环境依赖
[root
@localhost
~
]
# yum -y install gcc pcre-devel
zlib-devel
1
2
解压软件包
[root
@localhost
~
]
# tar xf nginx-1.17.9.tar.gz
[root
@localhost
~
]
# ls
nginx-1
.17.9
[root
@localhost
~
]
# cd nginx-1.17.9/
[root
@localhost
nginx-1
.17.9
]
# ls
auto CHANGES.ru configure html
man
src
CHANGES conf contrib LICENSE README
配置
[root
@localhost
nginx-1
.17.9
]
# ./configure --
prefix=/usr/local/nginx
......
Configuration summary
+
using system PCRE library
+
OpenSSL library is not used
+
using system zlib library
nginx path prefix
:
"/usr/local/nginx"
nginx binary file
:
"/usr/local/nginx/sbin/nginx"
nginx modules path
:
"/usr/local/nginx/modules"
nginx configuration prefix
:
"/usr/local/nginx/conf"
nginx configuration file
:
"/usr/local/nginx/conf/nginx.conf"
nginx pid file
:
"/usr/local/nginx/logs/nginx.pid"
nginx error log file
:
"/usr/local/nginx/logs/error.log"
nginx http access log file
:
"/usr/local/nginx/logs/access.log"
nginx http client request body temporary
files
:
"client_body_temp"
nginx http proxy temporary files
:
"proxy_temp"
nginx http fastcgi temporary files
:
"fastcgi_temp"
nginx http uwsgi temporary files
:
"uwsgi_temp"
nginx http scgi temporary files
:
"scgi_temp
编译
[root
@localhost
nginx-1
.17.9
]
# make
安装
[root
@localhost
nginx-1
.17.9
]
# make install
启动服务
[root
@localhost
~
]
# ls /usr/local/nginx/
conf html logs sbin
[root
@localhost
~
]
# ls /usr/local/nginx/sbin/
nginx
[root
@localhost
~
]
# /usr/local/nginx/sbin/nginx
[root
@localhost
~
]
# lsof -i :80
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF
NODE NAME
nginx
69481
root
6
u IPv4
137888
0
t0
TCP
*:
http (LISTEN)
nginx
69482
nobody
6
u IPv4
137888
0
t0
TCP
*:
http (LISTEN)
访问验证
方法一:
[root
@localhost
~
]
# curl -I http://localhost
HTTP
/
1.1 200
OK
Server
:
nginx
/
1.17.9
Date
:
Tue,
07
Apr
2020 03
:
51
:
50
GMT
Content-Type
:
text
/
html
Content-Length
:
612
Last-Modified
:
Tue,
07
Apr
2020 03
:
48
:
04
GMT
Connection
:
keep-alive
ETag
:
"5e8bf7f4-264"
Accept-Ranges
:
bytes
[root
@localhost
~
]
# echo $?
0
方法二:
[root
@localhost
~
]
# yum provides *bin/elinks
[root
@localhost
~
]
# yum -y install elinks
[root
@localhost
~
]
# elinks http://localhost --
dump
20
二、判断语句
if
2.1 if
shell
编程中引入
if
原因是:可以对命令执行进行选择;可以对命令
执行的结果进行判断等。
2.2 if
语法
if
使用语法取决于条件的多少
Welcome to nginx
!
If
you see this page, the nginx web server is
successfully installed and
working. Further configuration is required.
For
online documentation and support please
refer to [
1
]nginx.org.
Commercial support is available at
[
2
]nginx.com.
Thank you
for
using nginx.
References
Visible links
1.
http
://
nginx.org
/
2.
http
://
nginx.com
/
[root
@localhost
~
]
# echo $?
0
39
2.2.1
单条件使用
if
只需要一步判断,条件返回真干什么或者条件返回假干什么。
伪代码:
假如
条件为真
那么
执行
commands
代码块
结束
语句:
if
[ condition ]
#condition
值为
true or
false
then
#
条件为真的时候执行
commands
#
代码块 一行或者多行代码
fi
或
if
[ condition ];then
commands
2.2.2
单条件
if......else
单条件可能为真,也可能为假,如果是真,命令执行;如果为
假,则输出帮助信息或退出等等。
16
fi
条件为真,执行命令
[root
@localhost
shell03]
# cat 01_if_test.sh
#!/usr/bin/bash
# xxx
if
[
1
==
1
];then
echo
"hello world"
fi
[root
@localhost
shell03]
# bash -x 01_if_test.sh
+
'['
1
==
1
']'
+
echo
'hello world'
hello world
[root
@localhost
shell03]
# bash 01_if_test.sh
hello world
条件为假,不执行命令
[root
@localhost
shell03]
# cat 02_if_test.sh
#!/usr/bin/bash
# xxx
if
[
1
!=
1
];then
echo
"hello world"
fi
[root
@localhost
shell03]
# bash 02_if_test.sh
伪代码:
2
假如条件为真
3
那么
4
5
执行
commands1
代码块
6
否则
7
执行
commands2
代码块
8
结束
9
10
代码块:
11
if
[ condition ]
12
then
条件为真
13
commands1
真
要执行代码块
14
else
条件为假
15
commands2
假
要执行的代码块
16
fi
结束
2.2.3
练习
条件为真
[root
@localhost
shell03]
# cat 03_if_else_test.sh
#!/usr/bin/bash
# xxx
if
[
1
==
1
];then
echo
"hello world"
else
echo
"world hello"
fi
[root
@localhost
shell03]
# bash
03_if_else_test.sh
hello world
条件为假
[root
@localhost
shell03]
# cat 04_if_else_test.sh
#!/usr/bin/bash
# xxx
if
[
1
!=
1
];then
echo
"hello world"
else
echo
"world hello"
fi
[root
@localhost
shell03]
# bash
04_if_else_test.sh
world hello
1
请判断登录用户是否为
root
用户
思路:
1
、通过命令能否判断当前登录用户是
root
echo
$UID
或
echo
$USER
或
whoami
2
、
if
条件的写法
[root
@localhost
~
]
# [ `echo $UID` -eq 0 ]
[root
@localhost
~
]
# echo $?
0
[root
@localhost
~
]
# [ `echo $USER` == "root" ]
[root
@localhost
~
]
# echo $?
0
[root
@localhost
~
]
# [ `whoami` == "root" ]
[root
@localhost
~
]
# echo $?
0
代码实现:
[root
@localhost
shell03]
# cat 05_if_user_root.sh
#!/usr/bin/bash
# xxx
user
=
`whoami`
if
[
$user
==
"root"
];then
echo
"
当前登录的用户是
:
$user
"
fi
验证:
[root
@localhost
shell03]
# cp 05_if_user_root.sh
/tmp
[root
@localhost
shell03]
# chmod +x
/tmp/05_if_user_root.sh
[root
@localhost
shell03]
#
/tmp/05_if_user_root.sh
当前登录的用户是
:
root
[root
@localhost
shell03]
# su - tom
上一次登录:二
4
月
7 14
:
49
:
14
CST
2020
pts
/
1
上
[tom
@localhost
~
]
$
/
tmp
/
05
_if_user_root.sh
[tom
@localhost
~
]
$
exit
登出
请判断登录用户是否为
root
用户,如果是
root
用户,输出
:
hello
admin
;如果不是
root
用户
,
输出:
hello guest
思路:
代码实现:
[root
@localhost
shell03]
# cat 06_if_user_root.sh
#!/usr/bin/bash
# xxx
user
=
`whoami`
if
[
$user
==
"root"
];then
echo
"hello admin"
else
echo
"hello guest"
fi
或
[root
@localhost
shell03]
# cat 07_if_user_root.sh
#!/usr/bin/bash
# xxx
user
=
`whoami`
if
[
$user
!=
"root"
];then
echo
"hello guest"
else
2.2.4 if...elif...else
多条件判断,用于复杂的条件判断场景中使用。
echo
"hello admin"
fi
验证:
[root
@localhost
shell03]
# cp 06_if_user_root.sh
/tmp
[root
@localhost
shell03]
# chmod +x
/tmp/06_if_user_root.sh
[root
@localhost
shell03]
#
/tmp/06_if_user_root.sh
hello admin
[root
@localhost
shell03]
# su - tom
上一次登录:二
4
月
7 14
:
50
:
21
CST
2020
pts
/
1
上
[tom
@localhost
~
]
$
/
tmp
/
06
_if_user_root.sh
hello guest
伪代码:
2
假如 条件
1
为真
3
4
那么
5
6
执行代码块
1
7
8
假如 条件
2
为真
9
10
那么
11
12
执行代码块
2
13
14
以此类推的
N
个条件及对应的执行代码块
15
否则 【以上所有条件中没有一个满足的】
16
17
执行代码块
X
18
结束
1
代码块:
2
if
[ condition
1
]
满足第一个条件
3
4
then
真
5
6
command1
执行
command1
代码块
7
elif [ condition
2
]
满足第二个条件
8
9
then
真
10
11
commands2
执行
command2
代码块
12
13
......
.
14
else
如果条件都不满足
15
16
commandsX
执行
commandX
代码块
17
fi
结束判断
1
注意
:
多条件判断
需要从最严格开始判断
否者 后面的就不会生
效
2
判断内存的使用率
3
60
以上
警告
4
70
以上
严重警告
5
80
以上
发邮件
6
60
以下
打印使用率
7
8
如果你第一个判断的是
60
以上,那么
70 80
都符合第一个条件。
所以后面的判断
70
80
就没有任何意义
1
案例:对比两个整数的关系
2
思路:
3
1
、获取两个整数 三种方法 在脚本中定义整数变量;通过交互
式;通过位置参数变量传入参数的方式
2
、对比
if
条件进行判断 三个条件
if
..
.elif
方法一:
[root
@localhost
shell03]
# cat 08_inter_1.sh
#!/usr/bin/bash
# xxx
num1
=
100
num2
=
200
if
[
$num1
-gt
$num2
];then
echo
"
$num1
>
$num2
"
elif [
$num1
-eq
$num2
];then
echo
"
$num1
=
$num2
"
else
echo
"
$num1
<
$num2
"
fi
[root
@localhost
shell03]
# bash 08_inter_1.sh
100
<
200
[root
@localhost
shell03]
# bash -x 08_inter_1.sh
+
num1
=
100
+
num2
=
200
+
'['
100
-gt
200
']'
+
'['
100
-eq
200
']'
+
echo
'100 < 200'
100
<
200
方法二:
[root
@localhost
shell03]
# cat 09_inter_2.sh
#!/usr/bin/bash
# xxx
read
-
p
"plz input your num1:"
num1
read
-
p
"plz input your num2:"
num2
if
[
$num1
-gt
$num2
];then
echo
"
$num1
>
$num2
"
elif [
$num1
-eq
$num2
];then
echo
"
$num1
=
$num2
"
else
echo
"
$num1
<
$num2
"
fi
[root
@localhost
shell03]
# bash 09_inter_2.sh
plz input your num1
:
10
plz input your num2
:
5
10
>
5
[root
@localhost
shell03]
# bash 09_inter_2.sh
plz input your num1
:
10
plz input your num2
:
10
10
=
10
[root
@localhost
shell03]
# bash 09_inter_2.sh
plz input your num1
:
10
plz input your num2
:
20
10
<
20
方法三:
[root
@localhost
shell03]
# cat 10_inter_3.sh
#!/usr/bin/bash
# xxx
if
[
$1
-gt
$2
];then
echo
"
$1
>
$2
"
elif [
$1
-eq
$2
];then
echo
"
$1
=
$2
"
else
echo
"
$1
<
$2
"
fi
[root
@localhost
shell03]
# bash 10_inter_3.sh 10
20
10
<
20
[root
@localhost
shell03]
# bash 10_inter_3.sh 20
10
20
>
10
[root
@localhost
shell03]
# bash 10_inter_3.sh 10
10
10
=
10
练习:判断浮点数之间的关系
思路:
1
、获取浮点数方法 输入浮点数,放大浮点数,比对放大后的浮点
数
2
、使用
if
多条件
代码实现:
[root
@localhost
shell03]
# cat 11_float.sh
#!/usr/bin/bash
# xxx
num1
=
`
echo
"scale=2;
$1
*100"
|
bc
|
cut
-
d
"."
-
f1`
num2
=
`
echo
"scale=2;
$2
*100"
|
bc
|
cut
-
d
"."
-
f1`
echo
"
$num1
"
echo
"
$num2
"
if
[
$num1
-gt
$num2
];then
echo
"
$1
>
$2
"
elif [
$num1
-eq
$num2
];then
echo
"
$1
=
$2
"
else
echo
"
$1
<
$2
"
2.2.4 if
嵌套
[root
@localhost
shell03]
# sh 11_float.sh 1.34
2.33
134
233
1.34
<
2.33
[root
@localhost
shell03]
# vim 11_float.sh
[root
@localhost
shell03]
# cat 11_float.sh
#!/usr/bin/bash
# xxx
num1
=
`
echo
"scale=2;
$1
*100"
|
bc
|
cut
-
d
"."
-
f1`
num2
=
`
echo
"scale=2;
$2
*100"
|
bc
|
cut
-
d
"."
-
f1`
if
[
$num1
-gt
$num2
];then
echo
"
$1
>
$2
"
elif [
$num1
-eq
$num2
];then
echo
"
$1
=
$2
"
else
echo
"
$1
<
$2
"
fi
[root
@localhost
shell03]
# sh 11_float.sh 1.34
2.33
1.34
<
2.33
[root
@localhost
shell03]
# sh 11_float.sh 5.34
2.33
5.34
>
2.33
[root
@localhost
shell03]
# sh 11_float.sh 2.33
2.33
2.33
=
2.33
当在同一个条件中需要多次进行判断时使用
通过
if
条件嵌套实现输入整数关系判断
[root
@localhost
shell03]
# cat 12_inter_4.sh
#!/usr/bin/bash
# xxx
if
[
$1
-ne
$2
];then
if
[
$1
-gt
$2
];then
echo
"
$1
>
$2
"
else
echo
"
$1
<
$2
"
fi
else
echo
"
$1
=
$2
"
fi
[root
@localhost
shell03]
# bash 12_inter_4.sh 10
10
10
=
10
[root
@localhost
shell03]
# bash 12_inter_4.sh 11
10
11
>
10
[root
@localhost
shell03]
# bash 12_inter_4.sh 10
11
10
<
11
通过
if
条件嵌套实现输入整数关系判断
[root
@localhost
shell03]
# cat 13_inter_5.sh
#!/usr/bin/bash
# xxx
if
[
$1
-eq
$2
];then
echo
"
$1
=
$2
"
2.2.4 if
嵌套案例
nginx
安装
else
if
[
$1
-gt
$2
];then
echo
"
$1
>
$2
"
else
echo
"
$1
<
$2
"
fi
fi
[root
@localhost
shell03]
# bash 13_inter_5.sh 10
10
10
=
10
[root
@localhost
shell03]
# bash 13_inter_5.sh 10
11
10
<
11
[root
@localhost
shell03]
# bash 13_inter_5.sh 11
10
11
>
10
需求:通过
shell
脚本实现
web
中间件
nginx
安装、启动服务、验
证安装结果。
1
思路:
1
、下载
nginx wget
命令
if
2
、解决依赖
yum
-
y install gcc pcre-devel zlib
devel
3
、定义变量
4
、解压
5
、进入目录
if
6
、配置
.
/
configure
--
prefix
=/
usr
/
local
/
nginx
if
7
、编译
make
if
8
、安装
make install
if
9
、启动服务
/
usr
/
local
/
nginx
/
sbin
/
nginx
10
、验证
elinks http
://
localhost
--
dump
代码实现:
[root
@localhost
shell03]
# cat
14_nginx_install.sh
#!/usr/bin/bash
# DESC: install nginx
#
判断
wget
命令是否安装
which wget
if
[
$?
-eq
0
];then
echo
"wget installed"
else
echo
"wget not installed,beging
install..."
yum
-
y install wget
fi
#
使用
wget
命令下载
nginx
wget http
://
nginx.org
/
download
/
nginx-
1
.17.9
.tar.gz
#
解决依赖
yum
-
y install gcc pcre-devel zlib-devel
#
定义变量
nginx_pkg
=
nginx-1
.17.9
.tar.gz
#
解压
nginx
源码包
tar xf
${nginx_pkg}
#
进入
nginx
解压目录
if
[
$?
-eq
0
];then
cd
nginx-1
.17.9
else
echo
"Error:tar
解压失败,请重新解压!
"
exit
1
fi
#
配置
.
/
configure
--
prefix
=/
usr
/
local
/
nginx
if
[
$?
-eq
0
];then
#
编译
make
if
[
$?
-eq
0
];then
#
安装
make install
if
[
$?
-eq
0
];then
#
启动服务
echo
"nginx
starting......"
/
usr
/
local
/
nginx
/
sbin
/
nginx
else
echo
"Error:
安装失败,请重
新安装
"
exit
1
fi
三、
if
与
shell
运算结合
3.1 shell
运算
赋值运算
算术运算
逻辑运算
比较运算
else
echo
"Error:
编译失败,请重新编译!
"
exit
1
fi
else
echo
"Error:
配置失败,请检查所依赖软件是否安
装!
"
exit
1
fi
#
验证
which elinks
if
[
$?
-eq
0
];then
elinks http
://
localhost
--
dump
else
yum
-
y install elinks
elinks http
://
localhost
--
dump
fi
3.2 if
与
shell
比较运算
3.2.1
整数运算
3.2.2
浮点运算
原本
shell
不支持浮点数比较
[root
@localhost
shell03]
# test 1 -eq 1;echo $?
0
[root
@localhost
shell03]
# cat 15_if_inter.sh
#!/usr/bin/bash
# xxx
if
test
1
-eq
1
;then
echo
"hello world"
fi
[root
@localhost
shell03]
# sh 15_if_inter.sh
hello world
或
[root
@localhost
shell03]
# cat 16_if_inter.sh
#!/usr/bin/bash
# xxx
if
[
1
-eq
1
];then
echo
"hello world"
fi
[root
@localhost
shell03]
# sh 16_if_inter.sh
hello world
强行比较的话,可以把浮点数放大相同的倍数,再比较整数整
数部分
[root
@localhost
shell03]
# cat 17_if_float.sh
#!/usr/bin/bash
# xxx
num1
=
`
echo
"scale=2;
$1
*100"
|
bc
|
cut
-
d
"."
-
f1`
num2
=
`
echo
"scale=2;
$2
*100"
|
bc
|
cut
-
d
"."
-
f1`
if
[
$num1
-gt
$num2
];then
echo
"
$1
>
$2
"
elif [
$num1
-eq
$num2
];then
echo
"
$1
=
$2
"
else
echo
"
$1
<
$2
"
fi
[root
@localhost
shell03]
# sh 17_if_float.sh 1.34
2.33
1.34
<
2.33
[root
@localhost
shell03]
# sh 17_if_float.sh 2.33
1.34
2.33
>
1.34
[root
@localhost
shell03]
# sh 17_if_float.sh 2.33
2.33
2.33
=
2.33
[root
@localhost
shell03]
# sh 17_if_float.sh
2.333 1.344
2.333
>
1.344
3.2.3
字符串比较
案例:如果用户输入的是
root
,脚本就告诉用户,登录成功。
分析
if
条件判断
输入的是
root
,需要做比较,这个就用到的字符串比较
与谁进行比较,可提前输入字符
[root
@localhost
shell03]
# cat
18_if_str_user_login.sh
#!/usr/bin/bash
# xxx
read
-
p
"please input your name:"
username
if
[
$username
==
"root"
];then
echo
"
登录成功!
"
fi
[root
@localhost
shell03]
# sh
18_if_str_user_login.sh
please input your name
:
root
登录成功!
[root
@localhost
shell03]
# sh
18_if_str_user_login.sh
please input your name
:
tom
或
[root
@localhost
shell03]
# cat
19_if_str_user_login.sh
#!/usr/bin/bash
# xxx
read
-
p
"please input your name:"
username
if
[
$username
==
"root"
];then
echo
"
登录成功!
"
else
echo
"
用户名输入不正确,请重新输入
!"
fi
[root
@localhost
shell03]
# sh
19_if_str_user_login.sh
please input your name
:
root
登录成功!
[root
@localhost
shell03]
# sh
19_if_str_user_login.sh
please input your name
:
tom
用户名输入不正确,请重新输入
!
[root
@localhost
shell03]
# cat 28_test_str_if.sh
#!/usr/bin/bash
# xxx
a
=
123
if
[
-
n
"
$a
"
];then
echo
"
此变量为非空
"
else
echo
"
此变量为空
"
fi
[root
@localhost
shell03]
# sh 28_test_str_if.sh
此变量为非空
[root
@localhost
shell03]
# cat 29_test_str_if.sh
#!/usr/bin/bash
# xxx
1
2
3
4
3.2.4
文件比较
a
=
123
if
[
-
z
"
$a
"
];then
echo
"
此变量为空
"
else
echo
"
此变量为非空
"
fi
[root
@localhost
shell03]
# sh 29_test_str_if.sh
此变量为非空
[root
@localhost
shell03]
# cat 30_test_str_if.sh
#!/usr/bin/bash
# xxx
a
=
"abc"
b
=
"xyz"
if
[
"
$a
"
!=
"
$b
"
];then
echo
"
两个变量不相同
"
else
echo
"
两个变量相同
"
fi
[root
@localhost
shell03]
# sh 30_test_str_if.sh
两个变量不相同
[root
@localhost
shell03]
# test -d /home/tom;echo
$?
0
与
if
条件判断结合
如果
/
home
/
smartgo
目录存在的话,脚本退出,不存在,就创建
smartgo
目录。
[root
@localhost
shell03]
# cat 20_test_file_if.sh
#!/usr/bin/bash
# xxx
if
[
-
d
/
home
/
smartgo ];then
exit
0
else
mkdir
/
home
/
smartgo
fi
[root
@localhost
shell03]
# sh 20_test_file_if.sh
[root
@localhost
shell03]
# ls /home
smartgo
或
[root
@localhost
shell03]
# cat 21_test_file_if.sh
#!/usr/bin/bash
# xxx
if
[
! -
d
/
home
/
smartgo1 ];then
mkdir
/
home
/
smartgo1
else
exit
0
fi
[root
@localhost
shell03]
# sh 21_test_file_if.sh
[root
@localhost
shell03]
# ls /home
smartgo1
34
35
判断软件是否安装
[root
@localhost
shell03]
# cat 22_test_bin_if.sh
#!/usr/bin/bash
# xxx
which elinks
if
[
$?
-eq
0
];then
echo
"elinks installed"
sleep
3
exit
0
else
yum
-
y install elinks
fi
或
[root
@localhost
shell03]
# cat 23_test_bin_if.sh
#!/usr/bin/bash
# xxx
if
[
-
x
/
usr
/
bin
/
elinks ];then
echo
"installed"
else
yum
-
y install elinks
fi
[root
@localhost
shell03]
# sh 23_test_bin_if.sh
installed
或
[root
@localhost
shell03]
# cat 24_test_bin_if.sh
#!/usr/bin/bash
# xxx
if
[
! -
x
/
usr
/
bin
/
elinks ];then
yum
-
y install elinks
else
echo
"installed"
fi
[root
@localhost
shell03]
# sh 24_test_bin_if.sh
installed
33
34
35
36
37
38
39
40
判断进程是否存在,如果存在就结束它
[root
@localhost
shell03]
#
/usr/local/nginx/sbin/nginx
[root
@localhost
shell03]
# lsof -i :80
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF
NODE NAME
nginx
9649
root
6
u IPv4
68032
0
t0
TCP
*:
http (LISTEN)
nginx
9650
nobody
6
u IPv4
68032
0
t0
TCP
*:
http (LISTEN)
[root
@localhost
shell03]
# cat
/usr/local/nginx/logs/nginx.pid
9649
通过脚本实现结束进程
[root
@localhost
shell03]
# cat
25_test_proccess_if.sh
#!/usr/bin/bash
# xxx
if
[
-f /
usr
/
local
/
nginx
/
logs
/
nginx.pid ];then
pid
=
`
cat
/
usr
/
local
/
nginx
/
logs
/
nginx.pid`
kill
-
9
$pid
else
echo
"
进程不存在
"
fi
[root
@localhost
shell03]
# sh
25_test_proccess_if.sh
[root
@localhost
shell03]
# lsof -i :80
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF
NODE NAME
nginx
9650
nobody
6
u IPv4
68032
0
t0
TCP
*:
http (LISTEN)
我们发现
TCP
80
依旧被占用,进程结束不彻底。
[root
@localhost
shell03]
#
/usr/local/nginx/sbin/nginx
[root
@localhost
shell03]
# lsof -i :80
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF
NODE NAME
nginx
9793
root
6
u IPv4
69503
0
t0
TCP
*:
http (LISTEN)
nginx
9794
nobody
6
u IPv4
69503
0
t0
TCP
*:
http (LISTEN)
[root
@localhost
shell03]
# cat
26_test_proccess_if.sh
#!/usr/bin/bash
# xxx
if
[
-f /
usr
/
local
/
nginx
/
logs
/
nginx.pid ];then
pid
=
`
cat
/
usr
/
local
/
nginx
/
logs
/
nginx.pid`
kill
-
s QUIT
$pid
echo
"
进程被关闭
"
else
echo
"
进程不存在
"
fi
[root
@localhost
shell03]
# sh
26_test_proccess_if.sh
进程被关闭
[root
@localhost
shell03]
# lsof -i :80
3.3
、
if
与
shell
逻辑运算
3.3.1
逻辑与
真真为真 有假为假 假假为假
多个条件
使用
-
nt
-
ot
-
ef
比较文件的新旧或是否是同一个文件
[root
@localhost
shell03]
# mkdir /home/testfile1
[root
@localhost
shell03]
# mkdir /home/testfile2
[root
@localhost
shell03]
# test /home/testfile1 -
nt /home/testfile2;echo $?
1
或
[root
@localhost
shell03]
# cat
27_test_file_newold_if.sh
#!/usr/bin/bash
# xxx
if
[
/
home
/
testfile2
-
nt
/
home
/
testfile1 ];then
echo
"yes"
else
echo
"no"
fi
[root
@localhost
shell03]
# sh
27_test_file_newold_if.sh
yes
shell
中测试
[root
@localhost
shell03]
# test 1 == 1 && test 2
== 2
[root
@localhost
shell03]
# echo $?
0
[root
@localhost
shell03]
# test 1 == 1 && test 2
!= 2
[root
@localhost
shell03]
# echo $?
1
[root
@localhost
shell03]
# test 1 != 1 && test 2
== 2
[root
@localhost
shell03]
# echo $?
1
[root
@localhost
shell03]
# test 1 != 1 && test 2
!= 2
[root
@localhost
shell03]
# echo $?
1
[root
@localhost
shell03]
# cat 31_l_if.sh
#!/usr/bin/bash
# xxx
if
[
1
==
1
]
&&
[
2
==
2
];then
echo
"
全部为真
"
else
echo
"
有一侧为假
"
fi
[root
@localhost
shell03]
# sh 31_l_if.sh
全部为真
3.3.2
逻辑或
有真为真 全假为假
多个条件
[root
@localhost
shell03]
# cat 32_l_if.sh
#!/usr/bin/bash
# xxx
if
[
1
!=
1
]
&&
[
2
==
2
];then
echo
"
全部为真
"
else
echo
"
有一侧为假
"
fi
[root
@localhost
shell03]
# sh 32_l_if.sh
有一侧为假
[root
@localhost
shell03]
# test 1 == 1 || test 2
== 2
[root
@localhost
shell03]
# echo $?
0
[root
@localhost
shell03]
# test 1 == 1 || test 2
!= 2
[root
@localhost
shell03]
# echo $?
0
[root
@localhost
shell03]
# test 1 != 1 || test 2
== 2
[root
@localhost
shell03]
# echo $?
0
[root
@localhost
shell03]
# test 1 != 1 || test 2
!= 2
[root
@localhost
shell03]
# echo $?
[root
@localhost
shell03]
# cat 33_l_if.sh
#!/usr/bin/bash
# xxx
if
[
1
==
1
]
||
[
2
==
2
];then
echo
"
全部为真
"
else
echo
"
两侧为假全为假
"
fi
[root
@localhost
shell03]
# sh 33_l_if.sh
全部为真
3.3.3
逻辑非
真的为假,假的为真
一个条件即实现
[root
@localhost
shell03]
# cat 34_l_if.sh
#!/usr/bin/bash
# xxx
if
[
1
!=
1
]
||
[
2
!=
2
];then
echo
"
全部为真
"
else
echo
"
两侧为假全为假
"
fi
[root
@localhost
shell03]
# sh 34_l_if.sh
两侧为假全为假
[root
@localhost
shell03]
# test 1 == 1;echo $?
0
[root
@localhost
shell03]
# test ! 1 == 1;echo $?
1
[root
@localhost
shell03]
# cat 38_l_if.sh
#!/usr/bin/bash
# xxx
if
[
!
1
!=
1
];then
echo
"hello world"
else
echo
"world hello"
fi
或
四、
if
特殊用法
4.1
双小圆括号使用
可以植入数学运算
[root
@localhost
shell03]
# sh 38_l_if.sh
hello world
16
17
18
-
z
判断字符串长度是否为
0
,如果为
0
,则为真,如果为非
0
,则
为假。
[root
@localhost
shell03]
# cat 37_test_str_if.sh
#!/usr/bin/bash
# xxx
a
=
123
if
[
! -
z
"
$a
"
];then
echo
"
此变量为非空
"
else
echo
"
此变量为空
"
fi
[root
@localhost
shell03]
# sh 37_test_str_if.sh
此变量为非空
4.2
双方括号使用
可以在条件中添加通配符
*
或
?
[root
@localhost
shell03]
# cat 39_if.sh
#!/usr/bin/bash
# xxx
if
(( (
5
+
5
-
5
)
*
5
/
5
>
10
))
then
echo
"yes"
else
echo
"no"
fi
[root
@localhost
shell03]
# sh 39_if.sh
no
[root
@localhost
shell03]
# cat 40_if.sh
#!/usr/bin/bash
# xxx
for
var
in
ab
ac
rx bx rvv vt
do
if
[[
"
$var
"
==
r
*
]];then
echo
"
$var
"
fi
done
[root
@localhost
shell03]
# sh 40_if.sh
rx
rvv
五、
if
简写
或
[root
@localhost
shell03]
# [ 1 == 1 ] && echo
"yes" || echo "no"
yes
[root
@localhost
shell03]
# [ 1 != 1 ] && echo
"yes" || echo "no"
no
[root
@localhost
shell03]
# cat 41_test_bin_if.sh
#!/usr/bin/bash
# xxx
if
[
! -
x
/
usr
/
bin
/
elinks ];then
echo
"elinks not install"
else
echo
"elinks installed"
fi
[root
@localhost
shell03]
# sh 41_test_bin_if.sh
elinks installed
[root
@localhost
shell03]
# cat 42_test_bin_if.sh
#!/usr/bin/bash
# xxx
[
! -
x
/
usr
/
bin
/
elinks ]
&&
echo
"elinks not
install"
||
echo
"elinks installed"
[root
@localhost
shell03]
# sh 42_test_bin_if.sh
elinks installed