command line - first EOF not working? - Ask Ubuntu
i have ubuntu 16.04 lts , when run in terminal md5sum
waits input , enter hello
when press ctrl+d not terminate , have press ctrl+d second time terminate that! why?
in unix, objects can read , write - ordinary files, pipes, terminals, raw disk drives - made resemble files.
a program
cat
reads standard input this:n = read(0, buffer, 512);
which asks 512 bytes.
n
number of bytes read, or -1 if there's error.if did repeatedly ordinary file, you'd bunch of 512-byte reads, shorter read @ tail end of file, 0 if tried read past end of file. so,
cat
run untiln
<= 0.reading terminal different. after type in line, terminated enter key,
read
returns line.there few special characters can type. 1 ctrl-d. when type this, operating system sends of current line you've typed (but not ctrl-d itself) program doing read. , here's serendipitous thing: if ctrl-d first character on line, program sent line of length 0 - program see if got end of ordinary file.
cat
doesn't need differently, whether it's reading ordinary file or terminal.another special character ctrl-z. when type it, anywhere in line, operating system discards whatever you've typed until point , sends sigtstp signal program, stops (pauses) , returns control shell.
so in example
$ cat > file.txt pa bam pshhh<ctrl+z> [2]+ stopped cat > file.txt
you typed characters discarded,
cat
stopped without having written output file.$ cat > file.txt pa bam pshhh <ctrl+z> [2]+ stopped cat > file.txt
you typed in 1 line,
cat
read , wrote output file, , ctrl-z stoppedcat
.
source: ctrl+d ending terminal line input answer mark plotnick
Comments
Post a Comment