![](/img/trans.png)
[英]BASH - Comparing two files and redirecting the output to something readable
[英]Bash - comparing output of two commands
我有以下代碼:
#!/bin/bash
CMDA=$(curl -sI website.com/example.txt | grep Content-Length)
CMDB=$(curl -sI website.com/example.txt | grep Content-Length)
if [ "CMDA" == "CMDB" ];then
echo "equal";
else
echo "not equal";
fi
用這個輸出
root@abcd:/var/www/html# bash ayy.sh
not equal
應該是“等於”而不是“不等於”。 我做錯了什么?
Thnaks
您在那里忘記了變量CMDA
和CMDB
的$
。 這是您需要的:
if [ "$CMDA" = "$CMDB" ]; then
我也將==
運算符更改為=
,因為man test
只提到=
,而不是==
。
另外,您還有一些多余的分號。 整個事情有點干凈:
if [ "$CMDA" = "$CMDB" ]; then
echo "equal"
else
echo "not equal"
fi
您正在將字符串“ CMDA”與“ CMDB”進行比較,而應使用$來比較變量,例如$ {CMDA}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.