managing shell i o
play

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


  1. Managing Shell I/O Andrew Mallett LINUX AUTHOR AND CONSULTANT @theurbanpenguin www.theurbanpenguin.com

  2. 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

  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.

  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.

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

  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.

  7. Demo Working with advanced redirection.

  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.

  9. Demo Creating text files using HEREDOCs.

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

  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.

  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 on the shell.

  13. Demo Printing to the console

  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.

  15. Demo Understanding process substitution.

  16. 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)

  17. Next up: Debugging Scripts and Shells

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend