CS 497C - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS 497C

Description:

CS 497C Introduction to UNIX Lecture 34: Shell Programming – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 18
Provided by: Kindy
Category:
Tags: 497c | leno

less

Transcript and Presenter's Notes

Title: CS 497C


1
CS 497C Introduction to UNIXLecture 34 -
Shell Programming
  • Chin-Chih Changchang_at_cs.twsu.edu

2
Sample Scripts
  • The script cpback.sh shown in Fig. 18.1 protects
    your files from accidental overwritting by the cp
    command.
  • The script dentry1.sh shown in Fig. 18.2 accepts
    a code and its corresponding description from the
    terminal, performs some rudimentary validation
    checks, and then adds an entry to a file
    desig.lst.
  • The continue statements let you reenter the data
    or start a fresh cycle.

3
Sample Scripts
  • The break statement in the inner loop quits the
    loop after adding the line.
  • You should use _at_ with a for loop when using
    multiword arguments.
  • for file in _at_
  • basename is a command to strip directory and
    suffix from filenames
  • basename hello.java .java
  • hello

4
Sample Scripts
  • You can use basename inside a for loop to change
    the extensions of files.
  • for file in .txt do
  • leftnamebasename file .txt
  • mv file leftname.doc
  • done
  • The cpback2.sh script accepts multiple filenames
    and backup them without overwritting.

5
The Here Document (ltlt)
  • The here document (ltlt) provides input to a script
    from the script itself.
  • It can be used with both command substitution and
    variables.
  • It is often used with commands that dont use a
    filename as argument or for running interactive
    programs noninteractively.
  • mail cs497c ltlt EOF
  • Test Date is date.
  • EOF

6
let Computation A Second Look (ksh and bash)
  • Some advanced features of shell programming only
    happen in Korn and bash shell.
  • Korn shell is located in /usr/bin/ksh. Bash shell
    is located in /bin/bash.
  • You can compute with let statement
  • let sum256128 echo sum
  • There is no space after variable. If you need
    space, just quote the expression

7
let Computation
  • let sum3 6 4 / 2 echo sum
  • x22 y28 z5 z((xyz)) echo z
  • When a process is created by the shell, it makes
    certain features of its own environment to the
    child.
  • The created process make use of the inherited
    parameters including
  • The PID of the parent process.
  • The UID (owner) and GUID (group owner) of the
    process.

8
Sub-Shells
  • The current working directory.
  • The three standard files.
  • Other open files used by the parent process.
  • Some environment variables available in the
    parent process.
  • A variable defined in the parent is visible in
    the child only when it is exported (export).
  • However, when the child alters the value of the
    variable, the changed value is not seen by the
    parent.

9
Sub-Shells Arrays
  • The matching operators () run a group of commands
    in a sub-shell, but the dont spawn one.
  • Korn and bash support one-dimensional arrays
    where the first element has the index 0.
  • prompt2Enter your name
  • echo prompt2

10
Arrays
  • You can use a space-delimited list enclosed with
    parentheses
  • month_arr(0 31 28 31 30 31 30 31 31 30 31 30 31)
  • echo month_arr6
  • In the older version of Korn, you can use the set
    A statement
  • set A mon_arr 0 31 28 31 30 31 30 31 31 30 31 30
    31
  • Using the _at_ or as subscript, you an display all
    the elements of the array as well as the number
    of elements.

11
Arrays String Handling
  • echo month_arr_at_
  • echo month_arr_at_
  • The dateval.sh script in Fig. 19.2 use arrays to
    validate an entered date.
  • The length of string can be found by preceding
    the variable name with a .
  • nameJay Leno echo name
  • You can extract a substring
  • echo name44

12
Conditional Parameter Substitution
  • Shell variables can be evaluated in a conditional
    manner depending on whether they are assigned a
    non-empty value. This is called parameter
    substitution with the following format
  • variablesymbol string where symbol can be ,
    -, or ?.
  • When the option is used, variable is evaluated
    to string if it has a non-null value.

13
Conditional Parameter Substitution
  • foundls echo foundFiles found."
  • When - option is used, variable is evaluated to
    string if it has a null value.
  • The operator additionally assigns a value to
    the variable.
  • while x1 -le 10
  • The ? option prints an error message and exits
    the shell.
  • grep pattern flname?No file .. quitting

14
Shell Functions
  • A shell function consists of a group of
    statements that are executed together as a bunch.
  • Optionally, it also returns a value
  • function_name()
  • statements
  • return value
  • mainfunc.sh displays some functions.

15
Shell Functions
  • anymore()
  • echo option "\n1 ?(y/n) \c" 1gt2
  • read response
  • case "response" in
  • yY) echo 1gt2 return 0
  • ) return 1
  • esac
  • anymore Wish to continue

16
eval exec
  • eval processes a command line twice and is used
    to simulate arrays and execute.
  • With eval, you can create generalized numbered
    prompts and variables that significantly compact
    code.
  • prompt1User Name
  • x1 eval echo \promptx
  • exec overlays the current shell when prefixed to
    a command.

17
exec trap
  • exec date
  • To debug shell scripts, use set x at the
    beginning of the script so that every command
    line is echoed to the screen.
  • Use trap if you want your script to respond to an
    interrupt in a specific way.
  • trap command_list signal_list
  • It is useful in removing temporary files when a
    script receives a signal.
Write a Comment
User Comments (0)
About PowerShow.com