linux - 使用 Bash 脚本从 xdotool 搜索结果中检索单个元素

标签 linux bash ubuntu sh

使用 bash 脚本时遇到问题。我不知道如何从 xdotool 搜索功能中获取各个 pid。

代码

google-chrome --app=https://google.com &
google-chrome --app=https://google.com &
google-chrome --app=https://google.com &
google-chrome --app=https://google.com &

sleep 5

pids=$(xdotool search --onlyvisible --name google)
echo $pids

width=1920
height=1080

for i in 0 1 2 3;
  do
    x=$((((i)/2)*$width))
    y=$(((i%2)*$height))

    echo $x
    echo $y
    echo "${pids[$i]}"

    #xdotool windowmove ${pids[i]} $x $y
  done

输出
46137345 46137352 46137355 46137358
0
0
46137345
46137352
46137355
46137358
0
1080

1920
0

1920
1080

我看不到 /n在我认为正在做的字符串中,所以不知道它为什么要创建新行。

我对 bash 脚本非常陌生,所以我毫不怀疑这是显而易见的愚蠢行为。

我正在使用 bash 版本 5.0.3

最佳答案

问题是您的 pids变量只是一个字符串,但您正试图将它视为一个数组。

使用一组外部括号使 pids 成为一个数组,如下所示:

pids=($(xdotool search --onlyvisible --name google))

$ for i in "${pids[@]}"; do echo $i; done
46137345
46137352
46137355
46137358

关于linux - 使用 Bash 脚本从 xdotool 搜索结果中检索单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61810584/

相关文章:

linux - 为什么 cron 执行的部分脚本会失败,除非将 stderr 定向到/dev/null?

linux - Bash脚本为所有文件添加一个随机数

python - 如何从键值对列表中删除值

shell - 获取发送到电子邮件的回显 cron 文本

ubuntu - oracle java8中的格式错误的证书

iphone - 我有一个在 MAC OS 上使用 XCode 开发的项目,我如何在 Linux 上构建它?

linux - 在进程中使用互斥量/信号量

c++ - inotify 和 epoll 的区别

bash - 如何解压缩文件夹中的所有 .gz 文件并将它们组合成一个新文件而不为每个 .gz 文件生成未压缩的文件

python - 在 headless Ubuntu 14.04 服务器上使用 matplotlib