简体   繁体   English

Bash-比较两个命令的输出

[英]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 Thnaks

You forgot the $ for the variables CMDA and CMDB there. 您在那里忘记了变量CMDACMDB$ This is what you need: 这是您需要的:

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

I also changed the == operator to = , because man test only mentions = , and not == . 我也将==运算符更改为= ,因为man test只提到= ,而不是==

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}

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

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