多重、复杂条件判断式

在同一个数据的判断中,如果该数据需要进行多种不同的判断时,应该怎么作?举例来说,下面的脚本中,我们只要进行一次 $yn 的判断就好 (仅进行一次 if ),不想要作多次 if 的判断。

#!/bin/bash

read -p "Please input (Y/N)" yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK,continue!"
exit 0
fi

if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh,intrrupt!"
exit 0
fi

echo "I don't know what your choice is " && exit 0

此时你就得要知道底下的语法了:

#一个条件判断,分成功进行和失败进行(else)

if [ 条件判断式 ]; then

当条件判断式成立时,可以进行的命令工作内容;

else

当条件判断式不成立时,可以进行的命令工作内容;

fi

如果考虑更复杂的情况,则可以使用这个语法:

#多个条件判断(if 。。。 elif 。。。elif 。。。else)分多种情况运行

if [ 条件判断式一 ]; then

当条件判断式一成立时,可以进行的命令工作内容;

elif [ 条件判断式二 ]; then

当条件判断式二成立时,可以进行的命令工作内容;
else

当条件判断式一喝二都不成立的时,可以进行的命令工作内容;

fi

看下面的脚本:

#!/bin/bash

read -p "Please input (Y/N): " yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK, continue"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what your choice is"
fi

好了,让我们再来进行另外一个案例的设计。一般来说,如果你不希望使用者由键盘输入额外的数据时, 可以使用参数功能 ($1)!让使用者在下达命令时就将参数带进去! 现在我们想让使用者输入『 hello 』这个关键字时,利用参数的方法可以这样依序设计:

1.判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?";

2.如果没有加任何参数,就提示使用者必须要使用的参数下达法;

3.而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。

整个程序的撰写可以是这样的:

#!/bin/bash
if [ "$1" == "hello" ]; then
echo "Hello,how are you?"
elif [ "$1" == "" ]; then
echo "You must input parameters,eg: $0 someword"
else
echo "The only parameter you can use is 'hello',eg: $0 hello"
fi

测试结果:

[oracle@SOR_SYS~]$ sh decide.sh
You must input parameters,eg: decide.sh someword
[oracle@SOR_SYS~]$ sh decide.sh now
The only parameter you can use is 'hello',eg: decide.sh hello
[oracle@SOR_SYS~]$ sh decide.sh hello
Hello,how are you?
[oracle@SOR_SYS~]$

[root@www ~]# netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*

#封包格式 本地IP:端口 远程IP:端口 是否监听

上面的重点是『Local Address (本地主机的IP与端口对应)』那个栏位,他代表的是本机所启动的网络服务! IP的部分说明的是该服务位於那个介面上,若为 127.0.0.1 则是仅针对本机开放,若是 0.0.0.0 或 ::: 则代表对整个 Internet 开放。 每个端口 (port) 都有其特定的网络服务,几个常见的 port 与相关网络服务的关系是:

  • 80: WWW
  • 22: ssh
  • 21: ftp
  • 25: mail
  • 111: RPC(远程程序呼叫)
  • 631: CUPS(列印服务功能)

假设我的主机有兴趣要侦测的是比较常见的 port 21, 22, 25及 80 时,那我如何透过 netstat 去侦测我的主机是否有开启这四个主要的网络服务端口呢?由於每个服务的关键字都是接在冒号『 : 』后面, 所以可以藉由撷取类似『 :80 』来侦测的!那我就可以简单的这样去写这个程序喔:

#!/bin/bash
echo "Now, I will detect your Linux server's services!"
echo -e "The www, ftp, ssh, and mail will be detect! \n"

testing=$(netstat -tuln | grep ":80 ") #侦测看80端口在否?
if [ "$testing" != "" ]; then
echo "WWW is running in your system."
fi

testing=$(netstat -tuln | grep ":22 ") #侦测看22端口在否?
if [ "$testing" != "" ]; then
echo "SSH is running in your system."
fi

testing=$(netstat -tuln | grep ":21 ") #侦测看21端口在否?
if [ "$testing" != "" ]; then
echo "FTP is running in your system."
fi

testing=$(netstat -tunlp | grep ":25 ")#侦测看25端口在否?
if [ "$testing" != "" ]; then
echo "Mail is running in your system."
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值