Redirections and pipes
The three standard streams, the redirections >, >>, <, 2> and 2>&1, /dev/null, the pipe and tee, and the order in which the shell applies them.
- 23 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Standard streams: what is it?
Show the answer
The three channels every process has open from the start: standard input, standard output and standard error.
By default, input is the keyboard, output and error are the screen. Keeping the two outputs apart lets you redirect results without losing error messages.
With
ls /etc/passwd /nope > out, what is on the screen and what is inout?- Screen: the
/nopeerror;out:/etc/passwd - The screen stays empty;
outcontains/etc/passwdand the error - The screen shows
/etc/passwd;outcontains the error about/nope - The screen shows both lines;
outis created but stays empty
Show the answer
Screen: the
/nopeerror;out:/etc/passwd>only redirects standard output (descriptor 1). The message “ls: cannot access '/nope': No such file or directory” goes to the error output, so to the screen.- Screen: the
Standard input has the number …, standard output … and standard error ….
Show the answer
Standard input has the number 0, standard output 1 and standard error 2.
A bare
>is a shortcut for1>. That is why2>is needed to catch errors.>and>>: what is it?Show the answer
Send standard output into a file: the first overwrites it, the second appends to the end.
Both create the file if it does not exist.
>empties it as soon as the shell opens it, before the command even runs.After
sort notes.txt > notes.txt, the filenotes.txtis empty. Which command really sorts the file?sort -o notes.txt notes.txtsort notes.txt >> notes.txtsort notes.txt | notes.txtsort < notes.txt > notes.txt
Show the answer
sort -o notes.txt notes.txtThe shell opens
notes.txtfor writing, thus empties it, beforesortreads it.-oletssortread the whole file before writing.>>would append the sorted lines,|would look for a commandnotes.txt, and the<version empties the file the same way. You can also go through a temporary file thenmv.