Linux Shell
Linux Shell
Shell scripting
RC - Greece
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
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
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
pid ?
internal command
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
RC - Greece
Filesystem (1)
disk drive partision partision partision
i-list
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
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
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
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
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
user process
stream head
(system call interface)
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)
2.
3.
RC - Greece
5.
6.
RC - Greece
8.
9.
RC - Greece
10. REGULAR EXPRESSIONS Applying regular expressions Special expressions in regular expressions
Training will be based in the regarding document, on-line examples and exercises.
RC - Greece