-
[Linux] shell read命令 通过管道读取文件 [大三TJB_708]
2012-11-08 09:56:21shell read 命令除可以从标准输入流中读取字符串给变量外,还能从特定的文件中向变量赋值文件中的“一行内容”。每次给变量赋值为管道内容的输入的行内容。 1.例:read_file.sh 图1.read_file.sh读取文件内容 【3】...shell read 命令除可以从标准输入流中读取字符串给变量外,还能从特定的文件中向变量赋值文件中的“一行内容”。每次给变量赋值为管道内容的输入的行内容。
1.例:read_file.sh
图1.read_file.sh读取文件内容
【3】对CountNum赋初始值为1.
【5-10】用cat命令打开for_read.c(在shell界面直接使用这个命令时,for_read.c的所有内容将会直接被展现在屏幕上),通过管道 '|' 把for_read.c当前行的内容赋值给read命令后的变量line。打印行号和此行的内容,行号增加。如此循环,直到文件读取完毕,此时read命令返回值不为0.
【8-10】这是变量增1的两种表达方式。注意第十行的增1方式,前面的冒号":"不可省。
2.for_read.sh中的内容:
图2.for_read.c文件中的内容
3.运行脚本观看结果
首先,修改脚本权限:chmod +x read_file.sh
运行脚本:./read_file.sh
运行结果为:
图3.read_file.sh脚本运行结果
4.总结
前辈们写的工具都是考虑的全面,从最简单的功能设计开始,都是会涉及到有可能且合理的功能上面去的。
-
qt文件逐行读取_如何从C ++ / Qt Linux应用程序逐行读取FIFO /命名管道?
2020-12-24 10:19:10How do I read a FIFO/named pipe line by line from a C++/Qt Linux app?Today I can open and read from a fifo from a Qt program,but I can't get the program to read the data line by line.Qt reads the enti...How do I read a FIFO/named pipe line by line from a C++/Qt Linux app?
Today I can open and read from a fifo from a Qt program,
but I can't get the program to read the data line by line.
Qt reads the entire file, meaning he waits until the "sender" closes his session.
Let's take a example with some shell commands to show what I would like the app to do.
First create a fifo
mkfifo MyPipe
Then we can use cat to read from the fifo
cat MyPipe
And then we send some data in with another cat
cat > MyPipe
And then start to type something, and every time you hit enter it arrives at the reader.
And then when you close it with Ctrl+D both sides end.
Now the sender is easy to create with a QTextStream,
you just need to flush when you want to send.
QFile file("MyPipe");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file);
for(int i=0; i<3; i++) {
out << "Hello...: " << i << "\n";
out.flush();
sleep(2);
}
file.close();
But then to write a little reader that read line by line is where I'm stuck right now,
all my tries with the Qt lib ends up with that I get the data but
not until the sender uses file.close() on the fifo.
Not when he flush, as occurs when I use cat to read.
Like this example:
QFile file("MyPipe");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 0;
QTextStream in(&file);
QString line;
do {
line = in.readLine();
qDebug() << line;
} while (!in.atEnd());
file.close();
What am I missing?
It just feels like I need to use some kind of isReady or lineAvailable on
the stream or something like that,
but I can't find anything in the docs that fits...
/Thanks
Note:
If I go with the low level c style and read one char at the time I do
get the style Im searching for.
But it would be nice to be able to do the same Qt style.
FILE *fp;
fp=fopen("MyPipe", "r");
char c;
while((c=getc(fp)) != EOF)
{
printf("%c",c);
}
fclose(fp);
Update:
When I start a debugger the program is hanging on the readLine(),
and do not continue until the other party closes the fifo.
And I do get the same using ">>"
line = in.readLine();
in >> line;
解决方案
Use the low level c style and read one char at the time.
FILE *fp;
fp=fopen("MyPipe", "r");
char c;
while((c=getc(fp)) != EOF)
{
printf("%c",c);
}
fclose(fp);
-
Linux系统管道和有名管道的通信机制
2020-03-04 12:16:50管道两端可分别用描述字fd[0]以及fd[1]来描述,需要注意的是,管道的两端是...如果试图从管道写端读取数据,或者向管道读端写入数据都将导致错误发生。一般文件 的I/O函数都可以用于管道,如close、read、write等等。 -
linux socket read
2012-05-17 15:54:27linux socket read 阻塞 read函数只是一个通用的读文件设备的接口。是否阻塞需要由设备的属性和设定所决定。一般来说,读字符终端、网络的socket描述字,管道文件等,这些...怎么样以非阻塞的方式从管道中读取数据展开全文 -
linux下进程通信(无名管道和有名管道FIFO)
2020-09-01 22:48:10管道数据不能重复读取,一旦调用read读取写入管道的数据,这段数据将永久从管道中移除,不能被其它进程获取; 管道有点类似于队列,允许用户向其中连续放入多条内容,然后可以逐条取出; 二、有名管道和无名管道 ...一、什么是管道
管道是从unux继承过来的最早的IPC通讯方式之一,它有如下特点:
- 管道数据的读写是半双工的,即只能从写端写入,从读端读出;
- 管道数据不能重复读取,一旦调用read读取写入管道的数据,这段数据将永久从管道中移除,不能被其它进程获取;
- 管道有点类似于队列,允许用户向其中连续放入多条内容,然后可以逐条取出;
二、有名管道和无名管道
2.1、无名管道
- 无名管道的存在依赖于创建它的进程,当进程退出后,无名管道的资源也会自动释放;
- 无名管道通信只能用于具有亲缘关系的进程之间的通讯(有名管道没有这个限制);
2.2、有名管道
- 有名管道也叫FIFO,它在linux系统中作为一种特殊的文件而存在,并具有文件系统中的inode信息。
- 有名管道(FIFO)创建之后是独立存在的,它不依赖于创建它的进程,因此任何进程可以根据FIFO的名称来打开并进行读写操作;
- 读写FIFO的两个进程调用open打开同一个FIFO文件是会得到相同的文件描述符;
- 有名管道(FIFO)在创建之后如果不再使用,需要主动删除,否则它会一直存在于系统之中;
- 我们可以把FIFO简单理解成一个文件,只是FIFO不能用open或者create等普通文件的创建方式来创建,而需要用mkfifo函数来创建。使用mkfifo创建一个FIFO之后,我们就可以把FIFO当成普通文件来使用open打开,使用read和write进行读写。
四、管道相关函数
4.1、pipe函数
int pipe(int pipefd[2]);
- pipe创建一个无名管道,pipfd用于指向管道两端的描述符。fppfd[0]指向读端,pipfd[1]指向管道的写端;
- 数据从管道写段写入,从管道读端读出,使用pipe创建得到的pipefd指向的是内存中的某一块内存,这块内存由系统分配,之后可以调用read和write对pipefd进行读写操作。
4.2、mkfifo函数
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
- mkfifo用来创建一个有名管道(FIFO),它的第一个参数指定一个路径名,mode指定FIFO的权限,如0666表示具有可读和可写权限。
- mkfifo调用成功返回0,失败返回-1。
示例:
fifo_read.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <fcntl.h> int main() { int ret = -1; char *path = "/myfifo"; char buffer[1024]; int fd = -1; if(0 != access(path,F_OK))//如果文件不存在,创建FIFO文件 { ret = mkfifo(path,0666); if(0 != ret) { perror("mkfifo error\r\n"); return -1; } } fd = open(path,O_RDONLY); if(0 > fd) { perror("open fifo error\r\n"); return -1; } printf("read fifo fd:%d\r\n",fd); while(1) { memset(buffer,0,sizeof(buffer)); if(read(fd,buffer,sizeof(buffer))<=0) { perror("read error"); break; } printf("read fifo:%s",buffer); if(0 == strncmp(buffer,"exit",4)) { break; } } printf("read process exit\r\n"); close(fd); }
fifo_write.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <fcntl.h> int main() { int ret = -1; char *path = "/myfifo"; char buffer[1024]; int fd = -1; if(0 != access(path,F_OK))//如果文件不存在,创建FIFO文件 { ret = mkfifo(path,0666); if(0 != ret) { perror("mkfifo error\r\n"); return -1; } } fd = open(path,O_WRONLY); if(0 > fd) { perror("open fifo error\r\n"); return -1; } printf("write fifo fd:%d\r\n",fd); while(1) { printf("write some data:"); ret = fgets(buffer,1024,stdin); if(write(fd,buffer,sizeof(buffer))<0) { perror("write error"); break; } if(0 == strncmp(buffer,"exit",4)) { break; } } printf("write process close fd\r\n"); close(fd); }
gcc fifo_write.c -o write gcc fifo_read.c -o read
分别在两个shell中运行read和write,运行结果如下
-
linux socket read 阻塞
2019-07-24 15:01:21read函数只是一个通用的读文件设备的接口。是否阻塞需要由设备的属性和设定所决定。一般来说,读字 符终端、网络的socket描述字,管道文件等...怎么样以非阻塞的方式从管道中读取数据?因为我用read函数时,如果管... -
04LinuxC进程间通信之管道读写行为(非常重要)
2021-01-09 22:50:581) 如果所有指向管道写端的文件描述符都关闭了(管道写端引用计数为0),而仍然有进程从管道的读端读数据,那么管道中剩余的数据都被读取后,再次read会返回0,就像读到文件末尾一样。这个也是我们判断对方断开连接... -
Linux学习笔记-匿名和命名管道读写的相同点及不同点
2019-01-08 23:16:35单纯读时,在所有数据被读取后,read返回0,以表示到达了文件尾部。 b..单纯写时,则产生信号SIGPIPE,如果忽略该信号或捕捉该信号并从处理程序返回,则write返回-1,同时errno设置为EPIPE。... -
有名管道(读写),read ,write注意事项
2011-05-03 19:03:20reader.c 从argv[1]所指定的文件中读取内容,依次写到管道 ...writer.c 从管道/home/linux/myfifo中读取内容,写到argv[1]所指定 的文件中并保存 ==========================================write.c=======... -
管道的实现机制
2012-03-03 10:33:12在Linux中,管道是一种使用非常频繁的通信机制。从本质上说,管道也是一种文件,但它又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题,具体表现为: · 读取进程也可能工作得比写进程快。当所有... -
IPC通信之管道
2014-12-01 23:03:20管道 Linux管道(pipe)提供一种单向(半双工)的进程间通讯...管道的读写可以使用Linux标准IO操作接口进行,例如read、write等。从图1不难看出,数据一直缓存在内核中直到被读取出来。 单个进程的管道几乎没有任何用处 -
管道的实现机制
2011-01-15 20:56:00从本质上说,管道也是一种文件,但它又和一般的文件有所不同。实际上,管道是一个固定大小的缓冲区。在Linux中,该缓冲区的大小为1页,即4K字节,使得它的大小不象文件那样不加检验地增长。使用单个固定缓冲区也会... -
linux_C函数库中文手册
2013-06-01 17:10:21要求读取的字节数少, 则有可能读到了文件尾、从管道(pipe)或终端机读? 蛘呤莚ead()被信号中断了读取动 作. 当有错误发生时则返回-1, 错误代码存入errno 中, 而文件读写位置则无法预期. 错误代码 EINTR 此调用被... -
SHELL脚本read命令的具体用法
2021-01-09 03:26:53要与Linux交互,脚本获取键盘输入的结果是必不可少的,read可以读取键盘输入的字符。 shell作为一门语言,自然也具有读数据的功能,read就是按行从文件(或标准输入或给定文件描述符)中读取数据的最佳选择。当使用... -
shell有哪些重要知识点?Linux运维面试题
2020-12-16 15:39:321.2 在shell脚本中,用来读取文件内各个域的内容并将其赋值给shell变量的命令是______ fold join tr read 1.3 从后台启动进程,应在命令的结尾加上符号() A:& B:@ C:# D:$ 1.4 不是shell具有的功能和特点... -
跨平台C++服务器程序开发 (3)Linux文件描述符
2016-03-06 23:30:34Linux文件描述符在Linux系统中几乎所有可读写的对象都可视为文件,比如标准输入输出、磁盘文件、套接字、管道等,都可以用read函数读取数据,用write函数写入数据。 这一点相对于Windows系统来说更加统一,在Windows... -
其他备选的I/O模型--《linux/unix系统编程手册》
2015-08-15 19:27:071.当从一个管道读取数据时,如果管道中恰好没有数据,那么通常read()会阻塞。而如果管道中没有足够的空间保存待写入的数据时,write()也会阻塞,当在其他类型的文件如FIFO和套接字上执行I/O操作时也会出现相似的行为... -
Linux网络编程 (第2版 )
2018-08-03 11:52:3411.4.3 从套接口读取链路帧的编程方法 318 11.4.4 定位ip包头的编程方法 319 11.4.5 定位tcp报头的编程方法 321 11.4.6 定位udp报头的编程方法 322 11.4.7 定位应用层报文数据的编程方法 323 11.4.8 使用sock_... -
Linux程序设计 第4版.haozip02
2012-11-05 00:29:326.3.2 从屏幕读取 180 6.3.3 清除屏幕 180 6.3.4 移动光标 180 6.3.5 字符属性 181 6.4 键盘 183 6.4.1 键盘模式 183 6.4.2 键盘输入 184 6.5 窗口 185 6.5.1 window结构 185 6.5.2 通用函数 186 6.5.3 ... -
Unix/Linux 编程实践教程.PDF
2010-09-03 18:34:122.5.1 问题:如何从文件中读取数据结构 2.5.2 答案:使用 open、read 和 close 2.5.3 编写 whol,c 2.5.4 显示登录信息 2.5.5 编写 who2.c 2.5.6 回顾与展望 2.6 编写 cp(读和写) 2.6.1 问题 1:cp 命令能... -
libevent 学习:官方案例 event-read-fifo
2019-01-10 15:45:14这个案例实现的是从命名管道读取内容。(考虑在Linux环境,删除跨平台相关代码) #include <event2/event-config.h> #include <sys/types.h> #include <sys/stat.h> #... -
socke tcp 阻塞 非阻塞 read、write、write、writev、send、sendto、sendmsg
2014-11-01 12:52:14linux socket read 阻塞 read函数只是一个通用的读文件设备的接口。是否阻塞需要由设备的属性和设定所决定。一般来说,读字 符终端、网络的socket描述字,管道文件等,... 怎么样以非阻塞的方式从管道中读取数据? -
linux网路编程 中文 23M 版
2016-03-11 16:59:55第1 章Linux操作系统概述................... .......................................................................... 2 1.1 Linux发展历史........................................................ 2 ... -
Linux-0.11 [内核源代码带中文注释]
2010-04-02 08:42:45从linux 内核0.95 版后已经使用与现在相同的命名方法了。 entry start ! 告知连接程序,程序从start 标号开始执行。 start: ! 47--56 行作用是将自身(bootsect)从目前段位置0x07c0(31k) ! 移动到0x9000(576k)处,... -
Linux与unix shell编程指南1.rar
2010-03-09 11:01:0018.7.3 用while循环从文件中读取数据 189 18.7.4 使用IFS读文件 189 18.7.5 带有测试条件的文件处理 190 18.7.6 扫描文件行来进行数目统计 191 18.7.7 每次读一对记录 193 18.7.8 忽略#字符 193 18.7.9 处理格式化... -
python右下角写名字_如何在Python中打开命名的pipecharacter设备专用文件进行读写...
2020-12-17 20:42:33我有一个在Linux机器上运行的服务,它创建一个命名管道字符设备特殊文件,我想编写一个Python3程序,通过编写文本命令和从管道设备读取文本回复来与服务进行通信.我没有该服务的源代码.我可以使用os.open(named_pipe_...
-
常用JVM启动参数
-
FyreString:FyreString是PHP的免费开源字符串实用程序库-源码
-
2013年下半年 信息系统监理师 上午试卷 综合知识 软考真题【含答案和答案解析】
-
Go-SpeedTest-Bot:帮助您使用手机管理所有节点的机器人-源码
-
深究字符编码的奥秘,与乱码说再见
-
mpsoc zcu104 上做hdmi 显示实验
-
使用 Linux 平台充当 Router 路由器
-
用nodejs抓取-源码
-
NFS 网络文件系统
-
一天学完MySQL数据库
-
【Python-随到随学】FLask第二周
-
将和声搜索算法与杜鹃搜索混合,以进行全局数值优化
-
PPTP_NNN 服务生产环境实战教程
-
简单增删查改新闻管理系统
-
2009年下半年 信息系统管理工程师 上午试卷 综合知识 软考真题【含答案和答案解析】
-
朱老师C++课程第3部分-3.6智能指针与STL查漏补缺
-
FPS游戏逆向-UE4虚幻四游戏逆向
-
ApacheFlink漫谈系列-概述
-
工程制图 AutoCAD 2012 从二维到三维
-
支付宝架构师眼里的高可用与容灾架构演进