Whats Wrong With This Script - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Whats Wrong With This Script

Description:

Is the output from wc correct? What if one of the files specified is a directory? ... If you accidentally use only one , output_filename will be overwritten with the ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: Kenneth4
Category:

less

Transcript and Presenter's Notes

Title: Whats Wrong With This Script


1
Whats Wrong With This Script?
  • !/bin/sh
  • for filename
  • do
  • if test -d filename
  • then
  • echo filename is a directory
  • else
  • cat filename
  • fi
  • done

2
What if I use it like this?
  • clyde myscript.sh file1-9.txt wc -w
  • Is the output from wc correct?
  • What if one of the files specified is a
    directory?

3
Using STDERR
  • Whenever one of your scripts outputs prompts or
    error messages, they should use standard error,
    not standard out
  • This keeps your script from "creating data"
  • This keeps your script from injecting spurious
    "noise" into a pipeline

4
Standard Files and Device Files
  • A commands standard output is a default place
    for it to send output
  • A commands standard input is a default place to
    receive input
  • Standard error is the default place to send
    errors from a command
  • The Unix treatment of files as a stream of bytes
    extends to I/O devices also. They are a special
    type of file known as a device file.
  • Device files are commonly found in the /dev
    directory

5
Redirection
  • Redirection encompasses the various ways you can
    cause the shell to change where a command gets
    its standard input from or sends its standard
    output to
  • By default, these two files are associated with
    the terminal but the user can change this
    behavior
  • Standard error is also normally associated with
    the terminal but it also can be redirected

6
Redirecting Standard Output
  • The gt operator is used to redirect standard
    output
  • command options arguments gt output_filename
  • command is any executable program that doesnt
    hardcode its output destination
  • options and arguments are the same as you are
    familiar with
  • output_filename is the place (file) that you want
    the output of command saved in
  • If output_filename doesnt exist, it is created.
    If output_filename does exist, it is overwritten
    and the original contents are destroyed! (Unless
    the shell variable "noclobber" is set.
  • gt! over-rides "noclobber"

7
Appending Standard Output
  • The gtgt operator gets around the last problem
  • command options arguments gtgt output_filename
  • gtgt appends standard output to output_filename,
    preserving whatever was originally there and
    appending the new information
  • CAUTION You must ensure that you use both gtgt
    symbols. If you accidentally use only one gt,
    output_filename will be overwritten with the new
    data and all previous contents will be lost!

8
Redirecting Standard Input
  • Redirection of standard input works in a similar
    manner
  • lt is the standard input redirection operator
  • command options arguments lt input_filename
  • command is any executable program
  • options and arguments are the same as you are
    familiar with
  • input_filename is the place (file) that contains
    the data you want to use for input to command
  • Obviously, input_filename must exist

9
Using Input and Output Redirection
  • Standard input and standard output redirection
    can be used together on the same command
  • command options arguments lt input_filename gt
    output_filename
  • or
  • command options arguments lt input_filename gtgt
    output_filename

10
Redirecting Standard Error
  • Under the C shell, you can redirect both standard
    output (STDOUT) and standard error (STDERR) by
    using the gt or gtgt symbols followed by an
  • If you dont do this, commands like find may
    clutter up your screen with error messages while
    you are trying to accomplish other work
  • find / -name stdlib.c -print gt whereitis
  • or
  • find / -name stdlib.c -print gtgt whereitis

11
Redirecting STDERR STDOUT
  • You can also redirect STDIN and STDOUT to
    separate files using the Bourne shell (sh)
  • A file descriptor is associated with each of the
    standard files
  • STDIN is file descriptor 0
  • STDOUT is file descriptor 1
  • STDERR is file descriptor 2
  • Using the file descriptor, we can redirect STDOUT
    and STDERR independently or together

12
Bourne Shell Examples
  • To redirect STDERR
  • command 2gt file
  • To redirect STDERR and STDOUT to the same place
  • command gt file 2gt1
  • Note that order is paramount!
  • command 2gt1 gt file is NOT the same thing!
  • The first redirect sends STDOUT (file descriptor
    1) to file and then redirects file descriptor 2
    (STDERR) to the same place as file descriptor 1
  • The second sends file descriptor 2 (STDERR) to
    the same place as file descriptor 1 (STDOUT, the
    tty) and then redirects file descriptor 1 to file

13
How Do I Do This In My Scripts?
  • For a Bourne shell script, you can use
  • echo "This is a message" gt2
  • For Korn shell, you can use
  • print "This is a message" gt2
  • print -u2 "This is a message"
  • For awk, you can use something like
  • 'print "What a nice day!" gt "/dev/tty" '

14
Correct Way to Script
  • !/bin/sh
  • for filename
  • do
  • if test -d filename
  • then
  • echo filename is a directory gt2
  • else
  • cat filename
  • fi
  • done
Write a Comment
User Comments (0)
About PowerShow.com