码迷,mamicode.com
首页 > 其他好文 > 详细

if语句多条件判断

时间:2017-04-06 01:52:15      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:linux   shell   if语句   


想要编写一个简单的ping测试脚本,由用户输入起始和中指IP地址。其中需要判断用户输入的IP地址必须是0-255的访问,使用if语句进行条件判断如下:


if [ "${beginum}" -lt 0 ] || [ "${beginnum}" -gt 255 ] || [  "${endnum}" -lt 0 ] || [  "${endnum}" -gt 255 ]


运行的时候报错,经过几次尝试才知道这种if [ 条件1 || 条件2 ]格式只能支持最多2个条件。

if [[ 条件1 || 条件2 || 条件3 || 条件N ]]

使用这种[[ ]]双重中括号的形式能够支持多个条件



脚本如下:(还有多处未完善。。。)


#!/bin/bash
#Description
#       this program is for "for" practice
#Histroy
#2017/4/5

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gaga/.local/bin:/home/gaga/bin
export PATH

if [[ "$2" -lt 0 || "$2" -gt 255 ]];then
        echo "begin num should bettween(0-255)" && exit 1
fi

if [[ "$3" -lt 0 || "$3" -gt 255 ]];then
        echo "end num should bettween(0-255)" && exit 2
fi

for nethost in $(seq $2 $3)
do
        ping -c 3 -w 1 $1.${nethost} >/dev/null 2>&1 && statu=0 || statu=1
        if [ "${status}" == "1" ];then
                echo "$1.${nethost} is up!"
        else
                 
                echo "$1.${nethost} is down!"
        fi
done
exit 0

本文出自 “7995400” 博客,请务必保留此出处http://8005400.blog.51cto.com/7995400/1913186

if语句多条件判断

标签:linux   shell   if语句   

原文地址:http://8005400.blog.51cto.com/7995400/1913186

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!