作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何将可选参数传递给 bash 脚本以替换脚本中的现有变量?例如:
#!/bin/bash
#hostfinder.sh
#Find hosts in current /24 network
subnet=$(hostname -i | cut -d. -f1,2,3)
for j in \
$(for i in $subnet.{1..255}; do host $i; done | grep -v not | cut -d" " -f5)
do ping -c1 -w 1 $j; done | grep -i from | cut -d" " -f3,4,5 | tr ':' ' ' | \
sed -e 's/from/Alive:/'
这将获取当前主机的 IP,对可能的邻居运行反向查找,对它找到的任何主机名进行 ping 测试,并显示类似于此的输出:
Alive: host1.domain (10.64.17.23)
Alive: host2.domain (10.64.17.24)
...
叫我疯了,但它比 nmap 快得多,而且会生成一个不错的列表。
无论如何,在执行脚本时,我想选择性地将任何 C 类网络地址的前三个八位字节作为 $1 参数传递给 $subnet 变量。例如:
./hostfinder.sh 10.20.0
我的第一个想法是尝试类似 $subnet=$1 的方法,但我认为这不会起作用。我对重写脚本以使其更优雅或其他任何东西并不感兴趣,我主要只是对我在主题行中输入的内容感到好奇。
最佳答案
如何替换:
subnet=$(hostname -i | cut -d. -f1,2,3)
与:
case $# in
0) subnet=$(hostname -i | cut -d. -f1,2,3);;
1) subnet="${1}";;
*) echo "To many arguments" >&2; exit 1;;
esac
$#
是命令行参数个数getopt
优雅,但易于理解和扩展。关于linux - 在 bash 脚本中替换变量的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381602/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!