Command prompts and I/O redirection.
Command linesBack in the dark ages of computing, people had to remember commands to type in at a command prompt. They would enter a string of text with cryptic codes and formatting and press the Enter button to submit their command to the computer to execute. If they had mistyped their command, the enter button press was often followed by an expletive. The Graphical User Interface (GUI) was created to make complicated command entry a simple button click. Many people think Linux has no gui.But the real strength is still at the command line.
They are _clearly_ wrong.
The UNIX methodology is to have a huge selection of tools that do small tasks very well. Those tools can be strung together and used in series so the output of one command is the input to the next. That is a very powerful capability.
IO redirectionThere are 3 normal input/output channels for every process, STDIN, STDOUT and STDERR for standard in, standard out and standard error respectively. STDIN is by default the keyboard. STDOUT and STDERR is by default the screen. The
ps axprocess sends it's STDOUT to the STDIN of the
grep bashprocess using a pipe. The real power of all of this is commands can strung together up the line length limit which is the maximum bytes of args + environ for exec.
ARG_MAXis a kernel variable accessible through the
getconfcommand. It is defined in <limits.h> which is viewable at /usr/src/kernels/$(echo $(uname r)""$(uname -i))/include/linux/limits.h
There are are some other very useful things that can be done with I/O redirection such as write STDOUT to a file or use STDIN from a file.
ps ax > /tmp/ps_tempwill take the output of the
ps axcommand and write it to a new file called
If that file exists, it will be +overwritten+.
ps ax >> /tmp/ps_tempwill append the new text to the end of the existing file or create a new file and write it.
- STDERR can be written to a file as well by adding 2><filename> to the end of a command line.
2>&1; 1>/dev/nullwill redirect STDERR (2) to STDOUT (1) and then redirect STDOUT to a "bit bucket" /dev/null.
STDIN is 0, STDOUT is 1 and STDERR is 2
/usr/bin/time find ~ -name core > /tmp/corefiles 2>/tmp/corefileserrorwill write the output of the find process to the /tmp/corefiles file and all of the error messages will go to the /tmp/corefileserror file. The last lines of the /tmp/corefileserror file will have the output of the /usr/bin/time command as well since it write to STDERR.
- Shell windows in X are just like a console screen except that an environment variable called DISPLAY is set so that processes running on that display will have their output go to the correct screen (remember: Linux is multi-user!).
- Commands that fail write to STDERROR. So to capture that failure, redirect STDERROR to a file