More about Shell Script - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

More about Shell Script

Description:

... parameter a directory name and prints the file system structure under this ... is the time we wait between printing the file size # (in seconds) if ( $2 ) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 10
Provided by: a15159
Category:
Tags: more | prints | script | shell

less

Transcript and Presenter's Notes

Title: More about Shell Script


1
Lecture 6
  • More about Shell Script

2
Review Shell Variables
  • Special shell variables
  • Name Description
  • (For passing parameters)
  • 1 - 9 the positional parameters. (shiftn1
    becomes n handling more than 9 parameters)
  • 0 the name of the command that is being
    executed
  • the number of positional arguments
  • (for process control)
  • ? the exit status of the last command executed
  • the process number of this shell
  • ! the process id of the command run in the
    background.

3
Example 4
  • Task Write a script that accepts an optional
    command line parameter a directory name and
    prints the file system structure under this
    directory in the form of a tree. W/o parameters,
    it should start in the current directory. Call
    this script dirtree.csh

4
Example Result of dirtree.csh
  • dirtree
  • ------bin
  • ------.garbage
  • ------file1
  • ------examples
  • ------arg
  • ------average.csh
  • ------fileinfo.csh
  • ------hw6.sh
  • ------lab3

5
Script dirtree.csh
  • !/bin/csh -f
  • start at current dir, if no param.
  • if(argv 0) then
  • set thisdir"."
  • else
  • set thisdirargv1
  • endif
  • if(?TREEPREFIX) then
  • set prefix"TREEPREFIX"
  • else
  • set prefix""
  • endif
  • echo "prefix"
  • set filelistls -A thisdir
  • foreach file (filelist)
  • echo "prefix------file"
  • if(-d "thisdir/file") then
  • if(file filelistfilelist) then
  • setenv TREEPREFIX
  • "prefix "
  • else
  • setenv TREEPREFIX
  • "prefix "
  • endif
  • one level down the recursion ladder
  • 0 "thisdir/file"
  • endif
  • end
  • echo "prefix"

6
Example 5 watchfile.csh
  • Task write a script that watches a file with du.
    perhaps useful if you have a slow internet
    connection and want to track the progress of a
    large download. Won't stop until you press C!
  • Usage is watchfile filename delay_interval
  • if delay_interval is not specified, the script
    pauses 5 seconds between each time it shows the
    file size and elapsed time.

7
Example result of watchfile.csh
  • ./watchfile.csh fileinfo.csh 2
  • watching the file fileinfo.csh
  • Starting time 213721
  • 2 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 4 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 6 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 8 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 10 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 12 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • 14 seconds FILE fileinfo.csh SIZE 1 Kbytes
  • C

8
Script watchfile.csh
  • !/bin/csh -f
  • make sure there is at least 1 command line
    parameter
  • if ( argv 0 ) then
  • echo "usage watchfile filename
    delay_interval"
  • exit
  • else
  • record the file name we are supposed to
    watch
  • set file1
  • endif
  • make sure it is really a file
  • if (! -f file ) then
  • echo "Error file does not exist or is not a
    file!"
  • exit
  • endif
  • this variable is the time we wait between
    printing the file size
  • (in seconds)
  • if ( 2 ) then
  • we got 2 parameters, use the second as the
    delay interval
  • set delay2
  • else
  • set delay5
  • endif
  • get the initial hour, min and second
  • set ihourdate H
  • set imindate M
  • set isecdate S

9
Script watchfile.csh(II)
  • while ( 1 )
  • _at_ ikbytes dkbytes
  • sleep delay
  • get the current hour, min and second
  • _at_ hourdate H
  • _at_ mindate M
  • _at_ secdate S
  • compute elapsed time
  • _at_ dhour hour - ihour
  • _at_ dminmin - imin
  • _at_ dsecsec - isec
  • _at_ kbytesdu -L -k file cut -f1
  • _at_ dkbytes kbytes - ikbytes
  • if (dkbytes gt 1) then
  • _at_ elapsed dhour 3600 dmin 60
    dsec
  • echo -n "elapsed seconds "
  • echo " FILE " file " SIZE " kbytes
    "Kbytes"
  • else
  • exit
  • set hour 0
  • set min 0
  • set sec 0
  • compute elapsed time
  • set dhour 0
  • set dmin 0
  • set dsec 0
  • set elapsed 0
  • set ikbytes du -L -k file cut -f1
  • set kbytes ikbytes
  • set dkbytes 0
  • echo "watching the file 1"
  • echo "Starting time ihouriminisec"
  • now go in to a loop that never
  • ends, except the size is not
Write a Comment
User Comments (0)
About PowerShow.com