Shell Scripting (C shell) - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Shell Scripting (C shell)

Description:

* * * * * After the csh program starts, it is programmed to execute two files in the user s directory: These files allow users to initialize their own environments. – PowerPoint PPT presentation

Number of Views:319
Avg rating:3.0/5.0
Slides: 20
Provided by: siz6
Category:
Tags: scripting | shell

less

Transcript and Presenter's Notes

Title: Shell Scripting (C shell)


1
Shell Scripting (C shell)
  • SungHo Maeung (sxm3011_at_cs.rit.edu)
  • 10/27/2000Tutorial section
  • Computer Science Lab

2
Contents
  • C shell
  • First Script
  • Getting User Input
  • Command Line Arguments
  • Conditionals and File Testing
  • Switch Loops
  • Programming with C Shell
  • mailingList
  • Summary Resources

3
C Shell
  • ! /bin/csh
  • set name hello -gt echo name
  • array ( element1 elemsnt2 )
  • array1 , array2

4
C Shell First Script
  • LOGNAME
  • Print users name
  • unix command
  • Enclosed in back quotes
  • Performed command substitution
  • Ex) date
  • uname n
  • Display the machine name
  • ps ef grep LOGNAME
  • Users processes are printed
  • chmond ux greetme

5
C shell Getting User Input
  • lt
  • user input (std) without newline
  • array (name)
  • An array is created
  • Arithmetic ( operators ) _at_ integer
  • , - , / , , , ltlt , gtgt
  • , -, , /, , --
  • Ex) _at_ sum 46
  • Space is required after _at_

6
Cshell Debugging scripts
  • Echo (-x) and Verbose (-v)
  • csh x scriptname (command )
  • Display each line after variable substitution and
    before execution
  • set echo , unset ( in the file )

7
Cshell command line argument
  • 0 - The name of the script
  • 1, 2, 3,..10
  • Parameters are referenced by number
  • - All positional parameters
  • argv1, argv2
  • The first argument , second
  • argv or argv all arguments
  • argv The number of arguments (size)
  • argvargv The last argument

8
Cshell Conditional constructs
  • , ! , lt, gt , gt , lt , !
  • , !
  • String matches ex) ans Yy
  • ,
  • Logical operator

9
Cshell if examples
  • If( expression ) then
  • Command
  • else
  • Command
  • endif
  • Ex) if ( argv ! 1) then
  • echo 0 requires an argument
  • exit 1
  • endif

10
Cshell if expression
  • set name ltIf( name ! ) then
  • grep name data file
  • endif
  • If( answer Yy ) then
  • mail bob lt message
  • else
  • mail jon lt datafile
  • endif
  • set x 1 , y 2, z 3If( (x y) !
    z) then echo TRUE
  • If( argv 0 ) exit 1

11
Cshell if command
  • If (command) then command endif
  • If ( who grep 1 gt /dev/null ) then echo
    1 is logged onendif
  • startoverset grade ltif( grade lt 0
    grade gt 100 ) then echo Illegal grade goto
    startover
  • endif

12
Cshell if file testing
  • if( ! e file ) file exists
  • if( -d file ) a direcotry file
  • if( -f file ) a plain file
  • if( ! z file ) zero length
  • if( -r file -w file) readable and writable
  • if( -x file ) executable

13
Cshell Switch
  • set color ltswitch (color)case bl echo
    I feel color breakswdefault echo color not
    one of them breakswendsw

14
Cshell for
  • foreach variable (wordlist) commandsend
  • foreach person ( bob sam sue fred) mail person
    lt letterend
  • foreach person (cat maillist) mail person
    ltltEOF Hi person EOFend

15
Cshell while
  • set num 0while (num lt 10 ) echo num _at_
    numend
  • repeate takes two argument( number, command)
  • Ex) repeate 3 echo hellohellohellohello

16
Cshell While
  • shift command
  • Shifts the argv array by one word
  • Ex) While( argv) echo argv shift
    end loop a b c a b c b c c

17
Cshell While
  • break continue command set answer lt
    while( answer ! Mm ) echo Wrong\! Try
    again. set answer lt if( answer Mm)
    break end

18
Programming with C shell
  • mailingList
  • data
  • Hello XXX,
  • friends ( emails )

19
Resources
  • Tutorial sites
  • http//tobias.fritz.net/cs/csh-programming.htmlA
  • http//www.msi.umn.edu/dudley/cshell/programming.
    html
  • http//dpleated.ee.ic.ac.uk/unix/old/shell/cshell/
    prog/intro.html
Write a Comment
User Comments (0)
About PowerShow.com