Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you represent EOF in bash?

Tags:

I'm trying to do something like

read -d EOF stdin  for word in $stdin; do stuff; done 

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

Edit: 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

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?

like image 923
user50264 Avatar asked Jan 21 '09 02:01

user50264


People also ask

How do you indicate EOF in terminal?

the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). key combination is used to stop a process. It can be used to put something in the background temporarily.

How do I type EOF in Linux?

4 Answers. Show activity on this post. In Windows, Control+Z is the typical keyboard shortcut to mean "end of file", in Linux and Unix it's typically Control+D .

What is EOF character in Linux?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

What is cat << EOT >>?

linux - use cat << EOT >> to insert actual strings and escape logic - Stack Overflow.


2 Answers

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 
like image 118
Johannes Schaub - litb Avatar answered Oct 20 '22 04:10

Johannes Schaub - litb


To find what a control character is, run

$ cat | od -b ^D 0000000 004 012 0000002 

I typed ^V^D after issuing the command, and then RET and another ^D (unquoted) and the result is that EOF is octal 004.

Combining that result with read(1):

$ read -d "$(echo -e '\004')" stdin foo bar quuz^Hx ^D $ echo "$stdin" foo bar quux $ for word in $stdin; do echo $word; done foo bar quux 

Yes, I typed ^H above for backspace to see if read(1) did the right thing. It does.

like image 44
ashawley Avatar answered Oct 20 '22 04:10

ashawley