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

String 从终端或键盘快捷键调用的Bash脚本中的字符串比较给出不同的输出,string,bash,comparison,String,Bash,Comparison,我有两个监视器,我正在调用bash脚本将当前窗口移动到上面的监视器。下面是我的代码示例(我将变量名更改为更具描述性) 问题出现在最后一个if语句if[$currentWindowPosition==“over”]]中。当我从终端和上面监视器中的我的窗口调用脚本时,我得到: currentWindowPosition=above! The window will stay on this monitor 但当我通过键盘快捷键调用它(并将输出重定向到文件)时,我得到: 为什么?我也尝试了不同版本的

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

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

currentWindowPosition=above!
The window will stay on this monitor
但当我通过键盘快捷键调用它(并将输出重定向到文件)时,我得到:

为什么?我也尝试了不同版本的测试,但没有成功。先谢谢你

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”=低于]
作品请注意单个等号

如果第一条
if
语句中的测试为false,如何确保
$currentWindowPosition
中的值不是“高于”值?(顺便说一句,对于bash,您可以使用
if((yofcurrentwindown而不是旧式的测试命令(
[
)命令。)请在
if[[“$currentWindowPosition”==“over”]
中使用双引号。而不是在
中使用双引号
是不需要的。扩展第一个IF-THEN子句:
IF[“$yOfCurrentWindow”-lt“$yResolutionOfmonitor1”]然后currentWindowPosition=“Upper”else currentWindowPosition=“Down”fi
嗨,谢谢你的回答!我的代码比这更复杂,我认为没有必要包括这一行:else currentWindowPosition=“below”现在我这么做是因为我发现它会引起混乱。我尝试了Jdamian的if[“$currentWindowPosition”=below]并且有效!注意一个等号。你能解释一下这里发生了什么吗?$currWindowPosition已经有了“below”的值,但是如果从键盘快捷键调用[$currPos==“below”]时为false。
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