如何让 tclsh 忽略 EOF?
在 csh 中你可以这样做
set ignoreeof
,或者在 bash 中你可以这样做
export ignoreeof=1
,这将使 csh/bash 忽略 EOF,即它不会在 Ctrl+D 上退出,或者当它到达末尾或文件时。
有没有办法用 tclsh 做同样的事情?
有没有办法让 tclsh 在到达文件末尾时不退出?
At csh you can do
set ignoreeof
or at bash tou can do
export ignoreeof=1
and this will make csh/bash to ignore EOF, i.e. it will not exit on Ctrl+D, or when it reaches the end or file.
Is there a way to make the same with tclsh ?
Is there a way to make tclsh not to exit when it reaches the end of file ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 tclsh 正在运行脚本,它不会在检测到 stdin 上的 EOF 时退出;这纯粹是内置 REPL 的一个功能。您可以使用
eof stdin
自己检测这种情况,此时您可以决定如何处理它。如果您想让 Ctrl+D 不是 EOF,那么最简单的方法是将终端置于原始模式,如下所示
: ,像这样切换回来:
确保在程序退出之前切换回来!
另一件事是,如果您使用原始输入,则必须自己处理所有行编辑。一个方便的方法是使用纯 Tcl readline-alike 系统例如来自 Tcler's Wiki 的示例。您可能需要对其进行一些调整,以使 Ctrl+D 执行您想要的操作。
另一种方法是这样做,使事物处于煮熟模式,并使 Ctrl+D 非特殊(在 OSX 上测试):
同样,您需要将事物重新设置为exit,事实上它一点也不特别,可能会在其他地方引起问题;经过上面那一招之后,就只是一个普通的角色了。
If tclsh is running a script, it doesn't exit on detecting an EOF on stdin; that's purely a feature of the built-in REPL. You can detect such a condition yourself using
eof stdin
, at which point you can decide what to do about it.If you're wanting to make a Ctrl+D not be EOF, then your easiest method is to put the terminal into raw mode, like this:
When you're done, switch back like this:
Make sure you switch back before the program exits!
The other thing is that if you're working with raw input, you've got to handle all line editing yourself. A convenient way to do this is to use a pure Tcl readline-alike system such as this example from the Tcler's Wiki. You might need to adapt it a bit to make Ctrl+D do what you want.
An alternative is to do this which leaves things in cooked mode and just makes Ctrl+D non-special (tested on OSX):
Again, you need to set things back on exit, and the fact that it's not special at all might cause problems elsewhere; after that trick above, it's just a normal character.
您可能希望:将其另存为 tclsh.exp
然后运行 tclsh.exp somefile.tcl arg arg ...
You might want Expect: save this as tclsh.exp
then run
tclsh.exp somefile.tcl arg arg ...
您也许可以使用 TclX 中的
trap
命令。You might be able to use the
trap
command from TclX.tclsh 中没有这样的内置功能。
There is no such built-in feature in tclsh.