2010-12-17 11 views
5

私はOSXでbashコマンドラインを使用しています。私はANSIのエスケープシーケンス\ 033 [21tは、現在のターミナルウィンドウのタイトルを取得することを知っています。ですから、例えば:ANSIエスケープシーケンスを使用してbashでターミナルウィンドウのタイトルを取得する方法は?

$ echo -ne "\033[21t" 
...sandbox... 
$ # Where "sandbox" is the title of the current terminal window 
$ # and the ... are some extra control characters 

は、私は何をしたいのは、スクリプトでプログラムこの情報をキャプチャですが、私はそれを行う方法を見つけ出すことはできません。スクリプトが生のANSIエスケープシーケンスだけをキャプチャします。だから、さらに、例えば、この小さなRubyスクリプト:

cmd = 'echo -ne "\033[21t"' 
puts "Output from echo (directly to terminal):" 
system(cmd) 
terminal_name=`#{cmd}` 
puts "\nOutput from echo to variable:" 
puts terminal_name.inspect 

は、次のような出力生成されます

Output from echo (directly to terminal): 
^[]lsandbox^[\ 
Output from echo to variable: 
"\e[21t" 

を私は、端末に表示される情報と一致するために、第2の場合には情報が欲しいが、代わりに私が得るのは生のコマンドシーケンスです。 (私はsystem()を使ってファイルへの出力をキャプチャしようとしましたが、うまくいきません)誰かがこれを動作させる方法を知っていますか?

答えて

6

詳しくは、hereとすると、それを動作させるには汚いトリックを使用する必要があります。ここで

は、変更したスクリプトです:

#!/bin/bash 
# based on a script from http://invisible-island.net/xterm/xterm.faq.html 
exec < /dev/tty 
oldstty=$(stty -g) 
stty raw -echo min 0 
# on my system, the following line can be replaced by the line below it 
echo -en "\033[21t" > /dev/tty 
read -r x 
stty $oldstty 
echo $x 
関連する問題