我在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
Run Code Online (Sandbox Code Playgroud)
我想要做的是在脚本中以编程方式捕获此信息,但我无法弄清楚如何做到这一点.脚本只捕获原始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
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
Output from echo (directly to terminal):
^[]lsandbox^[\
Output from echo to variable:
"\e[21t"
Run Code Online (Sandbox Code Playgroud)
我希望第二种情况下的信息与终端上显示的信息相匹配,但我得到的只是原始命令序列.(我已尝试使用system()并将输出捕获到文件中 - 这也不起作用.)有没有人知道如何使其工作?
这是一个修改过的脚本:
#!/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
Run Code Online (Sandbox Code Playgroud)