简体   繁体   中英

Bash - comparing output of two commands

I have this code:

#!/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

with this output

root@abcd:/var/www/html# bash ayy.sh
not equal

which should be "equal" instead of "not equal". What did I do wrong?

Thnaks

You forgot the $ for the variables CMDA and CMDB there. This is what you need:

if [ "$CMDA" = "$CMDB" ]; then

I also changed the == operator to = , because man test only mentions = , and not == .

Also, you have some redundant semicolons. The whole thing a bit cleaner:

if [ "$CMDA" = "$CMDB" ]; then
  echo "equal"
else
  echo "not equal"
fi

您正在将字符串“ CMDA”与“ CMDB”进行比较,而应使用$来比较变量,例如$ {CMDA}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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