0

我正在编写一个简单的 C 程序,firefox如果没有firefox打开窗口,它将启动,否则firefox窗口将被聚焦。我xdotool在 shell 脚本中使用来执行此操作并在我的 C 程序中执行 shell 脚本。

这是C程序的关键部分:

char *path = "/home/simon/F.sh";
execl("/bin/bash", "bash", path, (char*)0);

文件F.sh如下:

winId=$(xdotool search --onlyvisible --class 'firefox')
echo $winId >> /home/simon/log #used to debug
if [ "$winId" ]; then
  for i in $winId; 
    xdotool windowactivate $i
  done
else
  firefox
fi

但是,当我运行 C 程序时,它有时会按预期工作,无论是否firefox已打开。但有时当firefox它已经打开并且我运行程序时,它会打开一个新窗口firefox并将空行回显到/home/simon/log.

我认为这不是 shell 脚本的问题,也不是xdotool. 因为每次我直接在终端中运行 shell 脚本,或者bash /home/simon/F.sh在终端中运行时,它总是给我正确的响应。我怀疑这是执行环境的问题。谁能给我解释一下这种现象?

#更新

这个问题通过查看xdotool.

4

0 回答 0