【问题标题】:Save Bash script output to text file with conditions将 Bash 脚本输出保存到带有条件的文本文件
【发布时间】:2016-02-03 10:15:43
【问题描述】:

我正在使用this 脚本检查我拥有的 ips 列表,看看它们是否在垃圾邮件阻止列表中。

auto.sh:

while read ip ; do
    ./blacklist.sh $ip
done < block.txt

blacklist.sh 是上面的链接脚本。 block.txt 一次列出我的每个 ips 一行(我有几个 /22)。

被阻止的 ip 扫描的典型输出如下所示:

Warning: PTR lookup failed
b.barracudacentral.org : 127.0.0.2
bb.barracudacentral.org : 127.0.0.2
black.junkemailfilter.com : 127.0.0.2
cbl.abuseat.org : 127.0.0.2
cidr.bl.mcafee.com : 127.0.0.4
dnsbl.justspam.org : 127.0.0.2
hostkarma.junkemailfilter.com : 127.0.0.2

----------------------------------------------------------
Results for <my ip>

Tested:        117
Passed:        110
Invalid:       0
Blacklisted:   7
----------------------------------------------------------

当上面的文本没有说“黑名单:0”时,我想做的是让脚本将输出输出到文件中。

我不知道如何解决这个问题,这行得通吗?

sudo ./auto.sh "列入黑名单的条件:> 0" >> 12.txt

感谢您的帮助

【问题讨论】:

    标签: bash


    【解决方案1】:

    将输出放在一个临时文件中,然后 然后检查它的内容:

    ./auto.sh > 12_temp.txt
    grep -q 'Blacklisted:[ \t]*0$' 12_temp.txt || cat 12_temp.txt >> 12.txt
    rm -f 12_temp.txt
    

    【讨论】:

    • 您需要将结果存储在某处,以便检查其输出。除了临时文件,您还可以使用 shell 变量 - 只是更复杂。
    • Thanks 几乎完全符合我的要求。只有我必须在 rm -r 之前添加双管。像这样: ./auto.sh > 12_temp.txt grep -q 'Blacklisted:[ \t]*0$' 12_temp.txt ||猫 12_temp.txt >> 12.txt || rm -f 12_temp.txt。否则我得到'cat:无效选项-'f'尝试'cat --help'以获取更多信息。错误,12.txt 文件为空。
    • 不幸的是,在以文本文件形式打开时存在格式问题。当我'cat 12.txt' 与我的问题中的上述示例完全相同但当我在文本编辑器上打开它时,它看起来像这样gist.githubusercontent.com/pavs/83ea142c8cfecd882f9c/raw/… 我认为这可能是由于原始脚本中的格式处理方式:github.com/IntellexApps/blcheck/blob/master/blcheck#L458 在 php 中打开文件时我能做些什么来修复输出?
    • 找到解决方案 -p 以纯文本格式提供格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多