CSCI 330 THE UNIX SYSTEM Shell Data Handling

  • Slides: 8
Download presentation
CSCI 330 THE UNIX SYSTEM Shell Data Handling: Redirection and Piping

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

OUTPUT CSCI 330 - The UNIX System The output statement of the shell is

OUTPUT CSCI 330 - The UNIX System The output statement of the shell is the “echo” command Syntax: echo [option] arg 1 arg 2 …arg. N its arguments can be strings or variables option “-n” will suppress trailing newline 2

OUTPUT WITH ECHO Examples: CSCI 330 - The UNIX System % echo “Hello Ray"

OUTPUT WITH ECHO Examples: CSCI 330 - The UNIX System % echo “Hello Ray" Hello Ray % echo “Hello $USER" Hello a 132436 % echo “It is now `date`” It is now Mon Feb 25 10: 24: 08 CST 2008 3

OUTPUT REDIRECTION (>) Syntax: command > file Sends output of command to file, instead

OUTPUT REDIRECTION (>) Syntax: command > file Sends output of command to file, instead of to terminal Calls the disk usage command for the Examples: CSCI 330 - The UNIX System % du > status current directory and redirects the output to a file called ‘status’ % (date; du) > 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 4

INPUT REDIRECTION (<) Syntax: Command < file Command will read (take input) from file,

INPUT REDIRECTION (<) Syntax: Command < file Command will read (take input) from file, instead of from terminal Example: CSCI 330 - The UNIX System % tr “[A-Z]” “[a-z]” < report. input 5

EXAMPLES: OUTPUT / INPUT Redirecting input and output: % tr ‘[A-Z]’ ‘[a-z]’ < r.

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 CSCI 330 - The UNIX System 6

APPENDING OUTPUT If CSCI 330 - The UNIX System Syntax: command >> file adds

APPENDING OUTPUT If CSCI 330 - The UNIX System Syntax: command >> file adds output of command at the end of file does not exist, shell creates it Examples: % date > usage-status % ls -l >> usage-status % du -s >> usage-status Build the file ‘usage-status’ from the output of the ‘date’, ‘ls’, and ‘du’ commands 7

SUMMARY: REDIRECTIONS AND PIPE Meaning command < file Redirect input from file to command

SUMMARY: REDIRECTIONS AND PIPE Meaning command < file Redirect input from file to command > file Redirect output from command to file command >> file Redirect output of command appends it to file command 1 | command 2 Take/pipe output of command 1 as input to command 2 CSCI 330 - The UNIX System Command Syntax 8