加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Bash多次写入文件

发布时间:2020-12-15 21:25:26 所属栏目:安全 来源:网络整理
导读:我正在创建一个bash脚本来自动执行某些命令,我??在将错误检查写入同一文件时遇到了一些麻烦. #!/bin/bashtouch ErrorLog.txtbro-cut service conn.log | sort | uniq -c ProtocolHierarchy.txtif [ $? -eq 0 ]; then echo -e "OK Protocol Hierarchy Created
我正在创建一个bash脚本来自动执行某些命令,我??在将错误检查写入同一文件时遇到了一些麻烦.

#!/bin/bash

touch ErrorLog.txt

bro-cut service < conn.log | sort | uniq -c > ProtocolHierarchy.txt
if [ $? -eq 0 ]; then
    echo -e "OK Protocol Hierarchy Createdn" > ErrorLog.txt
else
    echo -e "FAILED Creating Protocol Hierarchyn" > ErrorLog.txt
fi

bro-cut id.orig_h < dns.log | sort | uniq -c > AllIPAddresses.txt

if [ $? -eq 0 ]; then
    echo -e "OK Created all IP Addressesn" > ErrorLog.txt
else
    echo -e "FAILED Creating all IP Addressesn" > ErrorLog.txt
fi

目标是拥有一个我可以打开的文件,看到所有命令都工作或失败,目前文件看起来像这样

-e OK Created all IP Addresses

当我希望它看起来像这样

OK Protocol Hierarchy Created
OK Created all IP Addresses

我真的很喜欢bash脚本,所以任何提示都将不胜感激!

解决方法

打开一次,并多次写入该文件描述符.

# Open (creating or truncating) the output file (only once!)
exec 3>ErrorLog.txt

# Write a line to that already-open file
echo "something" >&3

# Write a second line to that already-open file
echo "something else" >&3

# Optional: close the output file (can also be implicit when the script exits)
exec 3>&-

另一个常见的习惯是使用>>以附加模式打开,但每行执行一次效率要低得多.

# Open ErrorLog.txt,truncating if it exist,write one line,and close it
echo "something" >ErrorLog.txt

# Reopen ErrorLog.txt,write an additional line to the end,and close it again
echo "something else" >>ErrorLog.txt

将此练习用于您的脚本(并进行其他一些最佳实践改进)如下所示:

#!/bin/bash

# not related to file output,but to making sure we detect errors
# only works correctly if run with bash,not sh!
set -o pipefail ## set exit status based on whole pipeline,not just last command

# picking 3,since FD numbers 0-2 are reserved for stdin/stdout/stderr
exec 3>ErrorLog.txt

if bro-cut service <conn.log | sort | uniq -c >ProtocolHierarchy.txt; then
    echo "OK Protocol Hierarchy Created" >&3
else
    echo "FAILED Creating Protocol Hierarchy" >&3
fi

if bro-cut id.orig_h <dns.log | sort | uniq -c >AllIPAddresses.txt; then
    echo "OK Created all IP Addresses" >&3
else
    echo "FAILED Creating all IP Addresses" >&3
fi

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读