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



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

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

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 $3 }' | sed -n '1 p' | awk 'BEGIN{FS="x"} { print $2}' | awk 'BEGIN{FS="+"} {print $1}')
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"=下方]作品注意单个等号。

最新更新