gpt4 book ai didi

linux - 如何将一行 Bash 脚本分隔成多行?

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:35 24 4
gpt4 key购买 nike

所以我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com