[英]Bash Output not redirecting to file
我正在編寫一個腳本來驗證給定的證書是否由我的 CA 頒發。腳本將我想要檢查的證書作為輸入。 下面是我正在使用的共享代碼片段,
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
如您所見,我將錯誤重定向到error.txt文件。 output 也應存儲在output變量中。
當我傳遞一個不存在的文件時,我會在屏幕上打印錯誤。 正則表達式也不起作用。 我沒有收到證書驗證失敗錯誤消息。
Output 來自我的 shell:
Admin@Bionic-WorkBook:~/CA$./verify.sh /home/Admin/ad21.pem
無法打開../ad21.pem 進行閱讀,沒有這樣的文件或目錄
139691424915904:錯誤:02001002:系統庫:fopen:沒有這樣的文件或目錄:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
139691424915904:錯誤:2006D080:BIO 例程:BIO_new_file:沒有這樣的文件:../crypto/bio/bss_file.c:79:無法加載證書
error.txt 的內容:
Admin@Bionic-WorkBook:~/CA$cat output.txt
無法打開../ad21.pem 進行閱讀,沒有這樣的文件或目錄
140041413374400:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
140041413374400:錯誤:2006D080:BIO 例程:BIO_new_file:沒有這樣的文件:../crypto/bio/bss_file.c:79:無法加載證書
我的問題:
i) 錯誤是如何在屏幕上打印的? 如您所見,錯誤被重定向到文件,output 被重定向到變量。 那么,錯誤是如何打印在屏幕上的?
i) 為什么正則表達式失敗? 由於 output 有同樣的錯誤信息,為什么正則表達式失敗?
您可以在此鏈接中找到您的疑問的答案:- https://superuser.com/a/935427
這是 TL;DR 答案:-
$(command)
將為您將command
的 output 存儲在變量output
中。
前任。
cat file.txt //all command's success failure can be check using $? variable.
echo $?
如果你想在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.