shell scripts学习(三)
来源:互联网 发布:怎样申请农村淘宝 编辑:程序博客网 时间:2024/06/08 13:40
1. netstat命令
可查询当前主机所有开启的网络服务端口.
[root@linux ~]# netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:43975 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:51886 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:45903 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::25 :::* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:69 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 192.168.98.255:137 0.0.0.0:*
说明:
-t---------------->tcp
-u--------------->udp
-l---------------->listening
-n--------------->numberic
80--------------->www
22--------------->ssh
21---------------->ftp
25---------------->mail
ex:
通过netstat来侦测主机是否开启了四个网络服务端口
[root@linux ~]# vim sh09.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
echo "Now, the services of your Linux system will be detect!"
echo -e "The www, ftp, ssh, and mail will be detect! \n"
testing='netstat -tuln | grep ":80"'
if [ "$testing" != "" ]; then
echo "WWW is running in your system"
fi
testing='netstat -tuln | grep ":22"'
if [ "$testing" != "" ]; then
echo "SSH is running in your system"
fi
testing='netstat -tuln | grep ":21"'
if [ "$testing" != "" ]; then
echo "FTP is running in your system"
fi
testing='netstat -tuln | grep ":25"'
if [ "$testing" != "" ]; then
echo "Mail is running in your system"
fi
2. 判断语句case......in...esac
判断变量有多个不同内容的情况
语法格式:
case $变量名称 in
"变量内容1")
程序段
;; #------------------------->用两个分号代表程序段的结束
"变量内容2")
程序段
;;
*) #----------------------->其他变量内容
其他程序段
exit 1
;;
esac
3. shell scripts中的函数功能
function的语法格式:
function func_name() {
程序段
}
ex:
[root@linux ~]# vim sh11-2.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
function printit() {
#-------->函数名自定义
echo -n "Your choice is"
}
echo "This program will print your selection!"
case $1 in
"one")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"two")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"three")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
*)
echo "Usage {one|tow|three}"
;;
esac
4. function拥有的内建变量($0, $1, $2, $3, .......),说白了也就是给函数传参
$0------------->代表了函数的名字
$1,$2...------->代表了给函数传的参数
ex:
[root@linux ~]# vim sh11-3.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
function printit() {
#-------->函数名自定义
echo -n "Your choice is $1"
}
echo "This program will print your selection!"
case $1 in
"one")
printit 1 #------------->传入参数1
;;
"two")
printit 2 #------------>传入参数2
;;
"three")
printit 3 #------------->传入参数3
;;
*)
echo "Usage {one|tow|three}"
;;
esac
可查询当前主机所有开启的网络服务端口.
[root@linux ~]# netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:43975 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:51886 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:45903 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::25 :::* LISTEN
udp 0 0 0.0.0.0:68 0.0.0.0:*
udp 0 0 0.0.0.0:69 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 192.168.98.255:137 0.0.0.0:*
说明:
-t---------------->tcp
-u--------------->udp
-l---------------->listening
-n--------------->numberic
80--------------->www
22--------------->ssh
21---------------->ftp
25---------------->mail
ex:
通过netstat来侦测主机是否开启了四个网络服务端口
[root@linux ~]# vim sh09.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
echo "Now, the services of your Linux system will be detect!"
echo -e "The www, ftp, ssh, and mail will be detect! \n"
testing='netstat -tuln | grep ":80"'
if [ "$testing" != "" ]; then
echo "WWW is running in your system"
fi
testing='netstat -tuln | grep ":22"'
if [ "$testing" != "" ]; then
echo "SSH is running in your system"
fi
testing='netstat -tuln | grep ":21"'
if [ "$testing" != "" ]; then
echo "FTP is running in your system"
fi
testing='netstat -tuln | grep ":25"'
if [ "$testing" != "" ]; then
echo "Mail is running in your system"
fi
2. 判断语句case......in...esac
判断变量有多个不同内容的情况
语法格式:
case $变量名称 in
"变量内容1")
程序段
;; #------------------------->用两个分号代表程序段的结束
"变量内容2")
程序段
;;
*) #----------------------->其他变量内容
其他程序段
exit 1
;;
esac
3. shell scripts中的函数功能
function的语法格式:
function func_name() {
程序段
}
ex:
[root@linux ~]# vim sh11-2.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
function printit() {
#-------->函数名自定义
echo -n "Your choice is"
}
echo "This program will print your selection!"
case $1 in
"one")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"two")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"three")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
*)
echo "Usage {one|tow|three}"
;;
esac
4. function拥有的内建变量($0, $1, $2, $3, .......),说白了也就是给函数传参
$0------------->代表了函数的名字
$1,$2...------->代表了给函数传的参数
ex:
[root@linux ~]# vim sh11-3.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
function printit() {
#-------->函数名自定义
echo -n "Your choice is $1"
}
echo "This program will print your selection!"
case $1 in
"one")
printit 1 #------------->传入参数1
;;
"two")
printit 2 #------------>传入参数2
;;
"three")
printit 3 #------------->传入参数3
;;
*)
echo "Usage {one|tow|three}"
;;
esac
0 0
- shell scripts学习(三)
- shell scripts学习(三)
- 学习 shell scripts
- 13 学习 shell scripts
- shell scripts学习(一)
- shell scripts学习(二)
- shell scripts学习(四)
- shell scripts学习(一)
- shell scripts学习(二)
- shell scripts学习(四)
- Shell scripts 学习
- 学习Shell Scripts
- Shell Scripts学习笔记
- 学习 Shell Scripts
- 12-学习Shell-Scripts
- shell scripts的学习(五)
- shell scripts的学习(五)
- 第十三章、学习 Shell Scripts
- shell scripts学习(二)
- 回滚段探究
- 如何让php对页面的缓存进行控制 php处理请求的浏览器缓存处理
- 多线程之主线程与子线程
- 学编程就像选家具:去宜家还是从种树开始?
- shell scripts学习(三)
- DIV盒子模型
- shell scripts学习(四)
- Java垃圾回收器总结
- 程序员最该看的30本书---------------------------国外编辑推荐
- shell scripts的学习(五)
- 【深入Java虚拟机】之七:Java编译与JIT编译
- 认识JavaScript中的作用域和上下文
- MySQL性能优化的最佳20+条经验