shell脚本学习笔记

1.查看端口情况
查看80端口:

[root@hadoop01 software]# ss -an | grep :80
LISTEN     0      128                       *:80          

2.shell脚本查看某个网址是否能够连接的通

ping -c1 www.baidu.com && echo "www.baidu.com is up" || echo "www.baidu.com is down"
[root@hadoop01 temp]# vim ping.sh
[root@hadoop01 temp]# chmod +x ping.sh 
[root@hadoop01 temp]# sh ping.sh 
PING www.a.shifen.com (36.152.44.96) 56(84) bytes of data.
64 bytes from 36.152.44.96: icmp_seq=1 ttl=128 time=11.7 ms

--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 121ms
rtt min/avg/max/mdev = 11.720/11.720/11.720/0.000 ms
www.baidu.com is up

如果我们不想看运行过程只想看结果:

ping -c1 www.baidu.com &>/dev/null && echo "www.baidu.com is up" || echo "www.baidu.com is down"
[root@hadoop01 temp]# sh ping.sh 
www.baidu.com is up

3.如何将2个脚本文件合并
将bash脚本和Python脚本合并:

[root@hadoop01 temp]# cat ping.sh 
#!/usr/bin/bash
ping -c1 www.baidu.com &>/dev/null && echo "www.baidu.com is up" || echo "www.baidu.com is down!"
[root@hadoop01 temp]# cat python01.sh 
#!/usr/bin/python
print "hello world!"
[root@hadoop01 temp]# cat python01.sh >>ping.sh 
[root@hadoop01 temp]# cat ping.sh 
#!/usr/bin/bash
ping -c1 www.baidu.com &>/dev/null && echo "www.baidu.com is up" || echo "www.baidu.com is down!"
#!/usr/bin/python
print "hello world!"

4.如何在一个脚本里实现不同编译器编译同一个脚本文件
案例

#!/usr/bin/bash
ping -c1 www.baidu.com &>/dev/null && echo "www.baidu.com is up" || echo "www.baidu.com is down!"
#!/usr/bin/python(合并后此行变成注释,为了让Python脚本可以正常编译运行添加如下,告诉编译器应该用什么去编译此段代码)
/usr/bin/python <<-EOF
print "hello world!"
EOF
echo "hello bash"

5.查看脚本执行过程

bash -vx  脚本名

6.查看上个命令执行的返回值(0表示执行成功,非0表示执行失败)

echo $?

7.shell条件测试命令

#方式一:
test 选项 参数
#示例(判断home是否是目录):
[root@hadoop01 ~]# test -d /home
[root@hadoop01 ~]# echo $?
0
[root@hadoop01 ~]# test -d /hmoe
[root@hadoop01 ~]# echo $?
1

#方式二:
[ 选项 参数 ]
#案例:
[root@hadoop01 ~]# [ -d /home ]
[root@hadoop01 ~]# echo $?
0

#方式三:
[root@hadoop01 ~]# [[ -d /home ]];echo $?
0
#查看test命令
[root@hadoop01 ~]# man test

文件测试[操作符 文件或目录]:
测试是否是目录:[ -d dir ]

方式一:
[root@hadoop01 ~]# test -d /home
[root@hadoop01 ~]# echo $?
0   #0表是上个命令执行成功说明home是个目录
[root@hadoop01 ~]# test -d /hmoe
[root@hadoop01 ~]# echo $?
1  #1表是上个命令执行失败说明hmoe不是个目录

#方式二
[root@hadoop01 ~]# [ -d /home ]
[root@hadoop01 ~]# echo $?
0

测试 文件或目录是否存在:[ -e dir|file ]

[root@hadoop01 ~]# [ -e /home/01/temp ]
[root@hadoop01 ~]# echo $?
0

测试是否是文件:[ -f file ]
测试当前用户是否对该文件有读权限:[ -r file ]
测试当前用户是否对该文件有写权限:[ -w file ]
测试当前用户是否对该文件有执行权限:[ -x file ]
测试当前用户是否对该文件有执行权限:[ -x file ]
测试该文件是不是链接:[ -L file ]
测试该文件是不是设备文件:[ -b file ]
测试该文件是不是字符设备文件:[ -c file ]

#如果ccc不是目录则创建ccc目录( 1 && 2 , 当1成立,则执行2 ,否则不执行2)
[ ! -d /ccc ] && mkdir /ccc
#如果ccc是目录则不创建目录( 1 || 2 ,当1成立,则不执行2,否则执行2 )
[ -d /ccc ] || mkdir /ccc

数值比较[整数1 操作符 整数2]
[ 1 -gt 10 ] 大于
[ 1 -lt 10 ] 小于
[ 1 -eq 10 ] 等于
[ 1 -ne 10 ] 不等于
[ 1 -ge 10 ] 大于等于
[ 1 -le 10 ] 小于等于

shell脚本中特使命令符号
() 子shell中执行

[root@hadoop02 temp]# (date)
Mon Jun  8 09:43:26 CST 2020

(()) 数值比较,运算 C语言

[root@hadoop02 temp]# ((1>2));echo $?
1
[root@hadoop02 temp]# ((1<2));echo $?
0

$() 命令替换 与``反引号作用相同

[root@hadoop02 temp]# touch $(date +%F)_file.txt
[root@hadoop02 temp]# ls
1.txt                employee.xlsx
2020-06-08_file.txt  _file.txt
2020-5-16.txt        flume.sh

$(()) 整数运算

[root@hadoop02 temp]# echo $((1+2))
3

{} 集合

[root@hadoop02 bbb]# touch {1..10}.txt
[root@hadoop02 bbb]# ls
10.txt  2.txt  4.txt  6.txt  8.txt
1.txt   3.txt  5.txt  7.txt  9.txt

${} 变量引用、替换

[root@hadoop02 bbb]# num=10
[root@hadoop02 bbb]# echo ${num}
10
[root@hadoop02 bbb]# num1=5
[root@hadoop02 bbb]# num2=10
[root@hadoop02 bbb]# num1=${num2}
[root@hadoop02 bbb]# echo ${num1}
10

[] 条件测试(可以进行整数数值比较,文件测试,字符串比较)

[root@hadoop02 bbb]# [ 1>2 ]
[root@hadoop02 bbb]# echo $?
1

[root@hadoop02 bbb]# [ -d /home -a -f /etc/files ];echo $?
1
[root@hadoop02 bbb]# [ -d /home -o -f /etc/files ];echo $?
0

[[]] 条件测试,支持正则 =~

[root@hadoop02 bbb]# [[ -d /home || -f /etc/files ]];echo $?
0
[root@hadoop02 bbb]# [[ -d /home && -f /etc/files ]];echo $?
1
[root@hadoop02 temp]# [[ 123 =~ ^[0-9]+$ ]];echo $?
0
[root@hadoop02 temp]# [[ ddd =~ ^[0-9]+$ ]];echo $?
1
[root@hadoop02 temp]# [[ 123aa =~ ^[0-9]+$ ]];echo $?
1

$[] 整数运算

[root@hadoop02 temp]# echo $[1+3]
4
[root@hadoop02 temp]# echo $[1*3]
3
[root@hadoop02 temp]# echo $[2**10]
1024
[root@hadoop02 temp]# echo $[2*10]
20
[root@hadoop02 temp]# echo $[10/3]
3
[root@hadoop02 temp]# echo $[10%3]
1
[root@hadoop02 temp]# echo $[10-3]
7

执行脚本
./xxx.sh 需要执行权限 在子shell中执行(可以使绝对路径,也可以是相对路径)
bash 01.sh 不需要执行权限 在子shell中执行
. xxx.sh 不需要执行权限,在当前shell中执行
source xxx.sh 不需要执行权限 在当前shell中执行
提示:通常修改系统配置文件如/etc/profile的PATH等变量后,使之在当前shell中生效

调试脚本
sh -n xxx.sh 仅调试syntax error(检测语法的错误)
sh -vx xxx.sh 以调试的方式执行,查询整个执行过程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值