SLIDE 1 @theurbanpenguin www.theurbanpenguin.com
LINUX AUTHOR AND CONSULTANT
Andrew Mallett
Managing Shell I/O
SLIDE 2
Redirect STDOUT and STDERR to a single location Redirect complete blocks The power of exec in redirection Working with HEREDOCs Using double quotes with echo and printf Advanced redirection with process substitution
Module Overview
SLIDE 3 $ ls /etc/hosts /etc/hosts (STDOUT) $ ls /etc/host ls: cannot access '/etc/host': No such file or directory (STDERR) $ ls /etc/hosts /etc/host &>file1
Redirection
Output from commands are usually divided into STDOUT and
- STDERR. We can redirect each of these channels individually or both
to the same file as in the example.
SLIDE 4 $ ( ls /etc/hosts ; ls /etc/host ) > file1 ls: cannot access '/etc/host': No such file or directory $ bash > output
Redirecting Blocks and Subshells
Commands can be blocked together with single parenthesis. The combined output from the command block can be redirected as
- required. Equally, redirecting the output of BASH itself will redirect
everything from the shell.
SLIDE 5
Demo
Let's work with shell redirection at the command line.
SLIDE 6
$ LOG=log.file $ exec 4>&1 $ exec > "$LOG" $ ls $ exec 1>&4 4>&-
Controlling Redirection Using Exec
Perhaps, more flexible is the exec command that can be used to create new file descriptors that connect to the builtin file descriptors. We use new file descriptors as they are easier to reset than the standard descriptors when redirection is no longer required.
SLIDE 7
Demo
Working with advanced redirection.
SLIDE 8
$ cat > myfile <<END This is line 1 This is line 2 END
HEREDOC
STDIN can be redirected from a file rather than the keyboard. Often this can be useful within scripts to create files from the scripts. The keyword END, in this case, can be any string that will not appear in the body text.
SLIDE 9
Demo
Creating text files using HEREDOCs.
SLIDE 10 The commands echo and printf can be used to print to the
- console. When doing so always quote variables to protect
spaces and other special characters that may be misinterpreted. Each command has a builtin and an external version portability is increased using external commands at the cost
Console Printing
SLIDE 11
$ username="jo smith" $ printf "The user is %s\n" $username The user is jo The user is smith $ printf "The user is %s\n" "$username" The user is jo smith
Quote Variables
Quoting variables prevents possible spaces in the variable causing havoc in your data.
SLIDE 12 $ type -a echo printf echo is a shell builtin echo is /bin/echo printf is a shell builtin printf is /usr/bin/printf
Builtin / External
Commands builtin to the shell use less resource and run more quickly, using the external commands aids portability as we are not dependent
SLIDE 13
Demo
Printing to the console
SLIDE 14 $ cat list1 jane bob $ cat list2 bob jack $ comm -3 <(sort list1 | uniq) <(sort list2 | uniq) jane jack
Process Substitution
Output from command groups can be redirected in the form of process substitution. Here we compare the sorted output or unique entries from two files. The option -3 excludes printing of the common lines in both files.
SLIDE 15
Demo
Understanding process substitution.
SLIDE 16
ls /etc/hosts /etc/host &>file1 ( ls /etc/hosts ; ls /etc/host ) > file1 bash > output exec 4>&1 exec 1>&4 4>&- cat > myfile <<END This is line 1 This is line 2 END Double quote variables comm -3 <(sort list1 | uniq) <(sort list2 | uniq)
Summary
SLIDE 17
Next up: Debugging Scripts and Shells