linux - bash脚本: options - case: range of multiple numbers

标签 linux bash case command-line-arguments optional-arguments

我正在 Linux Bash 中编写一个脚本,可以使用不同类型的选项。 基本上,该程序将 ping 到给定的 IP 地址。

现在,我想让用户在终端中写入一系列 IP 地址,然后程序将对其执行 ping 操作。

Fe:bash pingscript 25 - 125

the script will then ping all the addresses between 192.168.1.25 and 192.168.1.125.

这并不难,我只需要写一个小案例

[0-9]-[0-9] ) ping (rest of code)

现在的问题是:这段代码只能让我 ping 通 fe 的号码。 0 - 9 而不是 10 - 25

为此我需要写: [0-9][0-9]-[0-9][0-9](fe:ping 25 - 50)

但是一侧可能有 1 个数字,另一侧有 2 个数字:[0-9]-[0-9][0-9] (fe: ping 1 - 25)

或:[0-9]-[0-9][0-9][0-9](fe:ping 1 - 125)

等等...这意味着有很多可能性。 可能还有另一种写法,但是怎么写呢?

我不想在参数中出现任何字母,但我不能以此开始(循环系统)。

最佳答案

怎么样:

for i in 192.168.1.{25..125}; do ping -qnc1 $i; done

或者在以变量作为参数的脚本中:

for i in $(seq -f "192.168.1.%g" $1 $2); do ping -qnc1 -W1 $i; done

其中第一个参数是开始位置的数字,第二个参数是结束位置的数字。像这样调用脚本:

./script 25 125

ping 选项:

  • -q:ping 不打印摘要
  • -n:无 DNS 查找
  • -c1:仅发送 1 个包裹
  • -W1:超时为 1 秒(可以适当增加)

关于linux - bash脚本: options - case: range of multiple numbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458222/

相关文章:

linux - Netbeans 菜单在 Ubuntu 中很糟糕——完全不可读而且看起来很糟糕。任何修复?

linux - 如果查询列符合条件,则取列的值 - linux

bash:减去前缀后的常数

sql - 什么更好,动态 SQL 还是 where case?

linux - 如何解决 "FATAL CONFIG FILE ERROR"?

linux - 为什么 i2cdetect 总是在我的嵌入式 Linux 中的 RTC 上给出 UU

c++ - 在 ubuntu 中使用/bin/bash 运行 grep 时,popen 错误重定向意外

bash - Jenkinsfile 添加 if else 脚本?

if-statement - Tableau - 返回具有 MAX 值的维度

使用日期的 MySQL CASE 语句