bash - 为什么我在发送标准输出后有一个空文件
问题描述
使用 bash 我正在执行以下命令来过滤证书中的信息
openssl s_client -connect google.com:443 < /dev/null > cert.pem
openssl x509 -in cert.pem -noout -subject > commonName
tr "," "\n" < commonName | sed -nr '/CN/p' | tr -d ' /t' > commonName
最后一个命令将文件“commonName”留空,我想知道这是为什么。如果我改为附加文件“>>”,则会显示所需的过滤输出,但保留未过滤的内容。
将文件留空
tr "," "\n" < commonName | sed -nr '/CN/p' | tr -d ' /t' > commonName
有效,但内容不受欢迎
tr "," "\n" < commonName | sed -nr '/CN/p' | tr -d ' /t' >> commonName
编辑,可能会添加发送到具有新名称的文件按预期工作。例如,将“commonName”更改为“test”。
提前致谢!/R
解决方案
您不能使用其他命令编辑文件,并且只能在一个管道中使用 sed(您的操作方式)。您需要一个临时文件:
openssl s_client -connect google.com:443 < /dev/null > cert.pem
openssl x509 -in cert.pem -noout -subject > commonName
tr "," "\n" < commonName | sed -nr '/CN/p' | tr -d ' /t' > /tmp/temp
mv /tmp/temp commonName
还有一个更好的方法来实现整个脚本,没有临时文件:
openssl s_client -connect google.com:443 < /dev/null > cert.pem
openssl x509 -in cert.pem -noout -subject |
tr "," "\n" |
grep -o 'CN .*' > commonName
推荐阅读
- java - 在 java 编译器中标记行的问题
- python - 在 Python 中返回簇段的平均值
- javascript - 我该如何解决这个错误:SyntaxError: await is only valid in async function
- node.js - Nodejs MSSQL如何同时池化多个数据库
- python-3.x - 有没有办法设置可以放入 python 3 中的一组值?
- javascript - Discord.js 日志记录,如何通过嵌入将问题发送到特定频道?
- typescript - TypeScript 中的泛型和类型推断
- jenkins - 詹金斯登录后重定向回主页
- json - 如何在 django 函数中将单个对象作为 json 响应返回?
- python - 如何检查科学记数法是否存在 Python?