从终端或键盘快捷键调用的 Bash 脚本中的字符串比较给出不同的输出

String comparison in a Bash script called from terminal or keyboard shortcut gives different output

我有两个监视器,我正在调用 bash 脚本将当前 window 移动到上面的监视器。下面是我的代码示例(我将变量名称更改为更具描述性)。

问题出在最后一个 if 语句中 if [[ $currentWindowPosition == "above" ]]。当我在上面的监视器中从终端和我的 window 调用脚本时,我得到:

currentWindowPosition=above!
The window will stay on this monitor

但是当我从键盘快捷键调用它(并将输出重定向到文件)时,我得到:

currentWindowPosition=above!
The current monitor is on the bottom

怎么会?我也尝试了该测试的不同版本,但没有成功。提前谢谢你。

yOfCurrentWindow=$(xwininfo -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}') | grep "Absolute upper-left Y:" | cut -d: -f2)
yResolutionOfmonitor1=$(xrandr | awk '/ connected/ { print  }' | sed -n '1 p' | awk 'BEGIN{FS="x"} { print }' | awk 'BEGIN{FS="+"} {print }')

if [ $yOfCurrentWindow -lt $yResolutionOfmonitor1 ] 
then currentWindowPosition="above"
else currentWindowPosition="below"
fi

echo "currentWindowPosition=$currentWindowPosition!"

if [[ $currentWindowPosition == "above" ]] 
then echo "The window will stay on this monitor"
else echo "The current monitor is on the bottom"
fi

Jdamian 的使用建议: 如果 [ "$currentWindowPosition" = below ] 作品。注意单个等号。