Shell Commands Script System Calls
Shell Commands Script System Calls
&
System Calls
cp copy file(s)
mv move file from one place to another
cd change directory
pwd present working directory
Variable
No data types
Name of variable
Assigning value to variable
Display value of variable
read command
to read value from standard input device and assign it
to variable
script2.sh
if statement
Syntax :
if [ condition ]
then
commands
fi
Script3.sh
if..else statement
Syntax :
if [ condition ]
then
commands
else
commands
fi
script4.sh
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
while loop
Syntax :
while [ control condition ]
do
command(s)
done
script8.sh
until loop
Syntax :
until [ control condition ]
do
commands
done
script9.sh
Operator Description
= Equal to
!= Not Equal to
-z Zero length string
-n Non zero length string
case statement
Syntax :
case $option in
pattern-1)
command(s);;
pattern-2)
command(s);;
:
esac
script10.sh
Linux Workshop 20 March 2009
SYSTEM CALLS
Open ()
Syntax :
#include <sys/types.h>
#include<sys/stat.h>
#include <fcntl.h>
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
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>
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
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
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..)
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
Program5.c
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
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
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
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