使用pexpect/bash时防止换行 [英] Preventing linewrap when using pexpect / bash

查看:84
本文介绍了使用pexpect/bash时防止换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行以下脚本(简化示例)时:

When executing the following script (simplified example):

#!/usr/bin/env python
import pexpect
spawn = pexpect.spawn('/bin/bash')
with open('/tmp/logfile.log', 'w') as f:
    spawn.logfile_read = f
    spawn.sendline('echo "Hello I am a really long command, in fact I am 100 characters long! Potato Potato Potato....."')
    spawn.sendline('exit')
    spawn.expect(pexpect.EOF)

我在换行时遇到麻烦,导致 ^ M 字符出现在日志中.现在,pexpect日志文件实际上是这样的:

I have trouble with line wrapping causing ^M characters appearing in the log. The pexpect logfile now literally looks like this:

bash-3.2$ echo "Hello I am a really long command, in fact I am 100 characters lo ^Mng! Potato Potato Potato....."
Hello I am a really long command, in fact I am 100 characters long! Potato Potato Potato.....
bash-3.2$ exit
exit

80个字符后,该行用回车符括起来,我不希望这样.我尝试了很多不同的方法来禁用换行(使用 tput rmam ,事先调用 stty列1000 ,使用 spawn.setwinsize(1000,1000),设置 os.environ ['COLUMNS'] ="1000" ,将各种args传递到/bin/bash ..),我似乎找不到正确的技巧.

After 80 characters the line is wrapped by a carriage return, and I don't want that. I've tried a lot of different things to disable line-wrapping (using tput rmam, calling stty columns 1000 beforehand, using spawn.setwinsize(1000, 1000), setting os.environ['COLUMNS'] = "1000", passing various args to /bin/bash..) and I can't seem to find the right trick.

在这种情况下如何禁用换行?

How can the line wrapping be disabled in this scenario?

推荐答案

我终于明白了.当我记得 echo 命令是bash的内部命令时,向我指出了正确的方向.将命令更改为/bin/echo完全可以改变行为.这只是解决方案的一部分.

I finally figured this out. What pointed me in the right direction was when I remembered that the echo command is internal to bash. Changing the command to /bin/echo completely changed the behavior. This was only part of the solution.

看到bash本身就是问题,因此我在手册页中查看了原因所在.然后打我!Bash使用readline支持,并且无论是否使用/bin/echo或内部echo命令,确保禁用它足以解决您的问题.

Seeing that it appears that bash itself is the problem I looked through the man page to see what the cause may be. Then it hit me! Bash uses readline support and sure enough disabling it fixes your problem regardless of using /bin/echo or the internal echo command.

只需将您的生成行更改为 spawn = pexpect.spawn('/bin/bash --noediting').

Just change your spawn line to spawn = pexpect.spawn('/bin/bash --noediting').

这篇关于使用pexpect/bash时防止换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆