4

作業環境の準備を自動化するスクリプトを書いています。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 負荷に依存していると思います。ATOM で処理される .bashrc が非常に大きく、スクリプト処理中に負荷が高くなるためです。

単純な単純なコマンドを最初に実行するために、スクリプト内でwaitandsleepと特別な関数呼び出しの順序を使用します。open_lxterminal_execute_hold()これによりエラーは最小限に抑えられますが、まったく防止されません。

CPU負荷に関係なく安定した結果を得るために何を提案しますか? sleeps も取り除くのは素晴らしいことです。

#!/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 の繰り返し文字を防ぐにはどうすればよいですか?

4

4 に答える 4

2

xdotool を完全に回避する回避策を次に示します。良い面としては、かなり単純だと思います。マイナス面としては、lxterminal では動作しないように見えますが、xterm や gnome-terminal などの他の端末では動作します。

価値があるのは、次のアイデアです。

title環境変数の値に基づいて、bashrc に適切なコマンドを実行させます。

これを行うには、次のようなものを .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 &

何らかの理由で、lxterminal は の最初の値のみを使用しtitle、その後の値の変更を無視しているようです。

于 2012-07-20T13:13:57.067 に答える
2

これは、私の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」オプションを使用して新しいシェルを開始することです。
この方法では、「xdotool」、「wait」、および「sleep」コマンドを使用する必要はありません。

于 2012-07-24T20:40:03.460 に答える
0

実際、私は別の解決策を見つけました。ubuntu deb パッケージの 2009 年の古いバージョンを使用していることを発見しました。ソースからインストールされた Iの最新バージョンでxdotoolは、新しいコマンド ライン パラメータ--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
}

がんばれ!

于 2012-08-02T18:05:20.183 に答える