简体   繁体   English

Bash Output 未重定向到文件

[英]Bash Output not redirecting to file

I am writing a script to verify whether given cert is issued by my CA.The script takes the cert i wan to check as input.我正在编写一个脚本来验证给定的证书是否由我的 CA 颁发。脚本将我想要检查的证书作为输入。 Below shared code snippet is what i am using,下面是我正在使用的共享代码片段,

output=$(openssl verify -CAfile /home/Admin/CA/sign_CA.pem $1 2> error.txt)

if [[ $output == *"error"* ]];then
        echo "Certificate Verification Failed"
        exit 1
fi

As you can see, i am redirecting error to error.txt file.如您所见,我将错误重定向到error.txt文件。 Also output should be stored in the output variable. output 也应存储在output变量中。

When i pass a non-existing file, then i am getting the error printed in my screen.当我传递一个不存在的文件时,我会在屏幕上打印错误。 Also the regex is not working.正则表达式也不起作用。 I am not getting Certificate Verification Failed error message.我没有收到证书验证失败错误消息。

Output from my shell: Output 来自我的 shell:

Admin@Bionic-WorkBook:~/CA$./verify.sh /home/Admin/ad21.pem Admin@Bionic-WorkBook:~/CA$./verify.sh /home/Admin/ad21.pem

Can't open../ad21.pem for reading, No such file or directory无法打开../ad21.pem 进行阅读,没有这样的文件或目录
139691424915904:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r') 139691424915904:错误:02001002:系统库:fopen:没有这样的文件或目录:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
139691424915904:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79: unable to load certificate 139691424915904:错误:2006D080:BIO 例程:BIO_new_file:没有这样的文件:../crypto/bio/bss_file.c:79:无法加载证书

Contents of error.txt: error.txt 的内容:

Admin@Bionic-WorkBook:~/CA$cat output.txt Admin@Bionic-WorkBook:~/CA$cat output.txt

Can't open../ad21.pem for reading, No such file or directory无法打开../ad21.pem 进行阅读,没有这样的文件或目录
140041413374400:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r') 140041413374400:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
140041413374400:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79: unable to load certificate 140041413374400:错误:2006D080:BIO 例程:BIO_new_file:没有这样的文件:../crypto/bio/bss_file.c:79:无法加载证书

My Questions:我的问题:
i) How did error print in screen? i) 错误是如何在屏幕上打印的? As you can see error is redirected to file and output is to a variable.如您所见,错误被重定向到文件,output 被重定向到变量。 Then, How did the error print on screen?那么,错误是如何打印在屏幕上的?
i) Why did regex fail? i) 为什么正则表达式失败? As the output has the same error message, why did regex fail?由于 output 有同样的错误信息,为什么正则表达式失败?

You would find your answer to your doubts in this link:- https://superuser.com/a/935427您可以在此链接中找到您的疑问的答案:- https://superuser.com/a/935427

Here is TL;DR answer:-这是 TL;DR 答案:-

$(command) would store output of command in variable output for you. $(command)将为您将command的 output 存储在变量output中。

Ex.前任。

cat file.txt //all command's success failure can be check using $? variable.
echo $?

If you want to store error in output variable then just don't do redirect or use something like this (check the highlighted part in code block)如果你想在output变量中存储错误,那么不要做重定向或使用类似的东西(检查代码块中突出显示的部分)


output=$(openssl verify -CAfile /home/Admin/CA/sign_CA.pem $1 2> error.txt ; cat error.txt)
if [[ $output == *"error"* ]];then
        echo "Certificate Verification Failed"
        exit 1
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM