Sie sind auf Seite 1von 46

Shell Commands, Script

&
System Calls

Ms. Dhruti Sharma


Lecturer, IT Dept.
SCET, Surat

Linux Workshop 20 March 2009


Structure of UNIX

The UNIX OS has three parts


The Kernel
Core unit that directly interacts with hardware
CPU Scheduling, Resource Management, Process
Management
The Shell
Outer unit that interacts with the user
Acts like Command Interpreter.
Many different types of shells: Bourne Shell, C shell, Korn
Shell.
We are working with Bourne Shell
File system

Linux Workshop 20 March 2009


Shell Commands

Files & Directory Commands


Miscellaneous Commands
Utility Commands

Linux Workshop 20 March 2009


File & Directory

ls Listing of files & directory


cat create file & display content of file(s)
rm remove file
mkdir create directory
rmdir remove directory

Linux Workshop 20 March 2009


File & Directory (Cont..)

cp copy file(s)
mv move file from one place to another
cd change directory
pwd present working directory

Linux Workshop 20 March 2009


Miscellaneous

date display system date and time


cal display calendar
who display information of currently
logged in users
echo echo string or value of variable

Linux Workshop 20 March 2009


Special utilities

grep search for a given pattern in a file


sort sort the content of file
cut extract field(s)/ character(s) from file
comm file comparison
wc word count

Linux Workshop 20 March 2009


Special utilities (Cont..)

chmod change file permissions


passwd change password for current
login
expr expression calculation
man to display help

Linux Workshop 20 March 2009


Shell Programming

Shell is a powerful command Interpreter.


Variable creation
Use of programming constructs
if
if..else
for loop
while loop
until loop
case

Linux Workshop 20 March 2009


Shell Programming (Cont..)

Variable
No data types
Name of variable
Assigning value to variable
Display value of variable

Linux Workshop 20 March 2009


Shell Script

A Shell script is a command or sequence of


commands stored in a file which is executed
by typing.
Extension to script file .sh
Create using cat command or using vi
editor
Run using sh command
script1.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

In UNIX environment, all command line


arguments are known as positional
parameters.
The file name is always referred to as $0.
The arguments to the command are $1, $2
etc..
The total no. of arguments is $#.
All the arguments will be displayed by $*.

Linux Workshop 20 March 2009


Shell Script (Cont..)

read command
to read value from standard input device and assign it
to variable
script2.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

if statement
Syntax :
if [ condition ]
then
commands
fi
Script3.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

if..else statement
Syntax :
if [ condition ]
then
commands
else
commands
fi
script4.sh
Linux Workshop 20 March 2009
Shell Script (Cont..)

elif statement else


Syntax : default
if [ condition ] fi
then
commands Script5.sh
elif [ condition ]
then
commands
elif [ condition ]
then
commands
:
Linux Workshop 20 March 2009
Shell Script (Cont..)

for loop
Syntax :
for [variable name ] in [ list of values ]
do
command(s)
done
script6.sh
script7.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

while loop
Syntax :
while [ control condition ]
do
command(s)
done
script8.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

until loop
Syntax :
until [ control condition ]
do
commands
done
script9.sh

Linux Workshop 20 March 2009


Shell Script (Cont..)

Number Comparison Operator


Operator Description
-eq Equal to
-ne Not Equal to
-lt Less than
-le Less than & equal to
-gt Greater than
-ge Greater than & equal to
Linux Workshop 20 March 2009
Shell Script (Cont..)

String Comparison Operator

Operator Description
= Equal to
!= Not Equal to
-z Zero length string
-n Non zero length string

Linux Workshop 20 March 2009


Shell Script (Cont..)

case statement
Syntax :
case $option in
pattern-1)
command(s);;
pattern-2)
command(s);;
:
esac
script10.sh
Linux Workshop 20 March 2009
SYSTEM CALLS

System Calls provides an entry point to


Linux kernel.

Linux Workshop 20 March 2009


System Calls

Open ()
Syntax :
#include <sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>

int open(const char *name, int oflag, [mode]);


Returns : file descriptor if OK, -1 on error

Linux Workshop 20 March 2009


System Calls (cont..)

The pathname is the name of the file open or


create.
The value of oflag is any one from following :
O_RDONLY - Open for reading only.
O_WRONLY - Open for writing only.
O_RDWR - Open for reading and writing.

These constants are available in fcntl.h file.


O_APPEND - Append to the end of file on each write.
O_CREATE - Create the file if it doesnt exist.
O_TRUNC - If the file exists, and if the file is successfully
opened for either write-only or read-write,
truncate its length to 0.
Linux Workshop 20 March 2009
System Calls (cont..)

create ()
Syntax :
#include <sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
int create(const char *pathname);
Returns : file descriptor if OK, -1 on error

Linux Workshop 20 March 2009


The pathname is the name of the file open or create.

This function is equivalent to

open (pathname, O_WRONLY | O_CREAT |


O_TRUNC, mode);

One deficiency with this function is that the file is


opened only for writing.
Program1.c
Linux Workshop 20 March 2009
System Calls (cont..)

read ()
Data is read from an open file with the read function.
Syntax:
#include <unistd.h>
ssize_t read(int filedes, void *buff, unsigned nbytes);
Returns: number of bytes read, 0 if end of file, -1 on
error

Program2.c
Linux Workshop 20 March 2009
System Calls (cont..)

write ()
Data is written to an open file with the write function.

Syntax:

#include <unistd.h>

ssize_t write(int filedes, const void *buff, unsigned nbytes);

Returns : number of bytes written if OK, -1 on error

Program2.c
Linux Workshop 20 March 2009
System Calls (cont..)

close ()
An open file is closed by
Syntax :
#include <unistd.h>
int close(int filedes);
Returns : 0 if OK, -1 on error

Program2.c
Linux Workshop 20 March 2009
System Calls (cont..)

lseek ()
Syntax :
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fields, off_t offset, int whence);
Returns : new file descriptor if OK, -1 on error

Linux Workshop 20 March 2009


offset depends on the value of whence
If whence is 0, the files offset is set to offset bytes
from the beginning of the file.
If whence is 1, the files offset is set to its current value
plus the offset. The offset is positive or negative.
If whence is 2, the files offset is set to the size of the
file plus the offset. The offset is positive or negative.
Program3.c

Linux Workshop 20 March 2009


System Calls (cont..)

stat ()
Stat function returns the structure of information about the
named file.
Syntax:
#include <sys/types.h>
#include<sys/stat.h>
int stat(const char *pathname, struct stat *buf);
Returns : 0 if OK, -1 on error

Linux Workshop 20 March 2009


System Calls (cont..)

struct stat {
mode_t st_mode; // file type
ino_t st_ino; // i-node number
dev_t st_dev; // device number
dev_t st_dev; //device number for special file
uid_t st_uid; //use IDof owner
gid_t st_gid; //group ID of owner
off_t st_size; //size of file
time_t st_atime; //time of last access
time_t st_mtime; //time of modification
};
Linux Workshop 20 March 2009
System Calls (cont..)

Regular file, Directory file, Character special file, Block


special file, FIFO, Socket etc.
Types of File
S_ISREG() - Regular file S_ISDIR() - Directory file
S_ISCHR() - Character special file
S_ISBLK() - Block special file
S_ISFIFO() - FIFO or Pipe file
S_ISLINK() - Symbolic link S_ISSOCK() Socket
Program
Program4.c
Linux Workshop 20 March 2009
System Calls (cont..)

chmod ()
This function allows us to change the file access permission
for an existing file.
Syntax:
#include <sys/types.h>
#include<sys/stat.h>
int chmod(const char *pathname, mode_t mode);
Returns : 0 if OK, -1 on error

Linux Workshop 20 March 2009


System Calls (cont..)
Mode Description
S_IRUSR Read by User (Owner)
S_IWUSR Write by User (Owner)
S_IXUSR Execute by User (Owner)
S_IRGRP Read by Group
S_IWGRP Write by Group
S_IXGRP Execute by Group
S_IROTH Read by Other
S_IWOTH Write by Other
S_IXOTH Execute by Other

Program5.c

Linux Workshop 20 March 2009


System Calls (cont..)

remove ()
To remove file from the system we can use this function.
Syntax :
#include <stdio.h>
int remove (const char * pathname);
Returns: 0 if OK, -1 on error
The argument is the file name or filename with full path.

Program6.c
Linux Workshop 20 March 2009
System Calls (cont..)

rename ()
A file or Directory is renamed with rename function.
Syntax:
#include <stdio.h>
int rename(const char *oldname, const char *newname);
Returns : 0 if OK, -1 on error

Program7.c

Linux Workshop 20 March 2009


System Calls (cont..)

mkdir ()
Directory is created using mkdir function.
Syntax:
#include <sys/types.h>
#include<sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
Returns : 0 if OK, -1 on error

Program8.c
Linux Workshop 20 March 2009
System Calls (cont..)

rmdir ()
Syntax:
#include <unistd.h>
int rmdir(const char *pathname);
Returns : 0 if OK, -1 on error
The argument is the file name or filename with full path.

Program9.c

Linux Workshop 20 March 2009


System Calls (cont..)

Reading Directories
Syntax :
# include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname);
Returns : pointer if OK, NULL on error
struct dirent *readdir(DIR *dp);
Returns : pointer if OK, NULL at the end of
directory or error
Linux Workshop 20 March 2009
int closedir(DIR *dp);
Returns : 0 if OK, -1 on error

Program10.c

Linux Workshop 20 March 2009


System Calls (cont..)

chdir ()
We can change the current working directory of the calling
process by calling the chdir function.
Syntax:
# include <unistd.h>
int chdir (const char *pathname);
Returns : 0 if OK, -1 on error

Program11.c
Linux Workshop 20 March 2009
System Calls (cont..)

getcwd function
This function gives you the information about current
working directory.
Syntax :
#include <unistd.h>
char *getcwd(char *buf, size_t size);
Returns : buf if OK, NULL on error
Program12.c
Linux Workshop 20 March 2009
THANK YOU

Linux Workshop 20 March 2009

Das könnte Ihnen auch gefallen