UNIX Wéber André. - 2 - Objective of the training Scope of the course Unix commands & scripting....

Post on 16-Dec-2015

222 views 0 download

Transcript of UNIX Wéber André. - 2 - Objective of the training Scope of the course Unix commands & scripting....

UNIX

Wéber André

- 2 -

Objective of the training

Scope of the course

Unix commands & scripting.

Objectives

Give an overview of what is UNIX

Acquire knowledge of main Unix commands

Be able to write/modify Unix scripts.

- 3 -

Methods and tools

Training session

Unix principles, commands and ksh scripts.

Examples and stories

Open discussion

Exercises

Individual progression.

No obligation to do all exercises.

Complement of information on internet.

Ask other trainees and the trainer.

- 4 -

Methods and tools

The objectives of the training is to be comfortable with UNIX as advanced user

or as developer.

A separated training is dedicated to Unix administration.

- 5 -

Agenda

1. First day Introduction What is an OS and what is Unix Principles of Unix The command shell The file system and related commands

2. Second day Pattern matching (regular expressions) Processes and related commands Scripting with ksh

3. Third day, advanced topics Programming tools Networking Introduction on Perl – if we have enough time

- 6 -

Introduction

This training is part of the training plan of developers and other technical people on projects.

You will receive an overview of what is Unix, information & exercises about useful commands & tools, introduction to scripting.

After this training, you should be comfortable with the usual usage of Unix, with scripting in KSH and you will be able to find yourself supplementary information if needed.

- 7 -

General scheme of this set of slides

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 8 -

What is an Operating System (OS).

An OS take in charge the management of resources

The memory

The devices (hard disk, printer, …)

The allocation of the processor – preemptive or not – using static or dynamic priorities

The screen / the keyboard

An OS gives you ways to transmit orders to your system and to receive results.

Command processor (or shell).

GUI.

External access

An OS can have several characteristics:

Mono/Multi user

Mono/Multi task

Real Time

Management of virtual memory (swapping).

Management of priorities for processes.

- 9 -

History of Unix

1969 Unix is born at AT&T Bell Laboratories (Fathers : Dennis Ritchie, Ken Thomson). Written mainly in assembler for a PDP7.

1973 Unix is rewritten mainly in C to be moved on a PDP11.

1974 Unix is distributed freely to Universities.

1978 - … : wide distribution in the private sector.

1978 – 1984 : new versions (evolution and forks).

1984 - 1996 : standardizations

Today : different Unix OS more or less compatible (ISO/IEC 9945): Solaris, Linux, AIX, XENIX, BSD, …

- 10 -

History of Unix – simplified family tree

Source: http://www.doc.ic.ac.uk/~wjk/UnixIntro/Lecture1.html

- 11 -

General scheme of this set of slides

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 12 -

Login to Unix.

User / password

On the local console

With telnet

With a X-Window terminal

With a X-Window simulator

- 13 -

Login to Unix with telnet from a PC.

Command on your PC : telnet host

ex: telnet CW4

- 14 -

General scheme of this set of slides

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 15 -

Command processors

Bourne shell (sh)

Korn shell (ksh)

C shell (csh)

shellCommands, scripts and programs

Libraries

OS Kernel

Machine resources

- 16 -

Shell roles

To invite the user to give & edit a command.

To offer facilities : aliases, resolution of patterns related to file names / file paths, ...

To interpret the command.

Internal commands

execution

External commands

Localization

Loading

execution

To interpret scripts.

To display results.

- 17 -

Structure of a command line

<command> : <command name> <option list> <argument list>

<option list> : [+-] <option> <option list>

[+-] <option> <option value> <option list>

<empty>

<argument list> : <argument> <argument list>

<empty>

<argument> : [^ ]+

“.*”

- 18 -

Generic expressions in the command or in arguments

KSH adds patterns :

?(pattern) matches zero or one times the pattern.*(pattern) matches any time the pattern.+(pattern) matches one or more time the pattern.@(pattern) matches one time the pattern.!(pattern) matches string without the pattern. pattern == simple pattern or different alternative patterns: pattern1|pattern2|…

Wildcards for the shell :

? : Any one character

* : any string of characters

[ABC] : one char amongst ‘A’, ‘B’ or ‘C’

[A-Z] : one char amongst ‘A’,’B’,…,’Z’

[!abcde] : any char not in ‘a’,..,’e’

- 19 -

Special chars for the shell

\ : escape the special meaning of the next char

"<expression>" : do not interpret wildcards but interpret "\", "$" and "’"

‘<expression> ' : do not interpret any special chars

`<expression> ` : execute the expression and take its output as value of replacement

$<variable name> or ${<variable name>} : replace with the value of the corresponding variable

- 20 -

Execution of a command by the shell

1. Resolution of variables in the command line.

2. Resolution of wildcards Search of file name(s) corresponding to the resolution of wildcards If no file name found => leaves the argument as it If file names found => replaces the expression with one argument by file

name.

3. Search for a file corresponding to the command name By path if the command is given with a path. Otherwise, searches in the following order in

aliases, functions, internal commands, files in directories referred in the PATH variable.

4. For an external command, executes the file found if the “execute” right has been given to the current user. For an internal command, executes it directly.

- 21 -

Delimitation of the end of a command

Chars recognized by the shell as end of command:

end of line

“;” : execution of the next command (if any) in sequence.

“|” : pipe.

“&” : execution in back-ground.

“>”, “>>”, “<“, “2>”, 2>&1 : redirections

- 22 -

Execution context of a command

<command> : By a sub-shell

. <script> : in the current shell

<shellCnd> <scriptName> : by the shell specified

exec <shellCnd> : the new shell (or program) replace the current shell

- 23 -

Combining commands

<cnd1> && <cnd2> : execution of «cnd2 » only if « cnd1 » has an exit value equal to 0 (0 == true).

 <cnd1> || <cnd2> : execution of «cnd2 » only if « cnd1 » has an exit value different then 0.

(<cnd1>;<cnd2>) : grouping of “cnd1” and “cnd2” executed in sequence.

<cnd1> & <cnd2> : “cnd1” is launched as a separated process running in background and after “cnd2” is executed in the current process.

- 24 -

Standard IO & redirections.

Standard IO:

STDIN, number 0

STDOUT, number 1

STDERR, number 2

Redirections of I/O of commands:

<commandName> <args> > <fileName> : the normal output (STDOUT) of the command is redirected to the file (if the file exists this content is scratched).

<commandName> <args> > <fileName> 2>&1 : the error output (STDERR) is redirected to the normal output.

<commandName> <args> >> <fileName> : the normal output of the command is redirected to the file and concatenated to the existing content of this file.

<commandName> <args> < <fileName> : the content of the file is used as input to the command (STDIN).

- 25 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 26 -

Shell variables

Predefined vars

Local vars (not inherited).

Exported vars (inherited by commands executed by the shell).

Set a value :

<varName>=<value>

Get value :

$<varName>

${<varName>}

Get length :

${#<varName>}

List defined variables:

set : local variables.

env : environment variables.

Declaration of variables with typeset

typeset [-<option(s)>] <varName>[=<value>]

options:

l : lower case string

u : upper case string

i : integer

x : exported

r : read only (constant)

R<n> : right justify string

L<n> : left justify string

- 28 -

Predefined variables of the shell

HOME : home directory

PATH : paths for search of executable files

TERM : terminal type

PS1 : first prompt

PS2 : second prompt

PWD : working directory

! : PID of last launched command (sub-process).

? : exit value of last launched command

$ : PID of the current process

- 29 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 30 -

Unix - Notion of users

Users are organized in groups.

Every UNIX user belongs to one or more groups.

Each user belongs to a primary group that is stored in the /etc/passwd file.

The composition of user groups are stored in the file /etc/group.

The connected user may change his/her current group (via the command “newgrp”).

The connected user may change his/her password (“passwd” command).

A connected user may ask a new connection as an other user : “su <userId>”

There is one (and only one) super-user identified as “root”.

Notions of real/effective user or group

- 31 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 32 -

Principles related to the file system.

There is only one tree with all files and directories. Different disks can be inserted (mounted) in this tree.

A file is a sequence of bytes.

A text file line ends by a LF char.

A directory is a collection of files and of directories => tree of directories.

There is a “root” to the file system : /

A each moment, there is a “current directory” for each shell (in fact, for each process).

A path to a file can be absolute (begins with “/”) or related to the current directory.

Each directory has two predefined entries : “.” (itself), “..” its parent.

Info on devices are included in the file system; see /dev

Devices are block devices or byte streams.

A null output is provided : /dev/null

- 33 -

Principles related to the file system. …

Each file is protected via “r” (“Read”), “w” (“Write”), “x” (“Execute”) access modes: for the owner, for the users of the group of the owner, for the others.

Each directory is protected via “r” (see content), “w” (change composition), “x” (go through) access modes: for the owner, for the users of the group of the owner, for the others.

To see rights : ls –l tt.ksh

To change rights : chmod 755 t.ksh chmod u+rwx,g+rx-w,o+rx-w t.ksh chmod a=rx,u+w t.ksh(u= user (owner), g = owner’s group, o = others, a = all).

Principles related to the file system. …

A new created file (or directory) receives requested right filtered by the “umask”.

Sticky bit : if set on a directory, a directory item can be deleted or renamed only by the owner of this item or by the owner of the directory (or by “root”). chmod +t . chmod 1755 .

Set user id bit (suid) / set group id bit (sgid) : for an executable, asks execution as the owner or in the group of the

owner (modification of the effective UID/GID). chmod 6755 t.ksh chmod u+s t.ksh chmod g+s t.skh

for a directory, if the sgid is set, new items get the group of the parent directory.

- 35 -

Principles related to the file system. … iNodes

There is an unique table of inodes by device. In this table, each node is referred by its index.

An iNode is a structure of information containing for each file or directory:

Access rights (RWX/RWX/RWX).

UID of the owner

GID of the owner

File size in bytes.

Last access date and time.

Last modification date and time.

Last change date and time (modification of the iNode).

Number of references.

Number of blocks.

Links to each disk block.

A directory is a list of structures containing:

The indice of an iNode.

The name of the file / directory.

A directory also contains :

A reference on the parent directory

A reference on itself

- 36 -

Principles related to the file system. … iNodes

Usual directories

/ : root

/bin : low level programs (executable files)

/etc : system configuration and information files

/lib : libraries and programs for compilers

/tmp : temporary files

/dev : special “files” for I/Os (peripheral devices)

/usr : users commands (installed programs)

/usr/adm : administration

/usr/bin : commands and tools

/home : root of user’s directories

- 38 -

Commands - Actions on the file system

mount : attachs a disk structure in the file system

unmount : detachs a disk

cd : changes directory

mkdir : creates a directory

rmdir : removes a directory

cp : copies a file

mv : moves a file

ln : creates a link to a file

ln –s : creates a symbolic link to a file or a directory

rm : removes a file, link or directory

- 39 -

Commands - Actions on the file system …

chmod : changes rights

umask : sets the “mask” of rights for creation.

touch : modifies dates in the iNode.

chown : changes owner

chgroup : changes owner group

- 40 -

Commands - Info on the file system

df : displays info on devices

du : disk usage (display number of blocks used)

istat : displays info from an inode

find : finds a file

ls : lists the content of a directory

file :tries to find the type of a file

- 41 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 42 -

Principles - Process

A process is an instance of a program in execution.

A process has

An ID (PID)

Group and user owner

A parent process

A process owns to a group of processes.

A process obtains the rights corresponding to its owner.

Technically, a process is created by a call to “fork” (often followed by a call to “exec” in the child process).

On KSH, some predefined variables give us the PID of the current process ($$), the PID of the last sub-process launched ($!), : the exit value of the last launched sub-process ($?).

Principles – memory space allocated to a process

code

Static data

Heap

Stack

Principles - Status of a process

Sleeping : S

Runnable : R

Running : O

Stopped : T

Waiting : W

Principles – communication between processes

Files

Pipe

Named pipes

Signals

Message queues

Semaphores

Shared memory

Sockets

IPC: Inter-Process Communication

- 46 -

Commands - processes

ps : lists processes

kill : kills a process or a list of processes (in fact, send a signal)

nohup : asks for continuation after log-out.

nice : gives a low priority

wait : waits the end of child processes

at : differed launch of processes.

cron : regularly execution of commands (at specified hours).

fg: foreground

bg: background

jobs:lists background tasks

- 47 -

Command – processes …

top : lists processes consuming most system resources.

kill : sends a signal to a process

-1 SIGHUP the associated terminal is disconnected

-2 SIGINT interrupt (CTRL-C), can be intercepted

-9 SIGKILL urgency stop; no interception

-15 SIGTERM normal stop, default signal send by the “kill” command

ctrl-z interrupt (SIGTSTP)

ctrl-c stop (SIGINT)

fuser <filePath> command : displays which processes are accessing a file

- 48 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 49 -

Commands – filters - Principle

cnd1 cnd2 cnd3pipe pipe

STDIN STDOUT

The different processes linked by a pipe are executed concurrently and they process the flow line by line.

STDOUT STDIN

- 50 -

Commands - filters

cat : sends STDIN on STDOUT

more : displays “page by page”

grep : selects lines matching criteria

awk : executes command on lines matching criteria

head : displays first lines

tail : displays last lines

sort : sorts lines

wc : counts words / lines / chars / …

tee : outputs the input into a file and into the STDOUT.

sed : stream editor

tr : translates chars

cut : selects columns

- 51 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 52 -

Commands – Text processing

cat : concatenates content of a text files and display the result

ed / sed / ex : line editors

vi – emacs : screens editors

grep / egrep : extract lines of a file matching a pattern

awk : executes instructions on lines of a file.

nroff / troff: text formatters ; fonts & styles, lines, paragraphs, pages

lp : formats text for a line printer

pg : paging

tbl : formats tables

tr : translates chars

- 53 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 54 -

Commands - Miscellaneous

echo : prints argument

whence/which <commandName> : give the path to a command

pwd : prints working directory

date : displays the current date & time

time : gives information about execution time of a command

cmp : compare two files

diff : displays the diff of two files

split/cplit : split a file.

tar : creates an archive

compress / uncompress – pack/unpak : compression tools.

eval : evaluates KSH command produced dynamically.

- 55 -

Commands - Miscellaneous

uname : displays the version of your UNIX system

who : lists connected users.

who am I : displays information about of the current user.

finger : lists connected users with user names

lpstat : lists info on line printers

basename : gives the file name from a path

dirname : gives the parent directory path from a file/directory path

od : displays the content of a file, in octal notation.

mail : sends mail

talk : direct talk with an other connected user

write : sends directly a message to on other connected user.

man : consultation of the UNIX manual

- 56 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

Commands for programmers

Compilation cc : C compiler cb : Cobol compiler

Check Syntax lint : check syntax of C programs

Linkage ld : link object files to create an executable file

Build make : manage dependencies

Debugging dbx : a symbolic debugger for programs written in C, C++, Pascal or Fortran

Commands for programmers

Source control rcs sccs cvs subversion

Generation of code yacc lex

- 59 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands

Login on Unix

Commands principles (shells, structure of a command, execution, redirections, …)

Shell variables

Notion of user& command related to user-management

The file-system principles & commands related to the file-system

Processes & commands related to processes

Filters principles et commands

Text processing

Miscellaneous commands

Commands for programmers

KSH Networking

- 60 -

Where are we now?

Introduction

What it is an OS?

History of Unix

Unix

Principles and related commands

KSH

Features

Configuration

Scripting

Networking

- 61 -

ksh features

alias and functions capabilities;

command history mechanism

command-line editing

automatic completion mechanism for file names, directories names and full pathnames

integrated programming features

scripting language

the “~” character is interpreted as being the home directory of the current user.

- 62 -

ksh features … alias

Defines a name associated to a command

Predefined or user defined

To declare an alias : alias <aliasName>=‘<cnd>’ or alias –x <aliasName>=‘<cnd>’ (the –x option must be added to export the alias to the sub-shells)

To remove the definition of an alias : unalias <aliasName>

To display existing aliases : alias

To Execute an alias, you just have to type the alias name as command name.

Example:

alias ll=”ls –l” create an alias called “ll” on the command “ls –l”

- 63 -

ksh features … functions

Definition

function <functionName>

{

<functionBody>

}

OR

<functionName>() {

<functionBody>

}

Call

<functionName> <list of arguments>

- 64 -

ksh features … functions

FPATH environment variable contains a semicolon-separated list of directories to search for function definitions when an undefined function is executed.

autoload <functionName>, forces a reload of the definition of the function at the next call.

A function may return an numeric value (via the command “return <value>”) that the caller can read in the var “$?”.

You may define local scoping of variables in a function using the command “typeset”), otherwise variables are global. Example:

subfunc(){typeset var

echo sub: var starts as $var '(empty)' var=2 echo sub: var is now $var}var=1echo var starts as $var, before calling function '"subfunc"'subfunc # calls the functionecho var after function is now $var

- 65 -

ksh features … history of commands

Previous commands are stored in the file « ~/.sh_history ».

The « fc » command allows you to list, edit and re-execute previous commands.

fc –l : list history

fc –e : edit and re-execute a command

Get previous command in inline editor, In vi mode:

ESC k or ESC - : get previous command

ESC j or ESC + : get next command

- 66 -

ksh features … completion of a file name in a command or in an argument

This feature is rarely used but very useful to spare time. It consists to ask the shell to complete the beginning of file name you have just typed with the rest of the actual name. It will works only if a unique match exists.

In vi mode, you have to type “ESC \” to request the completion.

In emacs mode, you have to type “ESC ESC” to request the completion.

You may also obtain the list of file names matching the beginning of the name you have typed:

In vi mode and in emacs mode, you have to type “ESC =” to request the completion.

You have also the possibility to recall arguments used in the previous command. See http://www.softpanorama.org/Scripting/Shellorama/command_completion.shtml for more information.

- 67 -

ksh features … history of commands

Previous commands are stored in the file « ~/.sh_history ».

The « fc » command allows you to list, edit and re-execute previous commands.

fc –l : list history

fc –e : edit and re-execute a command

Get previous command in inline editor, In vi mode:

ESC k or ESC - : get previous command

ESC j or ESC + : get next command

For more information, see http://unixhelp.ed.ac.uk/shell/ksh_hist.html

- 68 -

Where are we now?

Introduction

What it is an OS?

History of Unix

Unix

Principles and related commands

KSH

Features

Configuration

Scripting

Networking

- 69 -

Configuration of ksh

Choice of the shell at log-in : command “chsh” (info stored into etc/passwd).

Scripts automatically executed at log-in by the current shell: “/etc/profile” (common to all users) and “~/.profile” (specific to each user).

It is also possible to ask the execution of a script (often named “.kshrc”) via the variable “ENV”.

Mode of command-line edition

set –o vi

set –o emacs

Definition of the prompt, for example on CW4:

PS1=[${HOST}:${USERNAME} ${PROJET} ${enviro}][$PWD]

- 70 -

Extract of .profile

if [ -s "$MAIL" ] # This is at Shell startup. In normal

then echo "$MAILMSG" # operation, the Shell checks

fi # periodically.

export FPATH=/SOFTWARE/glo

autoload se

se pk p

mesg y

export PATH=$PATH':/u/thb/prog'

alias adm='cd $adm'

alias b='ps -ef | grep -v \ 0\:'

- 71 -

Where are we now?

Introduction

What it is an OS?

History of Unix

Unix

Principles and related commands

KSH

Features

Configuration

Scripting

Networking

- 72 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

- 73 -

Scripting with ksh – general structure of a script

First line of the script file : #!/usr/bin/ksh

Commented lines : # …..

Commands : internal/external commands

End of command : end of line or “;”

Command on several lines with “\” at the end of the lines.

- 74 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

- 75 -

Scripting with ksh - arguments

$* : list of all arguments

$# : number of arguments

$0 : command name

$<i> : argument i

shift : shift argument list

- 76 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

- 77 -

Scripting with ksh - loops

forfor var [in list]

do

done

whilewhile command

do

done

untiluntil command

do

done

select : loops on selections done in a menu break : exits of current loop continue : resumes to the next iteration of the current loop

- 78 -

Scripting with ksh - branching

Based on only one condition

if <condition>

then

else

fi

Multiple choices

case $<var> in

<proposition1> ) <cnd>;;

<proposition2> | <proposition3> ) <cnd>;;

<pattern>) <cnd>;;

*) <cnd>;; # default

esac

- 79 -

Scripting with ksh – exceptions

To execute a command when a signal occurs :trap <command> <signalName>

Or

trap <command> <signalNumber>

To execute a command when the execution of a command returns an error :trap <command> ERR

To execute a command at the exit of the shell :trap <command> 0

Or

trap <command> EXIT

To abandon the trapping:trap - <signal>

Commonly used signals : 1 : HUP 2 : INT 15 : TERM 30 (on AIX) : USR1 – the number may vary, it’s better to use to signal name 31 (on AIX) : USR2 – the number may vary, it’s better to use to signal name

- 80 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

- 81 -

Scripting with ksh … testing

Tests :

Condition true if the command returns 0 as exit value

Special Commands for tests :

On files (« test <expression> » or “[[ <expression> ]]” ) -a <fileOrDirName> : true if the file or the directory exists

–r <fileOrDirName> : true if the current user has the “R” permission on the file or dir.

–w <fileOrDirName> : true if the current user has the “W” permission on the file or dir.

–x <fileName> : true if the current user has the “X” permission on the file or dir.

–f <fileName> : true if the file exists

–d <dirName> : true if the directory exists

–L <linkName> : true if the symbolic link exists

–s <fileOrDirName> : true if the file or dir exists and is not empty

- 82 -

Scripting with ksh … testing

On string expressions : « test <expression> » or “[[ <expression> ]]” s1 = s2 : true if s1 is equal to s2

s1 != s2 : true if s1 is different than s2

s1 : true if s1 is not empty

In the comparisons, we may use KSH regular expressions.

?(pattern) matches zero or one times the pattern.

*(pattern) matches any time the pattern.

+(pattern) matches one or more time the pattern.

@(pattern) matches one time the pattern.

!(pattern) matches string without the pattern.

Pattern == simple pattern or different alternative patters : pattern1|pattern2|…

- 83 -

Scripting with ksh … testing …

On numeric expressions (integer), form 1 : « test <expression> » v1 –eq v2

v1 –ne v2

v1 –gt v2

v1 –ge v2

v1 –lt v2

v1 –le v2

On numeric expressions (integer), form 2 : « (( <expression> ))» (( v1 == v2 ))

(( v1 != v2 )) or (( !(v1 == v2) ))

(( v1 > v2 ))

(( v1 >= v2 ))

(( v1 < v2 ))

(( v1 <= v2 ))

- 84 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

- 85 -

Scripting with ksh … Integer arithmetic

For variables declared as integer (typeset -i varName) : direct usage of arithmetic expressions:

i = $i +1

For variables not declared as integer

(( i = $i + 1 ))

or

i = `expr $i + 1`

or

i=$(( $i + 1 ))

It can vary depending of the compatibility of your shell.

- 86 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

Variable expansion

${varname} : if varname exists, returns its value otherwise returns an empty string

${varname:-word} : If varname exists and isn't null, returns its value; otherwise returns word.

${varname:=word} : If varname exists and isn't null, returns its value; otherwise set it to word and then returns its value.

${varname:?message} : If varname exists and isn't null, returns its value; otherwise prints varname: message, and aborts the current command or script. Omitting message produces the default message parameter null or not set. Note, however, that interactive shells do not abort.

${varname:+word} : If varname exists and isn't null, returns word; otherwise returns null.

Pattern-Matching Operators when you get variable content

${variable#pattern} : If the pattern matches the beginning of the variable's value, deletes the shortest part that matches and returns the rest.

${variable##pattern} : If the pattern matches the beginning of the variable's value, deletes the longest part that matches and returns the rest.

${variable%pattern} : If the pattern matches the end of the variable's value, deletes the shortest part that matches and returns the rest.

${variable%%pattern} : If the pattern matches the end of the variable's value, deletes the longest part that matches and returns the rest.

- 89 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Here document technique

Arrays

Miscellaneous

Networking

“Here document” technique

In a script, to automate the caption of input values (e.g. a shell script that asks for a password), the “here document” technique can be used. It consists of delimiting all the required inputs in a “virtual document” directly included in your script.

Example:

sqlplus /nolog << EOFconnect prl/prlselect count(1) from cwd01;exit;EOF

- 91 -

Scripting with ksh … Arrays

Index from 0 to 4096

Setting all values

set –A arr “hello” “world”

Setting some values

typeset arr[1]=“hello” arr[2]=“world”

Set one value

arr[3]=“hello”

Get a single value

${arr[3]}

Get the list of all values

${arr[*]}

Get size

${#arr[*]}

- 92 -

Where are we now?

Introduction What it is an OS? History of Unix

Unix Principles and related commands KSH

Features

Configuration

Scripting General Structure of a script

Arguments of the execution of the script

Loop structures, branching, exceptions

Tests

Integer arithmetic

Variable expansion

Arrays

Miscellaneous

Networking

- 93 -

Scripting with ksh … miscellaneous commands

read : gets user input on STDIN

set : replaces $1,$2,… variables

print : same function as echo, outputs a message on STDOUT

exit [<returnCode>] : exits the current script (return to the caller)

exec : open a file or executes a command in place of the current shell.

- 94 -

Scripting with ksh … debugging

The simplest way to debug a ksh script is to execute it using the « -x » option (or –xv options).

ksh –x ./scriptName

You may also modify the first line of your script:

#!/usr/bin/ksh -x

It has no effect on code lines of functions; to debug functions, place « set –x » in the first line of each functions to debug.

It exists some debuggers for ksh but there are not delivered by default on main distributions of Unix.

KSH – difference between Unix & Linux.

Due to proprietary reasons, LINUX uses the public domain ksh (pdksh) while UNIX uses ksh. These two Korn shells have a different behavior with pipelines.

On Linux, the right part of a command delimited by a pipe (”|”) is executed by a sub-shell. The consequence is that, on Linux, if you modify the value of a global variable in this part, the rest of your script won’t view this modification.

- 96 -

Where are we now?

Introduction

What it is an OS?

History of Unix

Unix

Principles and related commands

KSH

Networking

- 97 -

Networking – standard protocols

IP : addressing.

TCP/IP : Transport Communication Protocol – communication point to point in connected mode.

UDP : User Datagram Protocol – message-based connectionless protocol. Simple delivery of packets.

Sockets : end-point of a bi-directional communication

FTP : File Transfert Protocol

Telnet : TELecommunication NETwork

SMTP : Simple Mail Transfer Protocol, standard for email transmissions across the Internet.

NFS : Network File System. Mount distant file system as local (communicate through the network).

- 98 -

Networking – parametrization

/etc/hosts : definition of other machines – mapping from name to address.

/etc/networks : for routing, lists the names and addresses of your own, and other, networks.

/etc/services : definition of network services

/etc/protocoles : list of active protocols

/etc/netmasks : mask on IP addresses

- 99 -

Networking – tools

ping : tests the connection to an IP address

ftp : file transfer

telnet : simulation of a console with a communication via a network.

rlogin (similar to telnet), rsh, rcp : remote commands

slogin, ssh, scp : idem but more secure (use SSH, a secure network protocol)

inetd : server process for the Internet standard services.

netstat : information on the network

traceroute : traces path from your machine to an other one.

uucp : unix to unix copy

kermit : transfer of files

- 100 -

Networking administration

ifconfig : configures network interfaces

netstat : gives statistics on connected machines

rwho : lists users connected through the network

That’s all folks!