Managing Shell I/O Andrew Mallett LINUX AUTHOR AND CONSULTANT - - PowerPoint PPT Presentation

managing shell i o
SMART_READER_LITE
LIVE PREVIEW

Managing Shell I/O Andrew Mallett LINUX AUTHOR AND CONSULTANT - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

@theurbanpenguin www.theurbanpenguin.com

LINUX AUTHOR AND CONSULTANT

Andrew Mallett

Managing Shell I/O

slide-2
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
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
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
SLIDE 5

Demo

Let's work with shell redirection at the command line.

slide-6
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
SLIDE 7

Demo

Working with advanced redirection.

slide-8
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
SLIDE 9

Demo

Creating text files using HEREDOCs.

slide-10
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

  • f speed.

Console Printing

slide-11
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
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

  • n the shell.
slide-13
SLIDE 13

Demo

Printing to the console

slide-14
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
SLIDE 15

Demo

Understanding process substitution.

slide-16
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
SLIDE 17

Next up: Debugging Scripts and Shells