CSCI 330 The UNIX System

8
CSCI 330 THE UNIX SYSTEM Shell Data Handling: Redirection and Piping

description

CSCI 330 The UNIX System. Shell Data Handling: Redirection and Piping. Output. The output statement of the shell is the “echo” command Syntax: echo [option] arg1 arg2 …argN its arguments can be strings or variables option “-n” will suppress trailing newline. Output with echo. Examples: - PowerPoint PPT Presentation

Transcript of CSCI 330 The UNIX System

Page 1: CSCI 330 The UNIX System

CSCI 330THE UNIX SYSTEM

Shell Data Handling: Redirection and Piping

Page 2: CSCI 330 The UNIX System

OUTPUT

The output statement of the shell is the “echo” command

Syntax: echo [option] arg1 arg2 …argN its arguments can be strings or variables option “-n” will suppress trailing newline

2

CS

CI 330 - T

he UN

IX S

ystem

Page 3: CSCI 330 The UNIX System

OUTPUT WITH ECHO

Examples:% echo “Hello Ray"Hello Ray% echo “Hello $USER"Hello a132436% echo “It is now `date`”It is now Mon Feb 25 10:24:08 CST 2008

3

CS

CI 330 - T

he UN

IX S

ystem

Page 4: CSCI 330 The UNIX System

OUTPUT REDIRECTION (>)

Syntax: command > fileSends output of command to file, instead of to terminal

Examples:% du > status

% (date; du) > status

4

CS

CI 330 - T

he UN

IX S

ystem

Calls the disk usage command for the current directory and redirects the output to a file called ‘status’

( ) indicates command groups. Use it to combine the output of multiple commands. In this example, we place time and date in front of the disk usage

Page 5: CSCI 330 The UNIX System

INPUT REDIRECTION (<)

Syntax: Command < fileCommand will read (take input) from file, instead of from terminal

Example:% tr “[A-Z]” “[a-z]” < report.input

5

CS

CI 330 - T

he UN

IX S

ystem

Page 6: CSCI 330 The UNIX System

EXAMPLES: OUTPUT / INPUT

Redirecting input and output:% tr ‘[A-Z]’ ‘[a-z]’ < r.in > r.out

Output of command becomes input to next:% ls > temp.txt; wc < temp.txt

Eliminate the middleman: pipe% ls | wc

6

CS

CI 330 - T

he UN

IX S

ystem

Page 7: CSCI 330 The UNIX System

APPENDING OUTPUT

Syntax: command >> fileadds output of command at the end of file If file does not exist, shell creates it

Examples:% date > usage-status% ls -l >> usage-status% du -s >> usage-status

7

CS

CI 330 - T

he UN

IX S

ystem

Build the file ‘usage-status’ from the output of the ‘date’, ‘ls’, and ‘du’ commands

Page 8: CSCI 330 The UNIX System

SUMMARY: REDIRECTIONS AND PIPE

8

CS

CI 330 - T

he UN

IX S

ystem

Command Syntax Meaning

command < file Redirect input from file to command

command > file Redirect output from command to file

command >> file Redirect output of command and appends it to file

command1 | command2 Take/pipe output of command1 as input to command2