IT 3110 : Advanced System Administration

Standard output

Review

  • somecommand < inputfile > outputfile
  • If we dont put an input file, it will read from stdin

Preserve spacing

  • echo "this has some spacing"
  • printf gives you more control over formatting and placement of output
    • printf '%s = %d\n' Lines $LINES
      • %s means we will replace with a string
      • %d means we will replace with a digit

printf another example

  • printf '%-10.10s = %4.2f\n' 'Gigahertz' 1.9273534333
    • "The numbers between the % and the format type (s or f in our example) provide additional formatting details. For the floating-point type (f), the first number (4 in the 4.2 specifier) is the width of the entire field. The second number (2) is how many digits should be printed to the right of the decimal point. Note that it rounds the answer.

printf another example

For a string, the first number is the maximum field width, and the second is the number of bytes to be printed. The string will be truncated (if longer than max) or blank padded (if less than min) as needed. When the max and min specifiers are the same, then the string is guaranteed to be that length. The negative sign on the specifier means to left-align the string (within its field width). Without the minus sign, the string would right-justify."

Other printf examples

  • "%s %s .... %i"
  • newlines

Output to file(review)

  • > vs >>
  • cat files together
  • redirect std error (different files?) (same file)
  • head vs tail
    • tail -n +2 skips first 2 lines
  • /dev/null

Output

  • Use {} to group commands together
    • { ls -l; echo "foo"; } > out.txt
    • braces must have whitespace and end with ;
  • Could also use () to group those commands, but know that it spawns a subshell
    • { mkdir temp; cd temp; touch foo.txt; ls -l; } > temp.txt
    • after execution, you will be in the same spot.

Output

  • | pipes
  • tees, split output into 2 identical streams one to a file and one to stdout.
    • echo "hello" | tee somefile.txt

Output as arguments to other programs

  • Cant do this
    • find . -name '*.c' | rm
    • rm doesn't read from stdin
  • can do this
    • `rm $(find . -name '*.c')
  • remember () spawn a subshell, so do backticks