[英]Bash Script: options - case: range of multiple numbers
I'm working on a script in Linux Bash, with different kinds of options to use. 我正在使用各种不同的选项在Linux Bash中编写脚本。 Basically, the program is going to ping to the given ip-address.
基本上,程序将ping到给定的ip地址。
Now, I want to enable the user to write a range of ip-adresses in the terminal, which the program then will ping. 现在,我希望使用户能够在终端中编写一系列ip地址,然后程序将对其进行ping操作。
Fe: bash pingscript 25 - 125 铁:bash pingscript 25-125
the script will then ping all the addresses between 192.168.1.25 and 192.168.1.125.
然后脚本将对192.168.1.25和192.168.1.125之间的所有地址执行ping操作。
That's not to hard, I just need to write a little case with 这并不难,我只需要用
[0-9]-[0-9] ) ping (rest of code)
Now the problem is: this piece of code will only enable me to ping numbers of fe. 现在的问题是:这段代码只能让我ping fe。 0 - 9 and not 10 - 25 .
0-9,而不是10-25 。
For that I'd need to write: [0-9][0-9]-[0-9][0-9] (fe: ping 25 - 50) 为此,我需要写:[0-9] [0-9]-[0-9] [0-9](例如:ping 25-50)
But then there's the possibility of having 1 number on one side and 2 on the other: [0-9]-[0-9][0-9] (fe: ping 1 - 25)
但是,有可能一侧有1个数字,另一侧有2个数字:
[0-9]-[0-9][0-9] (fe: ping 1 - 25)
or: [0-9]-[0-9][0-9][0-9] (fe: ping 1 - 125)
或:
[0-9]-[0-9][0-9][0-9] (fe: ping 1 - 125)
and so on... That means there're a lot of possibilities. 等等...这意味着存在很多可能性。 There's probably another way to write it, but how?
可能还有另一种编写方式,但是怎么写呢?
I don't want any letters to be in the arguments, but I can't start with that (loop-through system). 我不希望在参数中包含任何字母,但是我不能以此开头(循环系统)。
How about that: 那个怎么样:
for i in 192.168.1.{25..125}; do ping -qnc1 $i; done
Or in a script with variables as arguments: 或在以变量为参数的脚本中:
for i in $(seq -f "192.168.1.%g" $1 $2); do ping -qnc1 -W1 $i; done
Where the first argument is the number where to begin and the second argument where to end. 第一个参数是从哪里开始的数字,第二个参数是从哪里结束的数字。 Call the script like this:
像这样调用脚本:
./script 25 125
The ping
options: ping
选项:
-q
: that ping doesn't print a summary -q
:ping不打印摘要 -n
: no dns lookups -n
:无dns查找 -c1
: Only send 1 package -c1
:仅发送1个包裹 -W1
: timeout to 1 second (can be increased of cource) -W1
:超时到1秒(可以增加次数) You can use extended pattern matching in your script by enabling the extglob
shell option: 您可以通过启用
extglob
shell选项在脚本中使用扩展模式匹配:
shopt -s extglob
So you can use braces with a +
quantifier like this: 因此,可以将括号与
+
量词一起使用,如下所示:
#!/bin/bash
shopt -s extglob
case $1 in
+([0-9])-+([0-9]) )
# do stuff
;;
esac
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.