You are on page 1of 14

HiPath 4000 Manager/Assistant XSWC E3

Shell scripting

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Shell Linux OS interaction Relation of kernel, shell, utilities and applications Shell functionality Unix Filesystem

Example: Linking
Process terminal communication Shell scripting: Contents

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Relation of kernel, shell, utilities and applications

terminal or shell script user commands and data output Shell Built-in commands transfer of control cat ls vi System-calls: requests for services Unix Kernel and Device Drivers output Interactive commands handle own input as well as output

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Shell functionality
print prompt and wait for user input wait termination of the child process (waitpid)

read cmd line and interpret it to the cmd and its arguments

parent

cmd type ?

external command

call fork to create a child process

pid ?

internal command

Different process but attached in the parent tty (spawning)

child

low-level kernel system-calls from shell process to execute kernel built-in cmd

execute exec system-call with argument the external cmd: this replaces the child process with the external cmd process external cmd does its job, prints the results in the tty and terminates

print to the tty the results returned by the kernel

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Filesystem (1)
disk drive partision partision partision

filesystem boot blocks(s) super blocks(s)

i-list

directory blocks and data blocks

i-node

i-node

...

i-node

A file has attributes, and data: Each file is associated with an i-node, which holds most of its attributes Actual contents of the file are stored in one or more data blocks A directory is just a file.

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Filesystem (2)
directory blocks and data blocks i-list data block data block directory block data block directory block

1st

2nd

3rd

i-node i-node

i-node

i-node
i-node number

filename

i-node number

filename

Directory entry: i-node number, filename File information (attributes) is in: i-node (most information): type, access permission bits, size, pointers to the data blocks, other A file can be linked to more than one directories
RC - Greece Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Filesystem (3)
directory blocks and data blocks i-list directory block directory block

i-node 0

i-node 222106

i-node 225476

225476 . 222106 . .

/home/nik/testdir # ls -i 225476 . 222106 .. /home/nik # ls -i 222106 . 228033 test1 8912 .. 229097 test2 228834 demo 225476 testdir

222106 .
i-node number

..

225476 testdir

How to find the name of the current directory: . i-node = 225476 directory block . . i-node of parent directory = 222106 access directory block of parent directory filename with i-node 225476 = testdir
RC - Greece Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Linking
Hard link:
ln filename linkname ls -i 225475 filename 225475 linkname ls -l -rw-r--r-- 2 root root 29 Jan 4 13:33 filename -rw-r--r-- 2 root root 29 Jan 4 13:33 linkname Same file (filename), only difference the filename in the directory table entry

Symbolic link
ln -s filename linkname ls -i 225475 filename 227093 linkname ls -l -rw-r--r-- 1 root root 29 Jan 4 13:25 filename lrwxrwxrwx 1 root root 8 Jan 4 13:25 linkname -> filename Two different files. linkname contains 8 bytes which is the string filename. It points to filename Removing the filename linkname points nowhere ! ls -l linkname lrwxrwxrwx 1 root root 8 Jan 4 13:38 linkname -> filename cat linkname cat: linkname: No such file or directory RC - Greece Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Process terminal communication


/dev/tty F.D. 1: std output F.D. 2: std error shell F.D. 0: std input

When Kernel starts a process it sets up its individual open files. The open files are special files that a process reads from or write to. Default open file for stdin, stdout and stderr is /dev/tty. When a process opens a file, Kernel returns a FD for this file. The process uses this number (FD) to speak with Kernel. Default FDs for the open files are 0: stdin, 1: stdout and 2: stderr. cat somefile something grep something somefile grep something somefile > grepout grep something somefile nofile 2> grepout </etc/passwd nl >$HOME/yyy
RC - Greece Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Process terminal communication


When a program wants to read or write to a file it must ask the operating system for a file stream that has an integer file descriptor associated with it. Every process has 3 file descriptors associated with it opened by default: stdin, stdout, stderr.

user process

read/write .. from /dev/..

stream head
(system call interface)

return file descriptors

kernel device driver


(or pseudo device driver)

Stream: full duplex path between a user process and a device driver. Stream devices are character special files in /dev/ User process requires a read/write. Kernel returns a FD. User process interface with kernel with stream head. Device driver interface with the device with the device driver.
RC - Greece

device
(in/out queues)

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Shell Scripting: Contents


1. BASICS Filename generation Redirection Variables Miscellaneous INTERPRETATION OF THE COMMAND LINE Interpretation process The 10 steps of Shell interpretation The eval command The type of a command Alias variables THE VI EDITOR Characteristics The most important commands Options
Gkalelis Nikolaos XSWC E3

2.

3.

RC - Greece

Development Innovations & Projects D.I.&P.

Shell Scripting: Contents


4. SHELL PROCESSES Process functionality Calling a process Variables User input Issuing messages Variable and parameter substitution Setting variable attributes CONTROL INSTRUCTIONS Overview End status of a command The test command Atithemetic operations FLOW CONTROL The if statement The operators || and && The case statement
Gkalelis Nikolaos XSWC E3

5.

6.

RC - Greece

Development Innovations & Projects D.I.&P.

Shell Scripting: Contents


7. LOOP STRUCTURES Overview Statements while and until Redirection in loop constructions The select statement USEFUL TOOLS The getopts command Here script Signal handling Debugging FUNCTIONS Function definition Calling a function Function utilization Differences among Shells

8.

9.

RC - Greece

Gkalelis Nikolaos XSWC E3

Development Innovations & Projects D.I.&P.

Shell Scripting: Contents

10. REGULAR EXPRESSIONS Applying regular expressions Special expressions in regular expressions

11. SED & AWK Overview Introduction of sed Introduction to awk

Training will be based in the regarding document, on-line examples and exercises.

RC - Greece

Gkalelis Nikolaos XSWC E3