我正在编写一个脚本来自动化工作环境的准备。我需要打开4个终端窗口,安排它们并在每个窗口中执行命令。
这是可行的,但有时I会失败-- 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被原子处理,而且它的负载在脚本处理过程中很高。
我首先使用wait和sleep以及脚本中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重复循环?
发布于 2012-07-24 20:40:03
下面是另一个解决方案,在我的Ubuntu11.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“、”等等等“和”睡眠“命令。
发布于 2012-07-20 13:13:57
这里有一个完全避免xdotool的解决方法。在积极方面,我认为这是相当简单的。负面的一面是,它似乎不适用于as终端--尽管它也适用于其他终端,比如xterm和gnome终端。
不管它的价值是什么,以下是一个想法:
让bashrc根据title环境变量的值运行适当的命令:
您可以通过在.bashrc中添加如下内容来做到这一点:
case "$title" in
rails-development.log)
ls
;;
rails-commandline)
tail -fn 100 /var/log/syslog
;;
*)
;;
esac然后在您的脚本中可以使用
function open_terminal_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
gnome-terminal -t "$title" &
fi
}
cd /tmp # TODO param
export title='rails-commandline' && open_terminal_execute_hold &
export title='rails-blah' && open_terminal_execute_hold &
export title='rails-development.log' && open_terminal_execute_hold &由于某种原因,seems似乎只对title使用第一个值,而忽略了对其值的后续更改。
发布于 2012-08-02 18:05:20
实际上我找到了更多的溶液。我发现我使用了一个旧版本的2009年从ubuntu包。从源代码安装的xdotool I的最新版本允许一个新的命令行param --delay <value>,如果与0 --delay 0一起使用,可以防止重复字符。因此,该函数现在看起来如下:
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"
xdotool type --delay 0 "$command"
xdotool key Return
fi
}祝各位好运!
https://stackoverflow.com/questions/11401520
复制相似问题