17
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang [email protected]. edu

CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Embed Size (px)

DESCRIPTION

CS 497C – Introduction to UNIX Lecture 34: - Shell Programming. Chin-Chih Chang [email protected]. Sample Scripts. The script cpback.sh shown in Fig. 18.1 protects your files from accidental overwritting by the cp command. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

CS 497C – Introduction to UNIXLecture 34: - Shell Programming

Chin-Chih [email protected]

Page 2: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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.

Page 3: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Sample Scripts• The break statement in the inner loop quits

the loop after adding the line.• You should use “$@” with a for loop when

using multiword arguments. for file in “$@”• basename is a command to strip directory

and suffix from filenames$ basename hello.java .javahello

Page 4: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Sample Scripts• You can use basename inside a for loop to

change the extensions of files.for file in *.txt ; do leftname=`basename $file .txt` mv $file ${leftname}.docdone• The cpback2.sh script accepts multiple

filenames and backup them without overwritting.

Page 5: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

The Here Document (<<)• The here document (<<) 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 don’t

use a filename as argument or for running interactive programs noninteractively.

mail cs497c << EOFTest Date is `date`.EOF

Page 6: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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 sum=256+128; echo $sum• There is no space after variable. If you need

space, just quote the expression:

Page 7: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

let: Computation$ let sum=“3 * 6 + 4 / 2”; echo $sum$ x=22 y=28 z=5; z=$((x+y+z)); 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.

Page 8: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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.

Page 9: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Sub-Shells & Arrays

• The matching operators () run a group of commands in a sub-shell, but the {} don’t spawn one.

• Korn and bash support one-dimensional arrays where the first element has the index 0.

$ prompt[2]=“Enter your name”

$ echo ${prompt[2]}

Page 10: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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_arr[6]}

• 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 @ or * as subscript, you an display all the elements of the array as well as the number of elements.

Page 11: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Arrays & String Handling$ echo ${month_arr[@]}$ echo ${#month_arr[@]}• 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 #.$ name=“Jay Leno”; echo ${#name}• You can extract a substring:$ echo ${name:4:4}

Page 12: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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:

${variable:symbol string} where symbol can be +, -, = or ?.

• When the + option is used, variable is evaluated to string if it has a non-null value.

Page 13: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Conditional Parameter Substitution

found=`ls`; echo ${found:+“Files 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 [${x:=1} -le 10]• The ? option prints an error message and

exits the shell.grep $pattern ${flname:?”No file .. quitting”}

Page 14: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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.

Page 15: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

Shell Functionsanymore() {

echo $option "\n$1 ?(y/n) : \c" 1>&2

read response

case "$response" in

y|Y) echo 1>&2 ; return 0 ;;

*) return 1 ;;

esac

}

$ anymore “Wish to continue”

Page 16: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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.

$ prompt1=“User Name:”$ x=1; eval echo \$prompt$x• exec overlays the current shell when

prefixed to a command.

Page 17: CS 497C – Introduction to UNIX Lecture 34: - Shell Programming

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.