A
PRACTICAL FILE
ON
“Operating system and shell programming”
2 0 M C A 2 2 C3
In Partial Fulfillment of the Requirements in Masters of Computer Applications Sem-02
Session: 2024-26
Submitted to: Submitted By:
Mrs. Shikha Mathur Ritwika Panda
Assistant Professor 145484
Dept. Of Computer Science MCA Sem 02
DPG STM
Table of Contents
Exp.
No. Name of the Experiments
UNIT-I
1.1 Installation of Linux operating system.
1.
1.2 Study of basic Linux commands.
2.1 Programs using the following system calls of Linux operating system: fork, getpid,
2. getppid, exit, wait, and close.
2.2 Use of Linux file related commands like ls, grep, cat, etc.
3.1 Programs using the I/O system calls of Linux operating system (open, read, write
3. etc).
3.2 Study of basics of shell programming.
UNIT-II
4.1 Write a program to show the use of echo.
4.
4.2 Write a program to read the keywords in shell programming.
5. Write a program to demonstrate the difference between while and until statement.
Write a shell script to generate a multiplication table of the given number.
6.
Write a shell script that counts the number of lines and words present in a given file.
7.
UNIT-III
8. Write a C program that illustrates uses of the opendir, readdir, and closedir APIs.
Experiment-1
Exp1.1
AIM: Installation of Linux operating system.
Step 1: Firstly empty any one drive from our computer and right click on the computer select manage and then select
disk fragmentation and then delete that drive which is empty.
Step 2: Then put the Red Hat DVD into the DVD drive and reboot the system, check the BIOS setting the computer
from DVD is selected if not selected. Then DVD runs on it.
Step 3: Then seen a) In text mode we have to need to type Linux ascue.
b) In Graphical mode we have to press enter.
Step 4: Welcome to Red Hat Enterprise Linux: The welcome screen does not prompt you for any input. From this
screen you can access the release notes for Red Hat Enterprise 5.0.0 by clicking on the release button.
Click on the next button to continue
Step 5: Language Selection: The language you select here will become the default language for the operating
system once it is installed. Selecting the appropriate language also helps target your time zone configuration later in
the installation. The installation program tries to define the appropriate time zone based on what you specify on this
screen.
Step 6: Keyboard Configuration: Using your mouse, select the correct layout type for example
U.S. English for the keyboard. You would prefer to use for the installation and as the system default.
Step 7: Enter the Installation Number: If you have a number then put on it otherwise skip this
option
Step 8: Create Default Layout options are
a) Remove all partitions on selected drives and create default layout: Select this option to
remove all partitions on your hard drives
b) Remove Linux partitions on selected drives and create default layout: Select this option to
remove Linux partitions on your hard drives
c) Use free space on selected drives and create default layout: Select this option to retain
your current data and partitions, assuming you have enough free space.
d) Create custom layout: If you delete free drive then select create custom layout.
Select the Create Custom Layout. Then click next button. If you have two hard disk then select which one.
Step 9: Choose custom layout, you must tell the installation program where to install Red Hat 5.0. This is done by
defining mount point for one or more disk partitions in which Red Hat is installed. You may also need to create or
delete partition at this time.
Mount Point: Enter the partition's mount point. For example, if this partition should be the root partition, enter / for
the root
/boot for the /boot partition, and
/swap files double of ram.
Pull-down menu to choose the correct mount point for your partition. For a swap partition the Mount point should not
be set - setting the files system type to swap is sufficient.
File System Type: Using the pull-down menu, select the appropriate file system type for this partition. For
more information on file system types, “File System Types”.
Allowable Drives: This field contains a list of the hard disks installed on your system. If a Hard disk's box is
highlighted, then a desired partition can be created on that hard disk. If the box is not checked, then the partition will
never be created on that hard disk. By using different checkbox settings, you can have Disk Druid place partitions
where you need them, or let Disk Druid decide where partitions should go.
Size (MB): Enter the size (in megabytes) of the partition. Note, this field starts with 100 MB; unless changed; only a
100 MB partition will be created.
The file systems are:
ext2 — An ext2 file system supports standard Unix file types (regular files, directories, symbolic links, etc). It
provides the ability to assign long file names, up to 255 characters.
ext3 — The ext3 file system is based on the ext2 file system and has one main advantage — journaling. Using a
journaling file system reduces time spent recovering a file system after a crash as there is no need to fsck1 the file
system. The ext3 file system is selected by default and is highly recommended.
Physical volume (LVM) — Creating one or more physical volume (LVM) partitions allows you to create an LVM
logical volume. LVM can improve performance when using physical disks. For more information regarding LVM,
refer to the Red Hat Enterprise Linux Deployment Guide.
Step 10: To boot the system without boot media, you usually need to install a boot loader. A boot loader
Is the first software program that runs when a computer starts? It is responsible for loading and transferring control
to the operating system kernel software. The kernel, in turn, initializes the rest of the operating system.
GRUB (Grand Unified Boot loader), which is installed by default, is a very powerful boot loader. GRUB can
load a variety of free operating systems, as well as proprietary operating system. With chain-loading (the mechanism
for loading unsupported operating systems, such as DOS or Windows, by loading another boot loader).
Click the next button
Step 11:. Time Zone Configuration: Set your time zone by selecting the city closest to your computer's physical
location. Click on the map to zoom in to a particular geographical region of the world. From here there are two ways
for you to select your time zone:
• Using your mouse, click on the interactive map to select a specific city (represented by a
yellow dot). A red X appears indicating your selection.
• You can also scroll through the list at the bottom of the screen to select your time zone.
Using your mouse, click on a location to highlight your selection.
Step 12: Set Root Password: Setting up a root account and password is one of the most important steps during your
installation. Your root account is similar to the administrator account used on Windows NT
Step 13: You want to select web server and software development and choose custom now and choose custom later,
any one of them.
Step 14: And installing the Linux.
Step 15: And rebooting the system.
Step 16: Firewall disabled or enabled.
Step 17: SELN disabled or enabled.
Step 18: Then continue means Linux is installed.
Experiment-1
Exp1.2
AIM: Study of basic LINUX commands.
List of some of the LINUX COMMANDS are givn below:
1. Id Syntax:
id
Id command is used to find a user's UID (user ID) or GID (group ID) and other
information in Linux. The main purpose of id command is to displays the system
identifications of a specified user.
2. Who Syntax: who
The who command prints information about all users who are currently logged in.
3. Who am i Syntax: who am i
The who am i command prints the user name (i.e., login name) of the owner of the current login session on
the monitor screen.
4. Date Syntax:
date
Date command is helpful to display date in several formats. It also allows you to set systems date
and time. When you execute date command without any option, it will display the current date and
time.
5. Cal
Display a conveniently-formatted calendar from the command line. i.
Syntax: cal
If no options are given, cal displays the current month at the command line.
ii. Syntax: cal [year]
Display a calendar for that entire year.
6. Clear
Syntax: clear
Clear command is used to clear the screen. The clear command does not affect files or
jobs, it simply clears the clutter from your terminal screen. `CLEAR` doesn't erase your terminal scroll
buffer, so if you need to see something that was on your screen a while ago, you can still use your mouse
wheel to scroll back.
7. Pwd
Syntax: pwd
‘pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the
current working directory or simply the directory user is, at present. It prints the current directory name with
the complete path starting from root (/).
8. Mkdir
Syntax: mkdir directory_name
It is a short for "make directory", mkdir is used to create directories on a file system . If the
specified DIRECTORY does not already exist, mkdir creates it.More than one DIRECTORY may
be specified when calling mkdir.
9. Cd
The cd command is used to change the current directory (i.e., the directory in which the user is currently
working) in Linux and other Unix-like operating systems.
i. Syntax: cd [directory]
When a directory name is provided, cd changes the current directory to it. When used without
specifying any directory name, cd returns the user to the previous current directory.
ii. Syntax: cd ..
This command helps the user to change to the parent of the current directory. On LINUX like
operating system parent directory of the current directory is represented by two consecutive dots.
10. Rmdir
Syntax: rmdir directory_name
The rmdir command is used to remove empty directories in Linux. If a directory contains files or sub
directories, then the rmdir command fails.
11. Ls
Syntax: ls
This command is usually used to view the contents of current directory. So, when you run this command,
the files and sub-directories included under the current directory will be listed before you.
Experiment-2
Exp2.1
AIM: Programs using the following system calls of Linux operating system: fork, getpid, getppid, exit, wait, and
close.
GET PID
OUTPUT
CODE
#include<stdio.h>
main(intarc,char*ar[]
) { intpid; char s[100];
pid=fork(); if(pid<0)
printf("error"); else
if(pid>0)
{
wait(NULL); printf("
\
n Parent Process:
\
n");
printf("
\n
\
tParent Process id:%d
\t
\
n",getpid()); execlp("cat","cat",ar[1],(char*)0);
error("can’t execute cat %s,",ar[1]);
} else
{
printf( "
\
nChild process:");
printf("
\n
\
tChildprocess parent id:
\
t %d",getppid());
sprintf(s,"
\n
\
tChild proc ess id
:
\ t%d",getpid());
write(1,s,strlen(s));
printf(" "); printf("
"); printf(" ");
execvp(ar[2],&ar[2])
;
error("can’t execute %s",ar[2]);
}
}
OUTPUT
[root@localhost ~]# ./a.outtst date Child process:
Child process id : 3137 Sat A
pr 10 02:45:32
IST 2010 Parent Process:
Parent Process id:3136
sd
dsaASD[root@localhost ~]# cat tst sd
dsaASD
FORK
OUTPUT
EXPERIMENT-2
Exp2.2
AIM: Use of Linux file related commands like ls, grep, cat, etc.
1. Ls: Lists the contents of a directory. List information about the Files (the current directory
by default).
Syntax: ls[OPTIONS]…[FILE]..
ls –a: List all files and folders including hidden file starting with ‘.‘.
ls –F: Using -F option with ls command, will add the ‘/’ Character at the end of
each directory. ls –r: Using ls -r option with ls command will display files and
directories in reverse order.
2. Cat: The cat (short for “concatenate“) command is one of the most frequently used
command in Linux/Unix like operating systems. cat command allows us to create single or
multiple files, view contain of file, concatenate files and redirect output in terminal or files.
Syntax: cat [options] [filenames] [-] [filenames] cat > filename: Typing the command cat
followed by the output redirection operator and a file name on the same line, pressing
ENTER to move to the next line, then typing some text and finally pressing ENTER again
causes the text to be written to that file. This command create and open file in writing mode.
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit.
cat filename: It is used to display the content of a file on the monitor screen.
cat file1 file2: This command is used to concatenate the contents of files. The contents of each file will be
displayed on the monitor screen (which, again, is standard output, and thus the destination of the output in
the absence of redirection) starting on a new line and in the order that the file names appear in the command.
tac file1 file2: This command is used to display the contents of files after concatenating in the order that
firstly the contents of last entered file will be shown and the contents of first entered file will be shown at
end.
3. Sort: sort is a simple and very useful command which will rearrange the lines in a text file
so that they are sorted, numerically and alphabetically. By default, the rules for sorting are:
● lines starting with a number will appear before lines starting with a letter;
● lines starting with a letter that appears earlier in the alphabet will appear before lines
starting with a letter that appears later in the alphabet;
● lines starting with a lowercase letter will appear before lines starting with the same letter
in uppercase.
Syntax: sort [OPTION]... [FILE]... sort filename: This command sort the contents of the given file
and display it on the monitor screen.
sort file1 file2: This command display the contents on the screen after sorting the contents of both the
files.
4. wc: The wc (word count) command in Unix/Linux operating systems is used to find out
number of newline count, word count, byte and characters count in a files specified by
the file arguments.
Syntax: wc [options] filenames
wc –l filename: Prints the number of lines in a file.
wc –w filename: Prints the number of words in a file.
wc –c filename: Displays the count of bytes in a file.
5. Head: Print the first 10 lines of each FILE to standard output. With more than one FILE,
precede each with a header giving the file name.
Syntax: head [OPTION]... [FILE]...
head –n filename: Print the first N lines instead of the first 10.
6. Tail: Print the last 10 lines of each FILE to standard output. With more than one FILE,
precede each with a header giving the file name.
Syntax: tail [OPTION]... [FILE]...
tail –n filename: Output the last N lines, instead of the last 10.
7. Grep: The grep command is used to search text or searches the given file for lines
containing a match to the given strings or words. By default, grep displays the matching
lines. Use grep to search for lines of text that match one or many regular expressions, and
outputs only the matching lines.
Syntax: grep [options] PATTERN [FILE...]
grep pattern filename: This command searches the named input FILEs (or standard input if no files are
named, or the file name -is given) for lines containing a match to the given PATTERN. By default, grep
prints the matching lines.
grep –v filename: -v option is used to print inverts the match; that is, it matches only those lines that do
not contain the given word.
EXPERIMENT-3
Exp 3.1
AIM: Programs using the I/O system calls of Linux operating system (open, read, write etc). #include<stdio.h>
#include<sys/stat.h>
#include<time.h> main(intag,char*arg[])
{ charbuf[100]; struct stat s;
int fd1,fd2,n;
fd1=open(arg[1],0);
fd2=creat(arg[2],0777)
; stat(arg[2],&s);
if(fd2==
-
1)
printf("ERROR IN CREATION"); while((n=read(fd1,buf,sizeof(buf)))>0)
{
if(write(fd2,buf,n)!=n)
{
close(fd1); close(fd2);
} }
printf( "
\t
\
n UID FOR FILE. ............ >%d
\
n FILE
ACCESS TIME........... >%s
\
n FILE
MODIFIED TIME. ..............>%s
\
n FILE I
-
NODE NUMBER. ........... >%d
\
n
PERMISSION FOR
FILE. ...... >%o
\n
\
n",s.st_uid,ctime (&s.st_atime),ctime(&s.st_mtime),s.st_mode);
close(fd1);
close(fd2);
}
OUTPUT
[root@localhost ~]# cc iosys.c [roo
t@localhost ~]#
./a.out UID FOR
FILE. ......... >0
FILE ACCESS TIME. ............ >Thu Apr 8 01:23:54 2011
FILE MODIFIED TIME ................. >Thu Apr 8 01:23:54 2011
FILE I
-
NODE NUMBER. ........... >33261
PERMISSION FOR FILE. ............ >1001101014
Exp 3.2
AIM: Study of basics of shell programming.
The term “shell scripting” gets mentioned often in Linux forums, but many users aren’t familiar with it. Learning
this easy and powerful programming method can help you save time, learn the command-line better, and banish
tedious file management tasks.
Being a Linux user means you play around with the command-line. Like it or not, there are just some things that are
done much more easily via this interface than by pointing and clicking. The more you use and learn the command-
line, the more you see its potential. Well, the command-line itself is a program: the shell. Most Linux distros today
use Bash, and this is what you’re really entering commands into.
Now, some of you who used Windows before using Linux may remember batch files. These were little text files that
you could fill with commands to execute and Windows would run them in turn. It was a clever and neat way to get
some things done, like run games in your high school computer lab when you couldn’t open system folders or create
shortcuts. Batch files in Windows, while useful, are a cheap imitation of shell scripts.
Shell scripts allow us to program commands in chains and have the system execute them as a scripted event, just like
batch files. They also allow for far more useful functions, such as command substitution. You can invoke a command,
like date, and use it’s output as part of a file-naming scheme. You can automate backups and each copied file can
have the current date appended to the end of its name. Scripts aren’t just invocations of commands, either. They’re
programs in their own right. Scripting allows you to use programming functions – such as ‘for’ loops, if/then/else
statements, and so forth – directly within your operating system’s interface. And, you don’t have to learn another
language because you’re using what you already know: the command-line. That’s really the power of scripting, I
think. You get to program with commands you already know, while learning staples of most major programming
languages. Need to do something repetitive and tedious? Script it! Need a shortcut for a really convoluted command?
Script it! Want to build a really easy to use command-line interface for something? Script it!
EXPERIMENT-4
Exp 4.1
AIM: Write a program to show the use of echo.
Program to print “first page” on the screen.
Source code:
Output:
Exp 4.2
AIM: Write a program to read the keywords in shell programming.
Source code:
Output:
Source code
EXPERIMENT-5
AIM: Write a program to demonstrate the difference between while and until statement.
1. Program to display first ten natural numbers using while loop.
Source code:
Output:
2. Program to display first ten natural numbers using until loop.
Source code:
Output:
EXPERIMENT-6
EXPERIMENT-7
Explanation:
The wc -l command counts the lines in the file, which is 3.
The wc -w command counts the words in the file, which is 9.
If the file does not exist, the script would output:
EXPERIMENT-8