从 Azure Pipeline 运行的 Bash 脚本中的比较始终返回 false

问题描述 投票:0回答:1

我正在运行一个脚本,需要比较两个字符串。它在本地正确运行,但如果我通过 Azure DevOPs 中的 Azure-Pipeline 运行,脚本会运行,但我的 if 语句始终会经过 else 部分。

例如此文件中的脚本:

#############################################
teststr="test"
echo $teststr
if [[ $teststr == "test" ]]; then
      echo "true"
else
      echo "false"
fi
    
if [[ $teststr != "test" ]]; then
      echo "true"
else
      echo "false"
fi
#############################################

应该输出

test
true
false

当我在命令行中运行它时,它会发生 但是当在像这样的天蓝色管道中运行时:

az vm run-command invoke --command-id RunShellScript --name MyVM -g Takeoffs --scripts "cd /home/user/workdir/ && 
    ./script.sh" 

它输出

test
false
false

如何让它正确比较字符串?

我尝试比较不同的字符串,添加引号,我尝试了 -eq 和 -ne 运算符。 但由于它在命令行中工作,我认为语法是正确的,也许存在环境问题?

bash azure-pipelines comparison
1个回答
0
投票

问题在于它实际上并不是用 bash 运行的。 原答案如下:

azure 实际上是使用 bash 运行脚本吗?获得所述输出的最可能的方法是运行不支持 [[ 的 shell,例如,我可以在破折号中复制。尝试将 shebang 添加到脚本 (#!/bin/bash) 和/或直接调用 bash - bash ./script.sh 与简单的 ./script.sh

© www.soinside.com 2019 - 2024. All rights reserved.