【发布时间】:2011-08-29 03:19:25
【问题描述】:
我正在尝试编写一个简单的小脚本来查询 3g 连接,如果连接断开,则发起重新连接。
我的问题是检查命令的输出 - 两个看似相等的字符串没有被评估为相等。我敢肯定这里的某个地方有一个菜鸟错误!
#!/bin/bash
echo "Checking connection"
a="Not connected."
b=$(./sakis3g status --console)
if [[ "$a"!="$b" ]]; then
echo "Strings not equal:"
echo "$a"
echo "$b"
else
echo "Strings equal!!"
fi
运行时的输出:
user@mypc:~$ ./test_3g.sh
Checking connection
Strings not equal:
Not connected.
Not connected.
运行./test_3g.sh | cat -A时:
user@mypc:~$ ./test_3g.sh | cat -A
Checking connection$
Strings not equal:$
Not connected.$
Not connected.$
【问题讨论】:
-
最后没有多余的换行符吧?
-
试试
./test_3g.sh | cat -A。如果您的cat不支持-A选项,请尝试./test_3g.sh | cat -v。 -
我不认为有任何多余的空格,但很乐意对此进行更正。 @Keith,我已将管道的输出添加到 cat -A 到问题中。
标签: bash shell logical-operators