Docsity

Shells, Shell Programming - Lecture Notes | COMP 6262, Study notes of Computer Science

Partial preview of the text

Download Shells, Shell Programming - Lecture Notes | COMP 6262 and more Study notes Computer Science in PDF only on Docsity!

  • COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if,debug March 13,

Outline {^

shells

{^

shell programming^ z

passing arguments (KW Ch.7) z exit status z if (KW Ch.8)

{^ test z^

debug

core shell functionality {^

built-in commands {^

scripts {^

variables (local, environment) {^

redirection {^

wildcards {^

pipes {^

sequences {^

subshells {^

background processing {^

command substitution

common shells {^

the shell you use is a matter of taste, power,compatibility, and availability {^

C shell is better than Bourne shell for interactivework, but slightly worse in some respects forscript programming {^

Korn shell incorporates the best of features of theBourne shell and C shells {^

Bash also “best of all worlds”, includes featuresfrom all the other major shells {^

Bourne shell comes with every version of UNIX {^

Bash is the newest, is becoming the mostpopular, open software

changing shell {^

chsh

{^

tcsh

{^

ksh

So Many Commands… {^

Unix has a lot of commands, but there isno way it has everything {^

What do you do if no command existsthat does what you want?^ z^

Build it yourself! z^

The shell itself is actually a programminglanguage z^

A shell program/script consists of a sequentiallist of commands^ {

think of it as a listing of programs to run inorder

Example {^

Check whether a user is logged on

Command Line Parameters {^

Just like all other Unix programs,shell scripts can read parameters offof the command line

{^

Different from user input^ z

Value is known before execution, nottyped in during execution

{^

Example:^ >commandsFile.sh param1.test

Example {^

Check whether a user is logged on

{^

Command file

ison

z^

who

|^

grep $

{^

Usage^ z

./ison 6262-

Shift {^

only first 9 arguments are directly accessible via$1, $2, …, $ {^

for 10 or above use ${10}, ${11}, etc. {^

if more are supplied use shift command to accessthem^ z^

Shifts the arguments to the left and brings newvalues in from the right

z^

z^

z^

z^

z^

z^

$# is automatically decremented by one

{^

example: testShift

exit status {^

Every program and shell returns aninteger value to indicate success orfailure^ z

0 - Success z Any other number – Failure z Used to inform other programs howthings went

exit status {^

variable $?^ z^

the exit status of the last command executed {^

in a pipeline, the exist status is that ofthe last command in the pipe {^

examples^ z^

who | grep 6262- z^

echo $? z^

who | grep good z^

echo $? z^

echo $?

The test Command {^

built-in shell command

{^

to check if certain conditions in an ifcommand

{^

Usage:

test

expression

z^

test evaluate

expression

z^

if the result is true, returns an exitstatus of 0 z^

otherwise, the result is false, returns anonzero exit status

Shortcut For Test {^

Because this is used very often, ashortcut exists {^

[]^ z^

[ is the name of the command z^

still initiates execution of the same testcommand, only that test expects to see aclosing ] z^

spaces after [ and before ] {^

example^ z^

test –f tFile z^

[ -f tFile ]