你将如何重新在bash present EOF? [英] How would you represent EOF in bash?

查看:102
本文介绍了你将如何重新在bash present EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情。

read -d EOF stdin

for word in $stdin; do stuff; done

在这里我要替换EOF文件字符结束的实际再presentation。

where I want to replace 'EOF' for an actual representation of the end of file character.

修改感谢您的答案,那的确是我要怎样做。其实我有一个捂脸时刻,当我看到标准输入= $(猫)哈哈

Thanks for the answers, that was indeed what I was trying to do. I actually had a facepalm moment when I saw stdin=$(cat) lol

只是踢,虽然你会如何去像一个C-D匹配的东西(或C-V M-V等),基本上只是一个字符与控制,Alt键,Shift键,组合无论在bash?

Just for kicks though how would you go about matching something like a C-d (or C-v M-v etc), basically just a character combined with Control, Alt, Shift, whatever in bash?

推荐答案

有没有档案结尾的性格真的。当preSS Ctrl-D或者类似字符,该终端驱动器信号到读取的应用程序,文件的结尾已经到达,通过返回无效值。同样由操作系统来完成,当您已到达文件的结尾。这是通过使用,而不是一个字节一个整数做(让你有一系列类似-2 ^ 16 .. 2 ^ 16,而不是只0..255)并返回超出范围的值 - 通常是 1 。但没有字符,将重新present EOF ,因为它的全部目的就是为不可以字符。如果你想读的一切从标准输入,直到文件结尾,尝试

There isn't an end-of-file character really. When you press Ctrl-d or similar characters, the terminal driver signals to the reading application that the end of file has been reached, by returning an invalid value. The same is done by the operation system, when you have reached the end of the file. This is done by using an integer instead of a byte (so you have range similar to -2^16 .. 2^16, instead of only 0..255) and returning an out-of-range value - usually -1. But there is no character that would represent eof, because its whole purpose is to be not a character. If you want to read everything from stdin, up until the end of file, try

stdin=$(cat)
for word in $stdin; do stuff; done

这将然而读取整个标准输入到变量。你可以只使用数组一行分配内存脱身,使读取一行字成数组:

That will however read the whole standard input into the variable. You can get away with only allocating memory for one line using an array, and make read read words of a line into that array:

while read -r -a array; do 
    for word in "${array[@]}"; do 
        stuff;
    done
done

这篇关于你将如何重新在bash present EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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