Lecture Four - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Lecture Four

Description:

Lecture Four – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 15
Provided by: john51
Learn more at: https://www.cse.scu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture Four


1
Lecture Four
  • Unix Shell Programming II

2
More If Example
  • send an email to a user on local system
  • if test 1 why 1?
  • then
  • echo Usage 0 username 1gt2 exit 1
  • fi
  • groups 1 gt /dev/null 2gt1
  • if ((? ! 0))
  • then
  • echo User 1 is invalid exit 1
  • fi
  • mail 1

3
For and While Example
  • c1
  • for i
  • do
  • echo "Arg c i"
  • cexpr c 1
  • done
  • c1
  • while (( ! 0))
  • do
  • echo "Arg c 1"
  • cexpr c 1
  • shift
  • done

4
Until Example
  • PATH/bin/use/bin
  • case in
  • 0) echo Usage 0 1gt2 exit 1
  • esac
  • until who egrep 1
  • do
  • sleep 60
  • done

5
Case Statement
  • case word in
  • pattern) commands
  • pattern) commands
  • esac

6
Case Example
  • PATH/bin/usr/bin
  • for i in _at_
  • do
  • echo -n i? gt /dev/tty
  • read response
  • case response in
  • y) echo i
  • q) break
  • esac
  • done

7
Why Use _at_
  • showeacharg a b c
  • and _at_ are the same when not enclosed by
    double quotes
  • showeacharg has for i in echo arg i
    done
  • output
  • arg a
  • arg b
  • arg c

8
Why Use _at_
  • one word
  • showeacharg has for i in echo arg i
    done
  • output
  • arg a b c
  • _at_ same as the args passed to the script
  • showeacharg has for i in _at_ echo arg i
    done
  • output
  • arg a b
  • arg c

9
Trap
  • Whats wrong with the script
  • PATH/bin/usr/bin
  • new/tmp/wwho1.
  • old/tmp/wwho2.
  • gtold
  • while is a built-in command that
    returns true, like command true
  • do
  • who gt new
  • diff old new
  • mv new old
  • sleep 60
  • done awk /gt/ 1 in print
  • /lt/ 1 out print

10
Use Trap to Clean up
  • PATH/bin/usr/bin
  • new/tmp/wwho1.
  • old/tmp/wwho2.
  • trap rm -f new old exit 1 1 2 15
  • gtold
  • while
  • do
  • who gt new
  • diff old new
  • mv new old
  • sleep 60
  • done awk /gt/ 1 in print
  • /lt/ 1 out print

11
Script with a Flaw
  • We want to do
  • sed s/UNIX/UNIX(TM)/g doc.txt overwrite
    doc.txt
  • Overwrite script
  • PATH/bin/usr/bin
  • case in
  • 1)
  • ) echo Usage 0 file 1gt2 exit 1
  • esac
  • new/tmp/0.
  • trap rm -f new exit 1 1 2 15
  • cat gt new
  • cp new 1
  • rm -f new

12
Almost Right Answer
  • PATH/bin/usr/bin
  • case in
  • 1)
  • ) echo Usage 0 file 1gt2 exit 1
  • esac
  • new/tmp/0.1.
  • old/tmp/0.2.
  • trap rm -f new old exit 1 1 2 15
  • cat gt new
  • cp 1 old
  • trap 1 2 15
  • cp new 1
  • rm -f new old

13
The Reason
  • What would happen if we have syntax error
  • sed s/abc/ABCg doc.txt overwrite doc.txt

14
The Right Answer
  • OPATHPATH
  • PATH/bin/usr/bin
  • case in
  • 01) echo Usage 0 file cmd args 1gt2 exit
    1
  • esac
  • file1shift
  • new/tmp/0.1.old/tmp/0.2.
  • trap rm -f new old exit 1 1 2 15
  • if PATHOPATH _at_ gt new
  • then
  • cp file old
  • trap 1 2 15
  • cp new file
  • else
  • echo 0 1 failed, file unchanged gt 1gt2
  • exit 1
  • fi
  • rm -f new old
Write a Comment
User Comments (0)
About PowerShow.com