Files
Every UNIX program has three file streams opened for it when it starts up under normal conditions. One is for input, other is for output and another one is opened for printing error messages.
Stdin¶
The input stream is referred to as the "standard input" stream (stdin). Under normal circumstances, File descriptor 0
is assigned to stdin.
Stdout¶
The output stream is referred to as the "standard output" stream (stdout). Under normal circumstances, File descriptor 1
is assigned to stdout.
Stderr¶
The error stream is referred to as the "standard error" stream (stderr). Under normal circumstances, File descriptor 2
is assigned to stderr.
Stream redirection¶
Output Redirection¶
The output from a command normally intended for standard output can be easily diverted to a file instead. This capability is known as output redirection.
If with the notation >
, file is appended to any command that normally writes its output to standard output, the output of that command will be written to file instead of your terminal.
cat test.c > test1.c
The output of cat is redirected to another file in the above case.
Input Redirection¶
Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character >
is used for output redirection, the less-than character <
is used to redirect the input of a command.
./bin < inp
The content of the file inp
is redirected to the binary as it's input.
Redirection of output of a process to another¶
The |
operator takes output from one program, or process, and sends it to another.
cat test.txt | grep "CTF"
Here we are redirecting the output of the cat
command to the grep
command to chain the effect of multiple commands used at once.
Redirection of file descriptors to pseudo-files¶
When needed , we can silence the output of certain commands by redirecting stdout and stderr to the pseudo-files like /dev/null
.
docker-compose up --build > /dev/null 2>&1
Here in this command , all of it's stdout and stderr will be redirected to the /dev/null
.
Here Document¶
A here document is used to redirect input into an interactive shell script or program.
The general form of the here document is :
command << delimiter
document
delimiter
Here the shell interprets the <<
operator as an instruction to read input until it finds a line containing the specified delimiter. The delimiter specified tells the shell that the here document has completed.
You can also use here document to print multiple lines in a shell script like this
cat << EOF
CTFs are fun but well
they're not easy sometimes
though u get a lot to learn :)
EOF