2010-12-17 49 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" 

我想在第二種情況下的信息,以匹配在終端上顯示的信息,但而我所得到的只是原始的命令序列。 (我嘗試過使用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 
相關問題