0

我正在尝试通过 shell 测试节能器启动和关闭设置,我的字符串比较总是响应没有

这是我的代码

#!/bin/sh

pmset repeat shutdown MTWRFSU 19:00:00 wakeorpoweron MTWRF 07:00:00

test=`pmset -g sched`
schd="Repeating power events: wakepoweron at 7:00AM weekdays only shutdown at 7:00PM every day"
echo $schd;
echo $test;
if [ '$test' = '$schd' ]; then 
    echo "yes"
else 
    echo "no"
fi

我在这里做错了什么?

添加了这个,它按预期返回是

test2="Repeating power events: wakepoweron at 7:00AM weekdays only shutdown at 7:00PM every day"
schd2="Repeating power events: wakepoweron at 7:00AM weekdays only shutdown at 7:00PM every day"

if [ "$test2" = "$schd2" ]; then 
    echo "yes"
else 
    echo "no"
fi

我怀疑我的测试命令的输出中存在隐藏字符,因为其他一切似乎都是为了有某种方法来确认和消除它们,如果这实际上是问题?

4

2 回答 2

2

变量引用不在单引号内解析;改用双引号:

if [ "$test" = "$schd" ]; then...

$schd另外,请注意,如果或$test被解析为空字符串,上述测试将不起作用。解决此问题的常见技巧如下:

if [ x"$test" = x"$schd" ]; then...
于 2013-07-03T17:39:12.963 回答
1

如果您认为 $test 可能有多余的字符,请尝试测试 $test 是否包含$schd:

case "$test" in
*"$schd"*) echo yes ;;
*) echo no ;;
esac

尝试改变

echo $schd;
echo $test;

echo "schd=>$schd<"
echo "test=>$test<"
于 2013-07-03T18:48:04.623 回答