CSCI 330 The UNIX System - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

CSCI 330 The UNIX System

Description:

User entered Laura Flowers. COMMAND LINE ARGUMENTS. Use arguments to modify ... turn off the special meaning of most wildcard characters and the single quote ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 61
Provided by: raimu
Category:
Tags: csci | unix | flowers | meaning | of | system

less

Transcript and Presenter's Notes

Title: CSCI 330 The UNIX System


1
CSCI 330The UNIX System
  • C Shell Programming

2
Steps to Create Shell Programs
  • Specify shell to execute program
  • Script must begin with ! (pronounced
    shebang)to identify shell to be executed
  • Examples
  • ! /bin/sh (defaults to bash)
  • ! /bin/bash
  • ! /bin/csh
  • ! /usr/bin/tcsh
  • Make the shell program executable
  • Use the chmod command to make the
    program/script file executable

CSCI 330 - The UNIX System
2
3
Example hello Script
  • ! /bin/csh
  • echo "Hello USER"
  • echo "This machine is uname -n"
  • echo "The calendar for this month is"
  • cal
  • echo "You are running these processes"
  • ps

CSCI 330 - The UNIX System
3
4
Example script output
  • chmod ux hello
  • ./hello
  • Hello ege!
  • This machine is turing
  • The calendar for this month is
  • February 2008
  • S M Tu W Th F S
  • 1 2 3 4 5 6 7
  • 8 9 10 11 12 13 14
  • 15 16 17 18 19 20 21
  • 22 23 24 25 26 27 28
  • You are running these processes
  • PID TTY TIME CMD
  • 24861 pts/18 000 hello.csh
  • 24430 pts/18 000 csh

5
Shell Logic Structures
  • Basic logic structures needed for program
    development
  • Sequential logic
  • User input
  • Decision logic
  • Looping logic
  • Case logic

6
Input to a C shell script
  • Reading/prompting for user input
  • Providing input as command line arguments
  • Accessing contents of files

7
Reading user input with
  • Use a special C shell variable
  • Reads a line from terminal (stdin)
  • up to, but not including the new line

  • 8
    Example Accepting User Input
    • ! /bin/csh
    • echo "What is your name?"
    • set name
    • echo Greetings to you, name
    • echo "See you soon"

    9
    Example Accepting User Input
    • chmod ux greetings
    • ./greetings
    • What is your name?
    • Laura Flowers
    • Greetings to you, Laura Flowers
    • See you soon

    User entered Laura Flowers
    10
    Command line arguments
    • Use arguments to modify script behavior
    • command line arguments become
    • positional parameters to C shell script
    • positional parameters are numbered variables
      1, 2, 3

    11
    Command line arguments
    • Meaning
    • 0 name of the script
    • 1, 2 first and second parameter
    • 10 10th parameter
    • prevents 1 misunderstanding
    • all positional parameters
    • argv the number of arguments

    12
    Example Command Line Arguments
    • ! /bin/csh
    • Usage greetings name1 name2
    • Input name1 and name2
    • echo 0 to you 1 2
    • echo Today is date 1 2
    • echo Good Bye 1 2

    13
    Example Command Line Arguments
    • chmod ux greetings
    • ./greetings Mark Flowers
    • ./greetings to you Mark Flowers
    • Today is Mon Feb 16 141803 CST 2008
    • Good Bye Mark Flowers

    0 greetings 1 Mark 2 Flowers
    14
    Decision logic
    • if Statement simplest forms
    • if ( expression ) command
    • if ( expression ) then
    • command(s)
    • endif

    15
    Decision logic
    • if-then-else Statement
    • if ( expression ) then
    • command(s)
    • else
    • command(s)
    • endif

    16
    Decision logic
    • if-then-else Statement
    • if ( expression ) then
    • command(s)
    • else if ( expression ) then
    • command(s)
    • else
    • command(s)
    • endif

    17
    Basic Operators in Expressions
    • Meaning
    • ( ) grouping
    • ! Logical not
    • ! equal to, not equal to
    • Logical or
    • Logical and

    18
    Expression examples
    • if ( 1 next ) echo 2
    • if ( argv ! 0 ) then
    • endif
    • if ( argv 0 argv
    • endif

    19
    Example Command Line Arguments
    • ! /bin/csh
    • if ( argv 0 ) then
    • echo -n "Enter time in minutes "
    • _at_ min
    • else
    • _at_ min 1
    • endif
    • _at_ sec min 60
    • echo min minutes is sec seconds

    20
    Example Reading file contents
    • ! /bin/csh
    • Usage lookup nameOrNumber
    • set list "users.txt"
    • if ( argv 0 ) then
    • echo -n "Enter name OR z-id "
    • set name
    • else
    • set name
    • endif
    • grep -i "name" list
    • if ( status ) echo "name not found"

    21
    File Testing operators
    • Syntax if ( -opr filename )

    22
    Example File Testing
    • if ( -e 1 ) then
    • echo 1 exists
    • if ( -f 1 ) then
    • echo 1 is an ordinary file
    • else
    • echo 1 is NOT ordinary file
    • endif
    • else
    • echo 1 does NOT exist
    • endif

    23
    C Shell looping constructs
    • predetermined iterations
    • repeat
    • foreach
    • condition-based iterations
    • while

    24
    Fixed number iterations
    • Syntax
    • repeat number command
    • executes command number times
    • Examples
    • repeat 5 ls
    • repeat 2 echo go home

    25
    The foreach Statement
    • foreach name ( wordlist )
    • commands
    • end
    • wordlist is
    • list of words, or
    • multi-valued variable
    • each time through,
    • foreach assigns the next item in wordlist to the
      variable name

    26
    Example foreach Statement
    • foreach word ( one two three )
    • echo word
    • end
    • or
    • set list ( one two three )
    • foreach word ( list )
    • echo word
    • end

    27
    Loops with foreach
    • useful to process result of command,
    • one at a time
    • Example
    • ! /bin/csh
    • _at_ sum 0
    • foreach file (ls)
    • set size cat file wc -c
    • echo "Counting file (size)"
    • _at_ sum sum size
    • end
    • echo Sum sum

    28
    The while Statement
    • while ( expression )
    • commands
    • end
    • use when the number of iterations is not known in
      advance
    • execute commands when the expression is true
    • terminates when the expression becomes false

    29
    Example while
    • ! /bin/csh
    • _at_ var 5
    • while ( var 0 )
    • echo var
    • _at_ var var 1
    • end

    30
    Example while
    • ! /bin/csh
    • echo -n "Enter directory to list "
    • set dirname
    • while ( ! -d dirname )
    • echo "dirname is not directory"
    • echo -n "Enter directory to list "
    • set dirname
    • end
    • ls dirname

    31
    loop control
    • break
    • ends loop, i.e. breaks out of current loop
    • continue
    • ends current iteration of loop, continues with
      next iteration

    32
    loop control example
    • ! /bin/csh
    • while (1)
    • echo -n "want more? "
    • set answer
    • if (answer "y") echo "fine"
    • if (answer "n") break
    • if (answer "c") continue
    • echo "now we are at the end"
    • end

    33
    loop control example
    • ! /bin/csh
    • while ( 1 )
    • echo -n "Enter directory to list "
    • set dirname
    • if ( -d dirname ) break
    • echo "dirname is not directory"
    • end
    • ls dirname

    34
    The switch Statement
    • Use when a variable can take different values
    • Use switch statement to process different cases
      (case statement)
    • Can replace a long sequence of
    • if-then-else statements

    35
    The switch Statement
    C shell compares string to each pattern until
    it finds a match
    • switch ( string )
    • case pattern1
    • command(s)
    • breaksw
    • case pattern2
    • command(s)
    • breaksw
    • endsw

    When a match is found, execute the command(s)
    until breaksw
    36
    The switch Statement
    • switch (string)
    • case pattern1
    • command(s)
    • breaksw
    • case pattern2
    • command(s)
    • breaksw
    • default
    • command(s)
    • breaksw
    • endsw

    When a match is not found, execute the commands
    following the default label
    37
    Example switch
    • switch (var)
    • case one
    • echo it is 1
    • breaksw
    • case two
    • echo it is 2
    • breaksw
    • default
    • echo it is var
    • breaksw
    • endsw

    38
    The switch Statement
    • if no pattern matches and there is no default,
      then nothing gets executed
    • do not omit the breaksw statement !
    • If you omit the breaksw statement, all the
      commands
    • under the next case pattern are executed until a
    • breaksw or endsw statement is encountered
    • pattern may contain wildcards
    • , ?,

    39
    Example switch greeting
    • ! /bin/csh
    • Usage greeting name
    • examines time of day for greeting
    • set hourdate
    • switch (hour4)
    • case 0
    • case 101
    • set greetingmorning breaksw
    • case 12-7
    • set greetingafternoon breaksw
    • default
    • set greetingevening
    • endsw
    • echo Good greeting 1

    40
    Example C Shell program
    • AVAILABLE OPTIONS
    • 1 Display today's date
    • 2 How many people are logged on
    • 3 How many user accounts exist
    • 4 Exit
    • Enter Your Choice 1-4

    41
    userutil shell script 1 of 2
    • ! /bin/csh
    • Usage userutil
    • while (1)
    • echo "AVAILABLE OPTIONS"
    • echo ""
    • echo "1 Display today's date"
    • echo "2 How many people are logged on"
    • echo "3 How many user accounts exist"
    • echo "4 Exit"
    • echo "Enter Your Choice 1-4"

    42
    userutil shell script 2 of 2
    • set answer
    • switch (answer)
    • case "1"
    • echo date breaksw
    • case "2"
    • echo users wc -w users are logged in
    • breaksw
    • case "3"
    • echo cat /etc/passwd wc -l users
      exists
    • breaksw
    • case "4"
    • echo "BYE"
    • break
    • breaksw
    • endsw
    • end end of while

    43
    Advanced C Shell Programming
    • Quoting
    • Here
    • Debugging
    • Trapping Signals
    • Functions ?
    • calling other scripts
    • exec, source, eval

    44
    Quoting
    • mechanism for marking a section of a command for
      special processing
    • command substitution ...
    • double quotes
    • single quotes
    • backslash \

    45
    Double quotes
    • prevents breakup of string into words
    • turn off the special meaning of most wildcard
      characters and the single quote
    • character keeps its meaning
    • ! history references keeps its meaning
    • Examples
    • echo " isn't a wildcard inside quotes"
    • echo "my path is PATH"

    46
    Single quotes
    • wildcards, variables and command substitutions
      are all treated as ordinary text
    • history references are recognized
    • Examples
    • echo ''
    • echo 'cwd'
    • echo 'echo hello'
    • echo 'hi there !'

    47
    backslash
    • backslash character \
    • treats following character literally
    • Examples
    • echo \ is a dollar sign
    • echo \\ is a backslash

    48
    The here Command
    • Example
    • ispell -l
    • I was running along quite nicely
    • when I was acosted by the mail man
    • whio insisted that my name is Raimund
    • but I did not believe him
    • DONE

    49
    Debugging Scripts
    • csh n scriptname
    • parse commands but do not execute them
    • csh v scriptname
    • Display each line of the script before execution
    • csh x scriptname
    • Displays each line of the script after variable
      substitutions and before execution
    • can also be added to shebang line !

    50
    Trapping Signals
    • any Unix process can be interrupted by a signal
    • common signal
    • C typed via keyboard
    • causes csh to terminate
    • can be trapped, i.e. other behavior specified
    • useful for cleanup upon forced exit

    51
    Trapping signal
    • Syntax
    • onintr label
    • execution continues at label if interrupt signal
      is received
    • onintr
    • ignore interrupt signal
    • onintr
    • restore previous interrupt signal behavior

    52
    onintr Example
    • ! /bin/csh
    • onintr label
    • while (1)
    • echo .
    • sleep 1
    • end
    • label
    • echo signal received

    53
    Divide and conquer
    • how to modularize a shell script
    • call Unix commands and utilities
    • call other scripts
    • as subshell
    • sourced
    • in place
    • evaluate strings to commands

    54
    Calling other scripts
    • as subshell, via
    • csh scriptname
    • scriptname
    • subshell does not see current shells variables
    • subshell sees current environment variables

    55
    example outer
    • ! /bin/csh
    • set var "outer"
    • setenv VAR "outer"
    • echo "outer var VAR"
    • csh inner
    • echo "outer var VAR"

    56
    example inner
    • ! /bin/csh
    • if ( ! ?var ) set var "unknown"
    • echo "inner var VAR"
    • set var "inner"
    • setenv VAR "inner"
    • echo "inner var VAR"

    57
    source other script no subshell
    • ! /bin/csh
    • set var "outer"
    • setenv VAR "outer"
    • echo "outer var VAR"
    • source inner
    • echo "outer var VAR"

    58
    exec other script no return
    • ! /bin/csh
    • set var "outer"
    • setenv VAR "outer"
    • echo "outer var VAR"
    • exec ./inner
    • echo "outer var VAR"

    59
    The eval Command
    • eval
    • evaluates string
    • executes resulting string
    • Example
    • set x 23
    • set y x
    • eval echo \y

    60
    Example The eval command
    • !/bin/csh
    • set A 1
    • set B 2
    • set C 3
    • foreach i (A B C)
    • eval echo \i
    • end
    Write a Comment
    User Comments (0)
    About PowerShow.com