Unix Fundamentals & System Programming

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Unix Fundamentals

&

System Programming

History of UNIX
Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s Two versions of UNIX that emerged are AT&T Unix and BSD Unix

In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4)
Two of the main standards mainly in use are POSIX(Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium which developed vendor-neutral Xwindow System.
Copyright Datagrid Technologies UNIX Basics 2

File system

dev

bin

tmp

home

etc

var

lib

usr

sh
console lp0 ls user1

inittab passwd user2

spool

bin

src

Commands

Listing contents of directory(ls)


ls Syntax : ls [options] [file.] options: -l list in long format -a list all file including those beginning with a dot -i list inode no of file in first column -R recursively list all sub directories -F mark type of each file -C display files in columns

Listing contents of directory


$ ls l total 6 -rwxr-xr-x drwxr-xr-x -rw-r--r--rw-------rw-r--r-1 2 1 1 1 user1 user2 user1 user1 user3 projA projD projA projA projC 12373 4096 12831 61440 255 Dec Dec Dec Dec Dec 15 22 12 15 20 14:45 14:00 13:59 11:16 14:29 a.out awkpro c core cs

File access permissions File type

User id Link count Group id

File size in bytes

Date & time of modification

File name

cat
cat command takes the input from the keyboard, and sends the output to the monitor We can redirect the input and output using the redirection operators
$ cat>file1 Type the content here <ctrl d> $ cat file1 Displays the content of the file $cat>>file1 (will append to the content of the file)

echo & read


echo command is used to print output to the screen $echo This is an example This is an example $x=10 $echo $x 10 $_ read command allows to read input from user and assign it to the variable specified. $read x RNS $echo $x RNS

grep
grep -Global Regular Expression Printer is used for searching regular expressions Syntax
grep <options> <pattern> <filename(s)>

grep options
-c displays count of the number of occurrences

-n
-v -i

displays line numbers along with the lines


displays all lines except lines matching pattern Ignores case for matching

10

Patterns
* - matches 0 or more occurrence of the previous character [^pqr] - Matches a single character which is not p ,q or r ^pqr -Matches pqr at the beginning of the line pqr$ -Matches pqr at the end of the line .- Matches any one character

11

Filter command - head


Displays the top n lines of the file

Can specify top n lines to be displayed


$ head -3 file1

12

Filter command - tail


Displays the end of the file Can specify last n lines to be displayed $ tail -3 file1

Can also specify the line number from which the data has to be displayed $ tail +5 file1

13

Filter command - tr
tr - translate filter used to translate a given set of characters usage :
$tr [a-z] [A-Z] <filename

14

Filter command - tr
Useful options for tr -s char
Squeeze multiple contiguous occurrences of the character into single char

-d char
Remove the character

15

Filter command - cut


Used to extract specified columns from the output of certain commands -c used to extract characters -d Delimiter for fields -f Field no. Examples $cut c 2-5 file1 $cut -d | f 2,3 file1
16

Shell Scripts
when a group of commands have to be executed regularly, they should be stored in a file, and the file executed as a shell script or a shell program. it is not mandatory using the .sh extension for shell scripts; makes them easy to match with the wild cards. shell script needs to have execute permission when invoked by its name.

Sample shell script


#! /bin/bash # # The above line has a special meaning. It must be the # first line of the script. It says that the commands in # this shell script should be executed by the bash # shell (/bin/bash). # --------------------------------------------------------------echo Hello $USER. echo Welcome to programming shell scripts.. # --------------------------------------------------------------18

Executing shell scripts


There are two ways of executing a shell script:

By passing the shell script name as an argument to the shell. For example:
$sh script1.sh If the shell script is assigned execute permission, it can be executed using its name. For example: $./script1.sh

19

Passing parameters to scripts


parameter can be passed to a shell script

parameters are specified after the name of the shell script when invoking the script.
Within the shell script, parameters are referenced using the predefined variables $1 through $9. In case of more than 9 parameters, other parameters can be accessed by shifting.

20

Built-in variables
Following are built-in variables supported
$0, $1$9 $* $@ - positional arguments - all arguments - all arguments

21

Passing parameters to scripts


Consider following shell script:

----------------------script2.sh-------------------------echo Total parameters entered: $# echo First parameter is : $1 echo The parameters are: $* shift echo First parameter is : $1 ------------------------------------------------------------

$script2.sh a b 123
$1 $2 $3

$*=a b

123

$#=3
22

Using the test command


The general syntax of test command is:
test <expression>

The expression can be formed using a combination of shell variables and the operators supported by the test command. These operators provide facility to compare numbers, string and logical values, file types and file access modes.

23

Using the test command


To compare two integers using test following operators are available:
-eq (equal to) -ne (not equal to) -lt (less than) -le (less than or equal to) -gt (greater than) -ge (greater than or equal to)

24

Using the test command


To compare two strings using the test command, following operators are available:
string1 = string2 (equal to, please note it is a single =) string1 != string2 (not equal to) string1 (string is not NULL) -n string1 (string is not NULL and exists) -z string1 (string is NULL and exists)

25

Using the test command


To check a file type/access permissions using the test command, following operators are available:
-s file (file is not empty and exists) -f file (Ordinary file and exists) -d file (file is a directory and exists) -r file (file is readable and exists) -w file (file is write-able and exists) -x file (file is executable and exists)

26

Combining conditions
It is possible to combine conditions by using following operators:
-a (logical AND operator) -o (logical OR operator) ! (logical NOT operator)

27

Condition checking in scripts


if condition then command fi

The condition is typically formed using the test command.

28

Using for loop


The Bash shell provides a for loop. The syntax of this loop is: for variable in list do command command done

29

Using while loop


while condition do command command done

30

System Programming

open
Name: open and create a file or device Prototype: #include<sys/types.h> #include<fcntl.h> int open(const char *path_name, int access_mode, mode_t permissions);

Description: open function establishes a connection between a process and a file. existing file create a new file Return value:- return value is a file descriptor Otherwise return -1 failure

1st argument: Pathname 2nd argument:Access modes O_RDONLY O_WRONLY O_RDWR Access Modifiers O_CREAT O_EXCL O_APPEND O_TRUNC 3rd argument Permision S_IRWXUSR S_IRWGRP S_IROTH S_IRWXU

File descriptor table

File table Fp=0 Mode=r rc=1

Inode table

244 rc=1

new.dat

3 user1

new.dat
fd=open(new.dat,O_RDONLY,0) user1 f1 184 new.dat 244

f1

Read
Name: read-reading data from a file Prototype #include<sys/types.h> #include<unistd..h> Size_t read(in fdesc,void *buf, size_t size); Return value: return number of bytes of data successfully read and stored in the buffer. Otherwise returns -1 failure

Name: write-writing data into a file Prototype #include<sys/types.h> #include<unistd..h> ssize_t write (in fdesc, void *buf, size_t size);

Write

Return Value: return number of bytes of data successfully write and stored in the buffer. Otherwise returns -1 failure

Name: lseek: random access Prototype #include <unistd.h> off_t lseek(int fd, off_t pos, int whence); Pos byte offset to be added Whence SEEK_CUR SEEK_SET SEEK_END File pointer=whence value+pos

lseek:

File descriptor table

File table Fp=0 Mode=r rc=1

Inode table

244 rc=1

new.dat

Fp=0+20

fd=open(new.dat,O_RDONLY,0) lseek(fd,20,SEEK_SET)

Name fork - create a new process Prototype #include <unistd.h> pid_t fork(void);

fork

Description The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below: The child process shall have a unique process ID.

Pid=422 P1

Int main() { printf(parent); pid=fork() printf(after fork): }

Pid=425

Pid=425
Int main() { printf(parent); pid=fork() // pid=0 printf(after fork): }

$./a.out parent after fork after fork $

P2

RETURN VALUE Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

wait
Name wait, waitpid - wait for the parent process Prototype #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);

Pid=422 P1

P2

Pid=425

Int main() { printf(parent); pid=fork() Pid=425 If(pid==0) printf(child pid=%d,getpid()): else $./a.out { parent wait(); Child pid=425 printf(parent pid=%d,getpid()); Parent pid=422 } $ } Int main() { printf(parent); pid=fork() If(pid==0) Pid=0 printf(child pid=%d,getpid()): else { wait(); printf(parent pid=%d,getpid()); } }

system
Name system - execute a shell command Prototype #include <stdlib.h> int system(const char *command); Return Value The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise.

stat/lstat
Name To retrieve property of a file prototype #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);

struct stat ( dev_t st_dev; /* inode's device */ ino_t st_ino; /* inode's number */ mode_t st_mode; /* inode protection mode */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of the file's owner */ gid_t st_gid; /* group ID of the file's group */ dev_t st_rdev; /* device type */ )

Using the S_*() macros on the st_mode member lets you find out type of file S_ISBLK(mode) S_ISCHR(mode) S_ISDIR(mode) S_ISFIFO(mode) S_ISLNK(mode) S_ISREG(mode)

You might also like