0% found this document useful (0 votes)
77 views33 pages

OS Lab Manual

Ken Thompson and Dennis Ritchie designed UNIX at AT&T Bell Laboratories in 1969 as a small multitasking system supporting two users. Originally written in assembly, UNIX was rewritten in C in 1973, making it portable to different hardware platforms. Today, UNIX powers many servers and workstations and has influenced operating systems such as Linux and macOS.

Uploaded by

Shekharesh Barik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views33 pages

OS Lab Manual

Ken Thompson and Dennis Ritchie designed UNIX at AT&T Bell Laboratories in 1969 as a small multitasking system supporting two users. Originally written in assembly, UNIX was rewritten in C in 1973, making it portable to different hardware platforms. Today, UNIX powers many servers and workstations and has influenced operating systems such as Linux and macOS.

Uploaded by

Shekharesh Barik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Experiment 1: History of UNIX.

Operating system: Operating system is an interface between hardware and user. It


helps to run the hardware smoothly and efficiently.
Unix had a modest beginning at AT&T Bell Laboratories in 1969. That was
when AT&T withdraws its team from the MULTICS project and some of them on
the development of a computing environment to pursue programming Research.
Ken Thomson and Dennis Ritchie then designed and built a small multitasking
system supporting two users. This new system had an elegant file system, a
command interpreter and a set of utilities. A member of the team, Brian
Kennighan presumably having MULTICS in mind, suggested the name UNICS
(UNiplexed Information and Computing System) for this operating system. In
1970, UNICS finally became the name by which it is known today Unix.
Os are designed with a particular machine in mind. Similarly computers
were also built for a specific operating system. They were invariably written in
assembler which understand and accessed the hardware well and had good speed.
Thompson and Ritchie however wanted a general operating system running on
more than one type of hardware. That implied use of a high-level language with
consequent sacrifice of speed.
Originally written in Assembler, UNIX was rewritten in 1973 in C, which
was principally authored by Ritchie. Writing the system in C made it possible to
maintain it and move it to different hardware platforms. Today Unix is quite a fast
performer.
Apart from AT&T, most of the solid features available on UNIX system
today can be attributed to the distinguished institution, Berkley. Berkley didnt like
most of AT&T UNIX and created a Unix of its own. They called it its BSDUNIX
(Berkley Software Distribution). These versions became quite popular worldwide, especially in universities and circles. Their latest versions became is
4.3BSD.

Bill joy had written standard editor of Unix (vi) and very popular shell. (c
shell ).
Another version of UNIX by AT&T was SVR4 (System V Release 4)
features a special directory where we find all the Berkley utilities. Sun Microsystem used the BSD system as a foundation for developing their own brand of
UNIX (Sun OS). Its major contribution was Network File System (NFS). Today
this version of Unix is known as SOLARIS and is SVR4 based. Microsoft was the
1st to run Unix on a pc with 640KB of memory. They called their product XENIX
that was based on earlier version of AT&T but with some BSD borrowed utilities.
XENIX was later sold off to SCO (Santa Cruz Operation). SCO Unix now offers
two major flavors: SCO Open Server Release 5 and SCO Unix-ware 7; the later is
SVR4-Compliant.

Linux:- Linus Torvalds is the father of Linux. Linux is distributed under the GNU
general public license which makes it mandatory for developers and sellers to
make the source code public.

Experiment 2: Features of UNIX.


Unix has number of features. Many of its features have been borrowed from other
operating system especially MULTICS.

Multi-User:
In Unix, the resources are actually shared between all users. It can be a memory,
CPU or Hard-disk.
The computer breaks up a unit of time into several segments and each user
is allotted a segment, so at any point of time, the machine will be doing the job of
a single user. The moment the allotted time expires; the next job that is stored in
the queue is taken up and executed.
The process goes on till the clock has turned a full circle and the first user job is
taken up once again. Thus the kernel does several jobs in one second.

Multi-tasking:
In UNIX a single user can also run multiple tasks simultaneously. You can switch
between processes.
You can run background and foreground processes simultaneously, suspend a
process or even kill them. This is done by running one job normally and the other
in the background. Processes which are time consuming and non-interactive in
nature can be done in the background while we can continue using the system to
do other work. An ampersand (&) placed at the end of command line does this
task.
Ex:- sort f1 &
936
You will be returned the $ prompt immediately .You will be also given a no which
represents the job. In this situation, you can another command.

Security:
In UNIX, all application programs runs in shell. The entire information (like harddisk, mouse, printer etc) is stored in files. When UNIX is booted all the necessary
information are written into respective files.
These files are created at booting time only (in Unix there is no executable files)
So, any application program (even a virus) cannot directly affect the hardware.
Hence, Unix systems are more secured and powerful than windows.

Pattern Matching:
It is the pattern matching feature of Unix that makes it so attractive to the user.
*, ? (wild card characters) used by the system to indicate the expression can match
a number of filenames.
ls l c* this command displays all the files starting with c.
some of the most advanced and useful tools also use a special expression called
regular expression.

Programming Facility:
The Unix shell programming language has all the necessary ingredients like
control structure, loop, if-else statement and variable etc. that establish it as a
programming language in its own right.
The shell simplifies a number of things that are impossible in other languages. A
shell script contains a sequence of commands to be executed with a single
filename.
There are different types of shell available. They are BOURNE shell, C shell,
KORNE shell.

Input Output Redirection:


Programs can be instructed to take input from a file other than the standard input
(key-board) and also the output can be sent to a file instead of a standard output
(i.e. monitor). This is called input & output redirection.

Pipes:
Simple programs can be developed for complex operation with minimum effort
such as pipes. This reduces the necessity of writing new programs for the complex
operation.
Ex:- who | wc -l

System Calls and Libraries:


Unix is written in the programming language c. Though there are 100 commands
handling specialized functions, they all perform by using a handful of functions
called system calls. These calls are built into the kernel and all library functions
and utilities are written using them. All the brand of Unix use the same system
calls.
Interaction through the system call represents an efficient means of
communication with the system. A user who wishes to write to a file, uses the
write( ) system call without going to the details of programming that achieves this
write operation. When a brand of Unix is ported to another machine, its only
implementation part of the system call that needs to be reprogrammed not the
system calls themselves.

Experiment 3: Structure of Unix.


Unix is very popular due to its special features and its structure. Its structure
consists of kernel, shell and tools and applications.

HW
Kernel
Shell
Fig: Structure of Unix
Kernel:- Kernel forms the core of the Unix operating system. This interacts with
the hardware. It is loaded into the memory when a system is booted .Its functions
are
Managing the system resources
Allocating time for different users and processes
Deciding process priorities and performing them.
Since it isolates itself from user programs, it can be moved across different
systems. Kernel does not interact with the user directly .Instead, it starts up a
separate file called Unix Shell.

Shell:-Shell forms the interface between the kernel and the user.

Experiment 4: File System in Unix.


Unix treats all information as files. Files are stored in a hierarchical order. This
enables easier search of the files. Directories and devices are also treated as files.
root

bin

date,cat,who,ls

dev

etc

usr

unix

lib

tmp

bin

Fig. Unix File System


The above directory structure may vary with different flavors of Unix.
When the system starts; an ordinary file called Unix is read from the disk into
memory. Actually, the process occurs in two steps: first the file /boot is read and
/unix is read.
The commonly used Unix commands like date, cat, who, etc. are stored in
the directories /bin and /usr/bin, and made available to every user. You dont need
to copy these files to your directory to use them.
The /etc directory contains other additional commands that are related to
system maintenance and administration. It also contains several system files which
store the relevant information about the users of the system, the terminals and
devices connected to the system.
The /lib directory contains all the library functions provided by Unix for the
programmers. Programs can be written in Unix using the library functions by
making system calls.
The /dev directory stores files that are related to the devices. Unix has a file
associated with each of the I/O devices.
The /usr directory (called /home directory in other version) is created for
each user to have a private work-area where he can store his files. This directory
can be named as anything. Here it is named as usr. This is called users HOME
directory. Thus every user has a HOME directory. bin directory is present within
the usr directory. bin directory contains additional Unix commands.
The /tmp directory is the directory into which temporary files are kept
(especially during vi editor). The files stored in this directory are deleted as soon
as the system is shutdown and restarted.

Study of different commands in Unix


Command Name: date
Syntax: date
Description: displays the current date and time
Example: date
wed Mar 16 13 : 18 : 04
--------------------------------------------------------------------------------------------------------------------

Command Name: clear


Syntax: clear
Description: It clears the screen
Example: clear
--------------------------------------------------------------------------------------------------------------------

Command Name: cal


Syntax: cal
Description: it displays the calendar of year
Example: cal
--------------------------------------------------------------------------------------------------------------------

Command Name: who


Syntax: who
Description: it displays all users who have logged into the system
Example: who
CSE 027 DTS/1 2011-03-16 12:31(192.168.2.47)
CSE 010 DTS/7 2011-03-16 13:24(192.162.2.10)
--------------------------------------------------------------------------------------------------------------------

Command Name: who am i


Syntax: who am i
Description: it displays the information about you
Example: who am i

CSE 0/0 DTS/7 2011-03-6 13:24(192.162.2.10)


--------------------------------------------------------------------------------------------------------------------

Command Name: echo


Syntax: echo
Description: it prints the text on the screen
Example: echo driems
driems
--------------------------------------------------------------------------------------------------------------------

Command Name: ls
Syntax: ls
Description: it displays the files in the current directory
Example: 1. ls
jkn rajni kumar
2. ls a* ( display the files starting with letter a )
ab
acc
avishek
awc
3. ls l (display all files in detailed format)
_rw_ _ r _ _ r 1 cse 020 47 Feb 23 :

12 : 35

--------------------------------------------------------------------------------------------------------------------

Command Name: cat


Syntax: cat filename
Description: it is used to create a file
Example: 1. cat>a //to create file named as a
Name: sumit nayak
RegdNo: 0907229058
Semeseter : 6th
2. cat a // to display the content of file the file a
Name: sumit das
RegdNo: 0907229058
Semeseter: 6th
--------------------------------------------------------------------------------------------------------------------

Command Name: wc
Syntax: wc option filename
Description: It displays the number of lines/words/characters in a file
Example: 1. wc lwc a
3 7 52

2. wc l a

//count the number of lines in file a

3
3. wc w a
7
4. wc c a
52

//count the number of words in file a


//count the number of characters in file a

--------------------------------------------------------------------------------------------------------------------

Command Name: nl
Syntax: nl filename
Description: It is used to provide line no to a file.
Example: nl file1
--------------------------------------------------------------------------------------------------------------------

Command Name: exit


Syntax: exit
Description: it is used to logout from the session
Example: exit
Input redirection:Syntax: command > filename
Description: output is redirected to a file instead of the standard-input (keyboard)
Example: ls l > b
cat b

(the output of ls l is stored in file b)


(display the contents of file b)

Create two files file1 and file2 as shown below


cat > file1

india
pak
england
cat > file2
india
china
japan
Command Name: cmp
Syntax: cmp file1 file2
Description: It is used to compare two files. Two files are compared byte by byte
and the location of the first mismatch byte-no is displayed on the screen.
Example:

cmp file1 file2


file1 file2 differ: byte 7, line 2

--------------------------------------------------------------------------------------------------------------------

Command Name: comm


Syntax: comm option file1 file2
Description: This command compares each line of the first file with its
corresponding line in the second file. In the output the first column contains lines
unique to file1. Second column contains lines unique to file2. Third column
contains lines common to both file1 and file2.
Example: 1. comm file1 file2
india
china
japan
pak
england

2. comm -1 file1 file2

//compare by dropping 1st column

india
china
japan
comm -2 file1 file2

//compare by dropping 2nd column

india
pak
england
--------------------------------------------------------------------------------------------------------------------

Command Name: diff


Syntax: diff file1 file2
Description: This command reports the difference between two files on a line by
line basis.
Example: diff file1 file2
2,3c2,3

//change line2,line3 of file1 with line2,line3 of file2 to make

two file equal


< pak
< england
--> china
> japan
Command Name: split
Syntax: split n filename
Description: This command is used to split one file into several files.
Example: split -2 a // splitting file a into several files in which each file contain
2 lines.
// the splitted files are named as xaa, xab, xac, xad .

Abbreviation table for chmod Command :Category

Operation

Permission

u user
g group

+ assign
remove

r read
w write

o others

x execute

Command Name: chmod


Syntax: chmod category operation permission filename
Description: It is used set permission to a file for all types of user.
Example:
1. chmod u+x file1

// assigning execute permission to user for file1

2. chmod ugo+w file1

// assigning write permission to all users for file1

3. chmod u-x file1

// remove execute permission from user for file1

4. chmod o+wx file1

// assigning write & execute permission to others for file1

--------------------------------------------------------------------------------------------------------------------

Command Name: ps
Syntax: ps
Description: It gives the information on process status for process associate with
your terminal.
Example: ps
PID

TTY

TIME

CMD

24518

pts/1

00:00:00

bash

24559

pts/1

00:00:00

ps

Command Name: kill


Syntax: kill PID

Process Identification Number

Description: It gives the information on process status for process associate with
your terminal.
Example: kill -9 24518 //permanently delete the process for the given PID

Filters
There are certain commands which perform some filtering action on the data
called as filters.
Command Name: more
Syntax: command | more
Description: It is used to display the contents page by page.
Example: cal 2011 | more
--------------------------------------------------------------------------------------------------------------------

Command Name: head


Syntax: head n filename
Description: It display the line specified(n) from the beginning of file.
Example: head -3 b

// display the first 3 lines of file b

--------------------------------------------------------------------------------------------------------------------

Command Name: tail


Syntax: tail n filename
Description: It display the lines specified(n) from the end of file.
Example: tail -3 b

// display the last 3 lines of file b

--------------------------------------------------------------------------------------------------------------------

Command Name: sort


Syntax: sort option filename
Description: It is used to sort the contents of a file
Example: sort r b

//reverse alphabetic order

sort f b

// forward alphabetic order

sort n

//numerical data sorting

sort u

//remove duplicate lines and display in output

sort o newfile file1

//sorted content of file1 is moved to newfile

--------------------------------------------------------------------------------------------------------------------

Command Name: grep (global search for regular expression)


Syntax: grep option pattern filename
Description: It is used to search for a particular pattern in a file.
Example: Let the filename is file1
grep v co file1 (display those lines which dont match the pattern co )
grep c co file1 (display count of those lines which match the pattern co )
grep n co file1 (display those lines which match the pattern co along with line no)
grep i co file1 (display lines which match the pattern co ignoring case distinction)
grep ^k file1 (display those lines which start with the character k in file1 )
grep k$ file1 (display those lines which end with the character k in file1 )
egrep(extended grep) :
egrep ab|pr f (display those lines which start with pattern ab or pr in file f )
fgrep (fixed grep) :
fgrep this is f (display those lines which contain the pattern this is in file f )
--------------------------------------------------------------------------------------------------------------------

Command Name: uniq


Syntax: uniq option filename
Description:
Example: uniq d f ( display consecutive-duplicate lines in file f )
uniq u f (display single occurrence lines in file f)
uniq c f ( display no of times before each line )

Command Name: cut


Syntax: cut option filename
Description: It is used to cut one or more field (column) from a file or output.

Example:
1. cut c 3 file1

//cut the 3rd character of each line in file1

2. cut d -f2 file1 //cut the field2(2nd column) of each line in file1
field
column2
separator

If file1 content is:- India is


My country
Then above command will display o/p :- is
country
Here field separator is single-space.
3. cut d -f2 -f3 file1 //cut the field1,field2 of each line in file1
--------------------------------------------------------------------------------------------------------------------

Command Name: pr
Syntax: pr filename | more
Description: It requires a file for printing by adding suitable header, footer &
formatted text.
Example: pr f | more
--------------------------------------------------------------------------------------------------------------------

Command Name: paste


Syntax: paste file1 file2 file3 . . . . . . .
Description: It merges the content of more than one file into a single file(line by
line)
Example: paste file1 file2
Command Name: tee
Syntax: tee file1
Description: It is used to store temporary results in a long pipeline.
Example: cat f | sort | tee temp
Here, sorted content of file f is stored in the file temp.
--------------------------------------------------------------------------------------------------------------------

Command Name: tr
Syntax: tr [a-z] [A-Z]
Description: It is used to translate all characters into uppercase.
Example: cat file1 | tr [a-z] [A-Z]

Shell Programming
1. PROGRAM TO FIND AVERAGE OF THREE NUMBERS
echo "enter three numbers"
read a
read b
read c
d=`expr $a + $b + $c`
e=`expr $d / 3`
echo "average=$e"
Output :enter three numbers
5
8
7
average=6
--------------------------------------------------------------------------------------------------2. PROGRAM USING if
echo "enter two number "
read a
read b
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
Output :enter two number
5
9
a is not equal to b
---------------------------------------------------------------------------------------------------

3. PROGRAM TO CHECK A NUMBER IS +VE OR VE


echo "enter a number"
read a
if [ $a -ge 0 ]
then
echo "number is positive "
else
echo "number is negative "
fi
Output :enter a number
7
number is positive
--------------------------------------------------------------------------------------------------4. PROGRAM USING if-else
echo "enter two number "
read a
read b
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
Output:enter two number
5
7
a is not equal to b
---------------------------------------------------------------------------------------------------

5. PROGRAM USING if-elif


echo "enter two number "
read a
read b
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
else
echo "a is less than b"
fi
Output:enter two number
4
9
a is less than b
---------------------------------------------------------------------------------------6. PROGRAM TO CHECK A NUMBER IS EVEN OR ODD.
echo "enter a number"
read a
n=`expr $a % 2`
if [ $n -eq 0 ]
then
echo "$a is a even no"
else
echo "$a is a odd no"
fi
Output:enter a number
7
7 is a odd no

---------------------------------------------------------------------------------------

7. PROGRAM TO FIND GREATEST AMONG THREE NUMBERS


echo "enter three numbers"
read a
read b
read c
big=$a
if [ $b -gt $big ]
then
big=$b
fi
if [ $c -gt $big ]
then
big=$c
fi
echo "$big is greatest"
Output:enter three numbers
5
9
3
9 is greatest
--------------------------------------------------------------------------------------------------8. PROGRAM TO DISPLAY GRADE ACCORDING TO MARK
echo "enter the average mark"
read mark
if [ $mark -ge 60 ]
then
echo "FIRST DIVISION"
elif [ $mark -ge 50 -a $mark -le 60 ]
then
echo "SECOND DIVISION"
elif [ $mark -ge 25 -a $mark -le 50 ]
then

echo "THIRD DIVISION"


else
echo "FAIL"
fi

Output :enter the average mark


69
FIRST DIVISION
--------------------------------------------------------------------------------------------------9. PROGRAM TO DISPLAY 1 TO 100
a=1
while [ $a -le 100 ]
do
echo "$a"
a=`expr $a + 1`
done
Output :1
2
3
.
.
.
99
100
---------------------------------------------------------------------------------------------------

10. PROGRAM TO DISPLAY ALL EVEN NUMBERS BETWEEN 1 TO 100


a=2
while [ $a -le 100 ]
do
echo "$a "
a=`expr $a + 2`
done
Output:2
4
6
.
.
.
98
100
--------------------------------------------------------------------------------------------------11. PROGRAM TO DISPLAY FIBONACCI SERIES
echo "enter number of term "
read n
a=0
b=1
echo "$a"
echo "$b"
m=`expr $n - 2`
while [ $m -ne 0 ]
do
c=`expr $a + $b`
echo "$c"
a="$b"
b="$c"
m=`expr $m - 1`
done

Output:enter number of term


7
0
1
1
2
3
5
8
--------------------------------------------------------------------------------------------------12. PROGRAM TO FIND FACTORIAL OF A NUMBER.
echo "enter a number"
read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "factorial of $n is $fact"
Output:enter a number
5
factorial of 5 is 120
--------------------------------------------------------------------------------------------------13. PROGRAM TO FIND SUM OF DIGITS OF A NUMBER.
echo "enter a number"
read n
temp=$n
while [ $temp -ne 0 ]
do
digit=`expr $temp % 10`

sum=`expr $sum + $digit`


temp=`expr $temp / 10`
done
echo "sum of digits of $n is $sum"
Output:enter a number
2314
sum of digits of 2314 is 10
--------------------------------------------------------------------------------------------------14. PROGRAM TO CHECK A NUMBER IS PALINDROME OR NOT.
echo "enter a number"
read n
temp=$n
rev=0
while [ $n -ne 0 ]
do
rem=`expr $n % 10`
rev=`expr $rev \* 10`
rev=`expr $rev + $rem`
n=`expr $n / 10`
done
if [ $rev -eq $temp ]
then
echo "number is palindrome "
else
echo "number is not palindrome "
fi
Output:enter a number
121
number is palindrome
---------------------------------------------------------------------------------------------------

15. PROGRAM TO CHECK A NUMBER IS PRIME OR NOT


echo "enter a number "
read n
count=0
m=$n
while [ $m -ne 0 ]
do
r=`expr $n % $m`
m=`expr $m - 1`
if [ $r -eq 0 ]
then
count=`expr $count + 1`
fi
done
if [ $count -eq 2 ]
then
echo " $n is prime no "
else
echo " $n not a prime no "
fi
Output:enter a number
7
7 is prime no
--------------------------------------------------------------------------------------------------16. PROGRAM TO DISPLAY A PYRAMID OF NUMBERS
a=0
while [ $a -lt 10 ]
do
b=$a
while [ $b -gt 0 ]
do
echo -n $b
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done

Output:1
21
321
4321
54321
654321
7654321
87654321
987654321
--------------------------------------------------------------------------------------------------17. PROGRAM USING FOR LOOP
ns="1 2 3 4 5"
for p in $ns
do
echo "$p"
done
Output:1
2
3
4
5
--------------------------------------------------------------------------------------------------18. PROGRAM TO DISPLAY A MULTIPLICATION TABLE
echo "enter a value"
read a
b=$a
for i in 1 2 3 4 5 6 7 8 9 10
do
a=`expr $b \* $i`
# \* represents multiplication
echo " $b * $i = $a"
done

Output:enter a value
5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
--------------------------------------------------------------------------------------------------19. PROGRAM TO CHECK A CHARACTER IS VOWEL OR NOT
echo "enter a character "
read c
case $c in
(a)echo "it is a vowel";;
(e)echo "it is a vowel";;
(i)echo "it is a vowel";;
(o)echo "it is a vowel";;
(u)echo "it is a vowel";;
(*)echo "it is not a vowel";;
esac
Output:enter a character
a
it is a vowel

---------------------------------------------------------------------------------------------------

20. PROGRAM USING break STATEMENT


a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
# break statement when executed, terminates the loop
fi
a=`expr $a + 1`
done
Output:0
1
2
3
4
5
--------------------------------------------------------------------------------------------------21. PROGRAM USING continue STATEMENT
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "an odd number!!"
done
Output:an odd number!!
Number is an even number!!
an odd number!!
Number is an even number!!
an odd number!!
Number is an even number!!
an odd number!!
---------------------------------------------------------------------------------------------------

22. PROGRAM USING continue STATEMENT


# continue forces for next iteration skipping the lines below it (inside the loop)

a=0
while [ $a -lt 10 ]
do
a=`expr $a + 1`
p=`expr $a % 2`
if [ $p -eq 0 ]
then
continue
fi
echo "$a"
done
Output:1
3
5
7
9
--------------------------------------------------------------------------------------------------23. PROGRAM TO DIPLAY PYRAMID
echo "enter no of rows"
read n
i=1
while [ $i -le $n ]
do
j=1
while [ $j -lt $i ]
do
echo -n " "# -n option is used to continue printing in same line instead of going to next line
j=`expr $j + 1`
done
k=$i
while [ $k -le $n ]
do
echo -n "* "
k=`expr $k + 1`
done
echo
# to go to next-line
i=`expr $i + 1`
done

Output:enter no of rows
10
**********
*********
********
*******
******
*****
****
***
**
*
--------------------------------------------------------------------------------------------------24. PROGRAM TO DIPLAY PYRAMID
echo "enter no of rows"
read n
i=1
while [ $i -le $n ]
do
j=$i
while [ $j -lt $n ]
do
echo -n " "
j=`expr $j + 1`
done
k=1
while [ $k -le $i ]
do
echo -n "* "
k=`expr $k + 1`
done
echo
i=`expr $i + 1`
done

Output:enter no of rows
10
*
**
***
****
*****
******
*******
********
*********
**********
--------------------------------------------------------------------------------------------------25. PROGRAM TO REVERSE A STRING ON COMMAND LINE
s=`echo $1|wc -c`
echo -n "reverse string is "
while [ $s -gt 0 ]
do
temp=`echo -n $1|cut -c $s`
s=`expr $s - 1`
echo -n "$temp"
done
Output:sh prog25.sh driems
reverse string is smeird

--------------------------------------------------------------------------------------------------26. PROGRAM TO DISPLAY THE ELEMENTS OF ARRAY


i=0
arr=(2 5 3 7)
while [ $i -lt 4 ]
do
echo ${arr[i]}
i=`expr $i + 1`
done

Output:2
5
3
7
--------------------------------------------------------------------------------------------------27. PROGRAM TO ADD THE ELEMENTS OF ARRAY
echo "how many numbers "
read n
i=0
sum=0
arr=( )
echo " enter $n numbers"
while [ $i -lt $n ]
do
echo -n "x="
read arr[i]
sum=`expr $sum + ${arr[i]}`
i=`expr $i + 1`
done
echo " sum is =$sum"
Output:how many numbers
4
enter 4 numbers
x=3
x=7
x=6
x=4
sum is =20

You might also like