gpt4 book ai didi

bash - `xdotool type ` - 随机重复字符

转载 作者:行者123 更新时间:2023-11-29 09:13:25 26 4
gpt4 key购买 nike

我正在编写一个脚本来自动准备工作环境。我需要打开 4 个终端窗口,排列它们并在每个窗口中执行命令。

它有效,但有时我遇到严重的失败 - xdotool type 随机重复一些字符:

rvm use ruby-1.99999999999999999999999999999999.3-p194@ro && rails c
~/my_src/ruby_apps/ro > rvm use ruby-1.99999999999999999999999999999999.3-p194@ro && rails c
ruby-1.99999999999999999999999999999999.3-p194 is not installed.
To install do: 'rvm install ruby-1.99999999999999999999999999999999.3-p194'
~/my_src/ruby_apps/ro >

或在另一个窗口中:

tail -fn 100 looooooog/thin.0.log
~/my_src/ruby_apps/ro > tail -fn 100 looooooog/thin.0.log
tail: could not open «looooooog/thin.0.log» for reading: No such file or directory
tail: no more files
~/my_src/ruby_apps/ro >

我猜这取决于 CPU 负载,因为我有非常大的 .bashrc 由 ATOM 处理,并且在脚本处理期间它的负载很高。

我在脚本中使用 waitsleep 以及 open_lxterminal_execute_hold() 函数调用的特殊顺序来首先执行简单的简单命令。这最大限度地减少了错误,但根本无法避免错误。

无论 CPU 负载如何(无论如何),您会建议什么来获得稳定的结果?如果能去掉 sleep 也很棒。

#!/bin/bash
#
# prepares work environment for rails project

# Opens lxterminal with title if windows with such title
# doesn't exist, executes command and stays open.
# Otherwise does nothing.
#
function open_lxterminal_execute_hold(){
local winid=`xwininfo -name $title 2>/dev/null |grep 'Window id:' |cut -d" " -f4`
if [ -n "$winid" ]; then
echo "Window for title '$title' exists with '$winid'"
else
lxterminal -t $title
sleep 1
wmctrl -i -a "$winid" # bring the window to front, activate
wait
xdotool type "$command"
wait
xdotool key Return # run the command
wait
fi
}

pkill devilspie
cd ~/my_src/ruby_apps/ro # TODO param
title='rails-commandline'; command='ls'; open_lxterminal_execute_hold
title='rails-development.log'; command='tail -fn 100 log/development.log'; open_lxterminal_execute_hold
title='rails-user_case'; command='tail -fn 100 log/thin.0.log'; open_lxterminal_execute_hold
sleep 5
title='rails-console'; command='rvm use ruby-1.9.3-p194@ro && rails c'; open_lxterminal_execute_hold
/usr/bin/devilspie -a 2>/dev/null & # arrange windows

更新如何防止 xdotool 重复字符?

最佳答案

这是另一个解决方案,已在我的 Ubuntu 11.04 系统上测试和运行:

#!/bin/bash

function open_lxterminal_execute_hold() {
xwininfo -name $1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Window for title '$1' already exists"
else
t=$(tempfile)
echo ". ~/.bashrc" > $t
echo "$2" >> $t
lxterminal -t $1 -e "$SHELL --rcfile $t" &
fi
}

#pkill devilspie
#cd ~/my_src/ruby_apps/ro
open_lxterminal_execute_hold 'rails-commandline' 'ls'
open_lxterminal_execute_hold 'rails-development' 'tail -fn 100 log/development.log'
open_lxterminal_execute_hold 'rails-user_case' 'tail -fn 100 log/thin.0.log'
#open_lxterminal_execute_hold 'rails-console' 'rvm use ruby-1.9.3-pl94@ro && rails c'
#devilspie -a 2>/dev/null &

您可能会注意到,我已经注释了一些用于测试的行,因此在您的系统上运行脚本之前,您应该从注释行中删除尾随的“#”。
我使用的技巧是在每个终端中启动一个带有“--rcfile”选项的新 shell。
这样,就不需要使用“xdotool”、“wait”和“sleep”命令了。

关于bash - `xdotool type ` - 随机重复字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11401520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com