我一直在努力想弄清楚如何将存储在变量中的命令的输出与Shell脚本中的另一个变量(也是字符串)的内容进行比较。 我知道它看起来像一个基本的RTFM案件,但我已经做了,我真的没有解决这个问题。如何将命令输出与Shell脚本中的字符串进行比较
所以,我有代码如下,涉及到Android(我使用的ADB工具),并与意见,以帮助理解:
# Using the ` char to execute the command and return the result as a string
# To store it in the variable
RES=`adb shell dumpsys power | grep "Display Power"`
# Store the string in the variable
EXPECTED='Display Power: state=OFF'
#Simple checks, both returning "Display Power: state=OFF" (without quotes) in the console
echo "$RES"
echo "$EXPECTED"
# Compare if the values of the variables, both strings, are equal
# If so, perform action
if [ "$EXPECTED" = "$RES" ]
then
echo "inside"
adb shell input keyevent 26
fi
的情况是,在弦如果比较从未像平等一样。
我认为这个错误是第一次赋值给变量RES,因为我可能没有正确理解'字符的意思,它返回的不是它看起来是什么。
我敢肯定,你们可以帮助我这里的基本情况。
非常感谢您的帮助
检查保存的stdout的确切内容。例如,有没有(还)在您的预期字符串中的尾随换行符? – mpez0
你能告诉我们你的“简单检查”中两个'echo'命令的结果吗?更好...'echo'> $ {RES} <“'和'echo”> $ {EXPECTED} <“' – mauro