微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linuxterminalinput:从terminal截断行读取用户input,限制在4095个字符

在bash脚本中,我尝试从标准input中读取行,在设置IFS=$'n'后使用内置read命令。 如果我将input粘贴到读取,则这些行将被截断为4095个字符的限制。 这个限制似乎来自terminal阅读,因为这工作得很好:

fill= for i in $(seq 1 94); do fill="${fill}x"; done for i in $(seq 1 100); do printf "%04d00$fill" $i; done | (read line; echo $line)

我经历了与Python脚本相同的行为(不接受来自terminal的4095以上input,但从pipe道接受):

#!/usr/bin/python from sys import stdin line = stdin.readline() print('%s' % line)

即使C程序工作相同,使用read(2) :

#include <stdio.h> #include <unistd.h> int main(void) { char buf[32768]; int sz = read(0,buf,sizeof(buf) - 1); buf[sz] = ''; printf("READ LINE: [%s]n",buf); return 0; }

在任何情况下,我都不能input超过4095个字符。 input提示停止接受字符。

C#在整个Windows中检测文本框input事件

如何从shell的'读'命令返回期望脚本中传递?

在Windowsbatch file中,如何分割围绕关键字的标准input并将一半传递给另一个程序?

是否有可能将原始inputredirect到特定的窗口?

影响原始input消息的Windows键重复设置

问题1:在Linux系统(至lessUbuntu 10.04和13.04)中是否有一种方式可以从terminal中读取4095个以上的字符?

问题2:这个限制来自哪里?

受影响的系统:我注意到Ubuntu 10.04 / x86和13.04 / x86中的这个限制,但是Cygwin(最近的版本)至less在10000个字符以上没有截断(因为我需要在Ubuntu中使用这个脚本,所以没有进一步testing)。 使用的terminal:虚拟控制台和KDE konsole (Ubuntu 13.04)和gnome-terminal (Ubuntu 10.04)。

如何模拟原始input/以正确的方式发送WM_INPUT消息到应用程序?

如何在Linux上使用input子系统来获取键盘事件

读取键盘inputasynchronous和使用DirectInput有什么区别?

如何通过proc_open后台进程,并有权访问STDIN?

浏览器从标准input读取HTML

这是部分答案。 在非规范模式下设置终端允许读取超过4096个字符(其中字符#4096需要成为新行)。

一个bash脚本中,可以这样做:

IFS=$'n' # Allow spaces and other white spaces. stty -icanon # disable canonical mode. read line # Now we can read without inhibitions set by terminal. stty icanon # Re-enable canonical mode (assuming it was enabled to begin with).

添加stty -icanon这个修改后,你可以粘贴长度超过4096个字符的字符串,并使用bash内置的read命令(我成功地尝试了超过10000个字符)成功地读取它。

终端线缓冲区的限制可能是由内核设置的。

去做:

C程序来演示这个(使用tcgetattr()和tcsetattr() )

测试Linux / x86_64架构 – 可以有不同的限制。

找到在内核中定义的位置(可能是${linux_source_path}/include/linux/tty.h定义的${linux_source_path}/include/linux/tty.h )。

我没有解决方法,但我可以回答问题2.在Linux中PIPE_BUF被设置为4096(在limits.h )如果你写了一个超过4096的管道,它将被截断。

从/usr/include/linux/limits.h :

#ifndef _LINUX_LIMITS_H #define _LINUX_LIMITS_H #define NR_OPEN 1024 #define NGROUPS_MAX 65536 /* supplemental group IDs are available */ #define ARG_MAX 131072 /* # bytes of args + environ for exec() */ #define LINK_MAX 127 /* # links a file may have */ #define MAX_CANON 255 /* size of the canonical input queue */ #define MAX_INPUT 255 /* size of the type-ahead buffer */ #define NAME_MAX 255 /* # chars in a file name */ #define PATH_MAX 4096 /* # chars in a path name including nul */ #define PIPE_BUF 4096 /* # bytes in atomic write to a pipe */ #define XATTR_NAME_MAX 255 /* # chars in an extended attribute name */ #define XATTR_SIZE_MAX 65536 /* size of an extended attribute value (64k) */ #define XATTR_LIST_MAX 65536 /* size of extended attribute namelist (64k) */ #define RTSIG_MAX 32 #endif

问题肯定不是read(); 因为它可以读取任何有效的整数值。 问题来自堆内存或管道的大小,因为它们是大小的唯一可能的限制因素..

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐