Managing Shell I/O Andrew Mallett LINUX AUTHOR AND CONSULTANT @theurbanpenguin www.theurbanpenguin.com
Redirect STDOUT and STDERR to a Module single location Overview Redirect complete blocks The power of exec in redirection Working with HEREDOCs Using double quotes with echo and printf Advanced redirection with process substitution
$ 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.
$ ( 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.
Demo Let's work with shell redirection at the command line.
$ 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.
Demo Working with advanced redirection.
$ 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.
Demo Creating text files using HEREDOCs.
Console Printing 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 of speed.
$ 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.
$ 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 on the shell.
Demo Printing to the console
$ 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.
Demo Understanding process substitution.
ls /etc/hosts /etc/host &>file1 ( ls /etc/hosts ; ls /etc/host ) > file1 bash > output exec 4>&1 Summary 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)
Next up: Debugging Scripts and Shells
Recommend
More recommend