Linux – STDIN STDOUT STDERR

Written by kisa

On May 17, 2020

STDIN

Standart input stream (descriptor 0) – arguments that goes into a program. We can rearrange the output stream of one program to standard input stream of another program.(piping)

$cat file_one | grep ^- | grep rwx | grep $2

(^ – lines starts with.. $ – line ends with.)

Not every program will have input to redirect the stream, (for example ls does not have one)

STDOUT STDERR

Standart output stream  (descriptor 1) – generated by a program. Shows us the output of a given program run. Prints to screen by default. We can redirect the output to a file.

This command rewrites an existed file.

To concatenate to a file use “>>”

 

$ls -la > stdout

SAME

$ls -lia 1> stdout

Standart error stream (descriptor 2) – errors generated by a program run. To redirect to a file, use 2>

$ls -li 2> stderr

We can combine both like this:

$ls -li 1> output_file 2> error_file


Bash

Sequence order of a command in born again shell:

$ls -l > stdout

(RightToLeft) checks if stdout file exists, if not, creates a file and puts the output stream of ls -l to it.

(!)(caution – ls command will see that stdout non existent, so upon creation it takes 0 bytes on hard drive, and that information will be saved in stdout – thus alters the stdout file size ! )

Suppose that file_test non existent:

$ls -l file_test 

gives us an error – because file_test non existent, BUT

$ls -l file_test > file_test

Bash will create file_test and will output the information of 0 byte file to it, (writes it (alters the size))

Be cautious!

$tail -n 50 log > log

Command will check if log file exists, if  YES it will erase the existent file with the output stream of tail command.


tee

$ls -l | tee file

tee doubles the output stream – tee rewrites a file, Add -a key to concatenate.


pointers

$ls -l filename ./ 1> file 2>&1

output of both streams to one file. &1 – tells the bash to output as stream 1.

$ls -l asdf ./ 2>&1 1> /dev/null | grep asd

1. | pipe – stdout 1 of ls is redirected to input of grep.

2. 2>&1 – stderr looks as stdout stream – to stdin of grep.

3. 1>/dev/null – redirect stream 1 to NULL device, but stderr 2 is still tied to stdin of grep.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *