简体   繁体   English

使用大小写在Linux脚本中Ping

[英]Ping in linux script with case

In Linux I use a script to ping a couple of addresses. 在Linux中,我使用脚本来ping几个地址。 Now I want that the user can do: bash Script 50-60 And that the script pings the addresses 192.168.100.50 - 192.168.100.60 I use xx-yy to do this and I tried but the part after the shift is wrong. 现在,我希望用户可以执行以下操作: bash Script 50-60且该脚本ping地址192.168.100.50-192.168.100.60,我使用xx-yy做到了这一点,我尝试过,但是shift后的部分是错误的。

for i in $*;
do
case $* in
 "XX-YY" ) shift;
           ping -c 1 192.168.0.$1 - $i
done

Script: 脚本:

#!/bin/bash
start=${1%-*}
end=${1#*-}
for ((i=start;i<=end;i++));do
    ping -c 1 192.168.0.$i
done

Usage: 用法:

bash path_to_the_Script 50-60

A little explanation: 一点解释:

I used bash parameter expansion to extract start and end from the input string 50-60 . 我使用bash参数扩展从输入字符串50-60提取startend

${1#*-} is the string after the first match of a - in $1 ${1#*-}$1第一个-匹配后的字符串

and

${1%-*} is the string before the last match (first match from right) of a - in $1 ${1%-*}是的最后一场比赛(右起第一场比赛)之前的字符串-$1

您可以尝试以下逻辑:ping 192.168.100。{50..60}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM