所以我在 bash 终端中创建了这个 ping 扫描,但我想为此制作一个外观整洁的脚本文件:
for IPs in 192.168.0.{1..254}; do ping -c1 -W1 $IPs; done | grep -B1 "1 received" | grep "192.168.0" | cut -d " " -f2 > BashPingSweep.txt
我认为我的 for 循环是正确的,但我无法将 for 循环通过管道传输到其他 greps 并剪切然后输出。这就是我现在拥有的:
#!/bin/bash
for IPs in 192.168.0.{1..254}
do
ping -c1 -W1 $IPs
done
grep -B1 "1 received"
grep "192.168.0"
cut -d " " -f2
> BashPingSweep.txt
最佳答案
你可以试试这个:
#!/bin/bash
for IP in 192.168.0.{1..254}
do
ping -c1 -W1 $IP
done |
grep -B1 "1 received" |
grep "192.168.0" |
cut -d " " -f2 \
> BashPingSweep.txt
它看起来有点笨拙,但这是在多行上格式化长管道的常用方法。你也可以这样说:
for IP in 192.168.0.{1..254}
do
ping -c1 -W1 $IP
done \
| grep -B1 "1 received" \
| grep "192.168.0" \
| cut -d " " -f2 \
> BashPingSweep.txt
这是我更喜欢的,因为它更容易看到管道的去向。
关于linux - 如何将一行 Bash 脚本分隔成多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484338/