CSC 140 Introduction to IT IO Redirection CIT

  • Slides: 28
Download presentation
CSC 140: Introduction to IT I/O Redirection CIT 140: Introduction to IT 1

CSC 140: Introduction to IT I/O Redirection CIT 140: Introduction to IT 1

Topics 1. 2. 3. 4. 5. 6. 7. Standard Files: stdin, stdout, stderr. Input

Topics 1. 2. 3. 4. 5. 6. 7. Standard Files: stdin, stdout, stderr. Input Redirection Output Redirection Stderr Redirection Appending Pipes Combining Pipes and Redirection CIT 140: Introduction to IT 2

Introduction All commands perform at least one of the following: 1. Input 2. Output

Introduction All commands perform at least one of the following: 1. Input 2. Output 3. Processing Standard files for commands 1. Standard Input (stdin) 2. Standard Output (stdout) 3. Standard Error (stderr) CIT 140: Introduction to IT 3

Standard Files and File Descriptors By default – stdin is associated with keyboard –

Standard Files and File Descriptors By default – stdin is associated with keyboard – stdout is associated with display screen – stderr is associated with display screen CIT 140: Introduction to IT 4

Input Redirection Use ‘<‘ for input redirection command < input-file Input comes from input-file

Input Redirection Use ‘<‘ for input redirection command < input-file Input comes from input-file instead of keyboard. Examples: cat < tempfile mail waldenj <. bashrc CIT 140: Introduction to IT 5

Input Redirection CIT 140: Introduction to IT 6

Input Redirection CIT 140: Introduction to IT 6

Output Redirection Use ‘>‘ for output redirection command > output-file Output sent to output-file

Output Redirection Use ‘>‘ for output redirection command > output-file Output sent to output-file instead of screen. Examples: cat file 1 file 2 > mergefile find / -name “*. h” >headerfiles CIT 140: Introduction to IT 7

Output Redirection CIT 140: Introduction to IT 8

Output Redirection CIT 140: Introduction to IT 8

Output Redirection In a network environment, use the following command to sort on the

Output Redirection In a network environment, use the following command to sort on the server machine the file called datafile residing on your machine: ssh server sort < datafile CIT 140: Introduction to IT 9

Combining I+O Redirection command < input-file > output-file command > output-file < input-file Input

Combining I+O Redirection command < input-file > output-file command > output-file < input-file Input from input-file, output to output-file. Order of > and < operators does not matter. Example: cat < lab 1 > lab 2 cat takes input from lab 1 and sends output to lab 2. CIT 140: Introduction to IT 10

Redirection with File Descriptors File descriptor: a small integer that the UNIX kernel attaches

Redirection with File Descriptors File descriptor: a small integer that the UNIX kernel attaches with every open file standard input (sdin) — 0 standard output (stout) — 1 standard error (sderr) — 2 By making use of file descriptors, standard output and standard input can be redirected, using, 0< and 1> respectively. Example: $ grep “John” 0< tempfile CIT 140: Introduction to IT 11

Redirecting Standard Error command 2> error-file Errors sent to error-file instead of screen. Example:

Redirecting Standard Error command 2> error-file Errors sent to error-file instead of screen. Example: ls –l foo 2> error. log find / -name “*. h” 2>error. log CIT 140: Introduction to IT 12

Redirecting Standard Error CIT 140: Introduction to IT 13

Redirecting Standard Error CIT 140: Introduction to IT 13

Redirecting stdout and stderr Redirect stdout + stderr to same file using descriptors cat

Redirecting stdout and stderr Redirect stdout + stderr to same file using descriptors cat lab 1 lab 2 1> cat. output 2>cat. errors Send file descriptor 2 to fd 1 with 2>&1 cat lab 1 lab 2 lab 3 1>cat. output. errors 2>&1 cat lab 1 lab 2 lab 3 >cat. output. errors 2>&1 Send file descriptor 1 to fd 2 with 1>&2 cat lab 1 lab 2 lab 3 2>cat. output. errors 1>&2 CIT 140: Introduction to IT 14

Redirecting stdout and stderr CIT 140: Introduction to IT 15

Redirecting stdout and stderr CIT 140: Introduction to IT 15

Redirecting stdout and stderr CIT 140: Introduction to IT 16

Redirecting stdout and stderr CIT 140: Introduction to IT 16

Redirecting stdin, stdout, stderr command 0<input-file 1>output-file 2> error-file Command gets stdin from input-file

Redirecting stdin, stdout, stderr command 0<input-file 1>output-file 2> error-file Command gets stdin from input-file sends stdout to output-file sends stderr to error-file File descriptors 0 and 1 not required as they are default values sort 0<students 1>students. sorted 2>sort. error sort 2>sort. error 0<students 1>students. sorted CIT 140: Introduction to IT 17

Appending Output By default, output and error messages overwrite the contents of the destination

Appending Output By default, output and error messages overwrite the contents of the destination file. Append by using >> instead of using > Examples: cat memo letter >>stuff 2>error. log find / -name “*. h” >>files 2>>errors find / -name “*. h” >>find. output 2>&1 CIT 140: Introduction to IT 18

Noclobber Options > set -o noclobber > touch a > cat small. File >a

Noclobber Options > set -o noclobber > touch a > cat small. File >a bash: a: cannot overwrite existing file > set +o noclobber > cat small. File >a CIT 140: Introduction to IT 19

The null device > ls -l /dev/null crw-rw-rw- 1 root 1, 3 Oct 15

The null device > ls -l /dev/null crw-rw-rw- 1 root 1, 3 Oct 15 10: 11 /dev/null > cat small. File >/dev/null > cat /dev/null > find / -name “*. h” 2>/dev/null /usr/include/zconf. h … > ls –l small. File -rw-r--r-1 waldenj 1100 Oct 19 14: 13 small. File > cat /dev/null >small. File > ls -l small. File -rw-r--r-1 waldenj 0 Oct 29 20: 29 small. File CIT 140: Introduction to IT 20

UNIX Pipes (‘|’) Connects the stdout of one command to the stdin of another.

UNIX Pipes (‘|’) Connects the stdout of one command to the stdin of another. command 1 | command 2 | … | command. N Standard output of command 1 is connected to stdin of command 2, …, stdout of command N-1 is connected to stdin of command N. Filters: a special class of UNIX commands that take input from stdin process it and send it to stdout. I/O redirection and pipes can be used in a single command. CIT 140: Introduction to IT 21

UNIX Pipes (‘|’) CIT 140: Introduction to IT 22

UNIX Pipes (‘|’) CIT 140: Introduction to IT 22

UNIX Pipes (‘|’) CIT 140: Introduction to IT 23

UNIX Pipes (‘|’) CIT 140: Introduction to IT 23

Sending stdout+stderr to a pipe. 1. Send file descriptor 2 to fd 1. 2.

Sending stdout+stderr to a pipe. 1. Send file descriptor 2 to fd 1. 2. Use pipe as usual. Example: find / -name "*. h" 2>&1 | less CIT 140: Introduction to IT 24

Redirection and Piping combined command 1| tee file 1…file. N|command 2 Standard output of

Redirection and Piping combined command 1| tee file 1…file. N|command 2 Standard output of ‘command 1’ is connected to ‘stdin’ of tee, and tee sends its input to files ‘file 1’ through ‘file. N’ and as stdin on ‘command 2’ Example: cat names stuents | grep “John Doe” | tee file 1 file 2 | wc –l CIT 140: Introduction to IT 25

Redirection and Piping combined CIT 140: Introduction to IT 26

Redirection and Piping combined CIT 140: Introduction to IT 26

Redirection in the C Shell Input, output, and append operators(<, > , >>) same

Redirection in the C Shell Input, output, and append operators(<, > , >>) same in csh. The operator for error redirection is >& in the C Shell. command >& file Redirects the stdout and stderr of command to file. Examples: ls –l foo >& error. log Csh does not have an operator for redirecting stderr alone. Use >>& operator to redirect and append stdout + stderr. CIT 140: Introduction to IT 27

Redirection in the C Shell Allows stdout and stderr of a command to be

Redirection in the C Shell Allows stdout and stderr of a command to be attached to stdin of another command with |& operator. command 1 |& command 2 Send stdout + stderr of command 1 to command 2. Examples: cat file 1 file 2 |& grep “John Doe” file* |& wc –l CIT 140: Introduction to IT 28