Linux Flashcards
What is a Shell?
It is a program that exposes the operating system. For example bash (Bourne Again Shell). It is usually interacted with via terminal (a shell prompt) by keyboard input.
Dash, ash, are all types of shell.
How to eliminate duplicate lines in a linux file?
cat file.txt | sort –unique
Also
cat file.txt | sort | uniq
Also works, but is more verbose.
What are the standard streams in a Linux system?
The standard streams are three interconnected input and output communication channels.
The two output streams are stdout and stderr. They are represented as files at /dev/stdout and /dev/stderr respectively. While stdout is meant to be the default destination text output from the shell, while stderr receives error output from the shell. Of course there can be exceptions. The file descriptor for stdout is 1 and for stderr is 2.
The input stream is stdin, represented by a file at /dev/stdin. Not all programs require input streams. The file descriptor for stdin is 0.
How to send error stream to the output stream?
On bash: 2>&1
2 is the file descriptor for stderr
1 is the file descriptor for stdout
> is redirect; for example command 2> file
would redirect stderr to a file
& is necessary to identify 1 as a file descriptor and not as a file named 1
What does true && false || true
evaluate to?
It evaluates to true.
(true && false) evaluates to false
(false || true) evaluates to true
Given aa && bb || cc
; which commands are executed given different exit codes?
Either:
- aa (if a is false)
- aa, bb (if a is true and b is true)
- aa, bb, cc (if a is true and b is false)
In summary:
- && is AND; only executes the second command if first is true
- || is AND; only executes the second command if first is false