【发布时间】:2011-05-27 03:45:26
【问题描述】:
我在 OSX 中使用 bash 命令行。我知道 ANSI 转义序列 \033[21t 将检索当前终端窗口的标题。所以,例如:
$ 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() 并将输出捕获到文件中——这也不起作用。)有人知道如何让它工作吗?
【问题讨论】: