Linux Training : c. Command Prompt and IO redirection

Command prompts and I/O redirection.

  • Command lines

    Back 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.
    They are _clearly_ wrong.
    But the real strength is still at the command line.
    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.
    For example
    Find all instances of bash running right now.

    The ps ax command will show all running process and the grep bash command will find the string bash in a text string. So

    ps ax | grep bash
    

    will find all running bash instances. The secret is the "|" (pipe). It is an I/O redirector

  • IO redirection

    There 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 ax process sends it's STDOUT to the STDIN of the grep bash process using a pipe. The real power of all of this is commands can strung together up the line length limit
     getconf ARG_MAX
    131072
    which is the maximum bytes of args + environ for exec. ARG_MAX is a kernel variable accessible through the getconf command. 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_temp will take the output of the ps ax command and write it to a new file called /tmp/ps_code.
      • If that file exists, it will be +overwritten+.
    • ps ax >> /tmp/ps_temp will 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/null will 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/corefileserror will 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
IO exercise
  1. For each of the following directories
    /home
    /users
    /var
    /notarealfolder
    
    count of the number of entries it finds along with the folder name it's working on. Write this to a single file. Be sure to capture any failures to the file as well.