0% found this document useful (0 votes)
208 views

Linux 2 PDF

The document provides an overview of Linux including its history, features, and pros and cons. It discusses that Linux is an open-source operating system originally created by Linus Torvalds in 1991. Key features highlighted include multitasking, multi-user support, memory protection between processes, virtual memory, and compatibility with POSIX and UNIX standards. The document also outlines several advantages of using Linux such as it being free, stable, and less vulnerable to malware compared to other operating systems.

Uploaded by

Pavitar Khatri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
208 views

Linux 2 PDF

The document provides an overview of Linux including its history, features, and pros and cons. It discusses that Linux is an open-source operating system originally created by Linus Torvalds in 1991. Key features highlighted include multitasking, multi-user support, memory protection between processes, virtual memory, and compatibility with POSIX and UNIX standards. The document also outlines several advantages of using Linux such as it being free, stable, and less vulnerable to malware compared to other operating systems.

Uploaded by

Pavitar Khatri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

INDEX

Sno Topic Sign Remarks

# Introduction to Linux

# History of Linux

# Features of Linux

# Introduction about Shells

# Commands

=> ShellScripts

1 Write a shell script to add two numbers.

2 Write a shell script to multiply two numbers.

3 Write a shell script to divide two numbers.

4 Write a shell script to find remainder of two numbers.

5 Write a shell script to subtract two numbers.

6 Write a shell script to perform modulus operation on two numbers.

7 Write a shell script to calculate the area of a triangle.

8 Write a shell script to calculate the area of a rectangle.

9 Write a shell script to calculate the area of a square.

10 Write a shell script to calculate the area of a circle.

11 Write a shell script to find whether a number entered is even or odd.

12 Write a shell script to find the status of the student according to the given
constraints
13 Write a shell script to find whether the year entered is leap or not.

14 Write a shell script to find the largest of three numbers


15 Write a shell script to find the larger of two numbers.

16 Write a shell script to convert Fahrenheit to Celsius.

17 Write a shell script to illustrate case statement by making 12 cases. Each


representing a month in a year.
18 Write a shell script to create a menu driven program for performing
arithmetic operations on two numbers.
Write a shell script to illustrate case statement defining a case for
19
each [A-Z], [a-z] and [0-9].

20 Write a shell script to illustrate date, ls, ps, time.

21 Write as shell program to find the sum of first n natural numbers.

22 Write a shell script to sort n numbers.

23 Write a shell script to print the given pattern.

24 Write a shell script to print the given pattern.(5 patterns)

25 Write a shell script to check whether a given number is prime or not.

26 Write a shell script to calculate the factorial of a number.

27 Write a shell script to display fibonacci series.

28 Write a shell script to display multiplication table of a number.

29 Write a shell script to find the position of a word from the string.

Write a shell script to count the number of words, characters and


30
blank spaces.
Introduction to LINUX
ABOUT

Linux is a Unix-like computer operating system assembled under the model of free and open
source software development and distribution. The defining component of Linux is the Linux
kernel, an operating system kernel first released 5 October 1991 by Linus Torvalds.

The term Linux properly refers to just the operating system kernel itself. However, in popular
parlance it is used to refer to a complete Linux Distribution which includes GUI components and
many other utilities, many of which are supplied by the GNU Project. Android, for example also
uses the Linux kernel but includes different components to most desktop Linux distributions.

Linux is a UNIX-based operating system originally developed as for Intel-compatible PC's. It is


now available for most types of hardware platforms, ranging from PDAs (and according to some
reports, a wristwatch) to mainframes. Linux is a "modern operating system", meaning it has such
features as virtual memory, memory protection, and preemptive multitasking.

Linux is a leading operating system on servers and other big iron systems such as mainframe
computers and supercomputers: more than 90% of today's 500 fastest supercomputers run some
variant of Linux, including the 10 fastest. Linux also runs on embedded systems (devices where
the operating system is typically built into the firmware and highly tailored to the system) such
as mobile phones, tablet computers, network routers, televisions and video game consoles; the
Android system in wide use on mobile devices is built on the Linux kernel.

The development of Linux is one of the most prominent examples of free and open source
software collaboration: the underlying source code may be used, modified, and distributed—
commercially or non-commercially—by anyone under licenses such as the GNU General Public
License. Typically Linux is packaged in a format known as a Linux distribution for desktop and
server use. Some popular mainstream Linux distributions include Debian (and its derivatives
such as Ubuntu), Fedora and openSUSE. Linux distributions include the Linux kernel,
supporting utilities and libraries and usually a large amount of application software to fulfil the
distribution's intended use.
FEATURES OF LINUX

1. Multitasking: Several programs running at the same time.

2. Multi-user: several users on the same machine at the same time (and no two-user
licenses!).

3. Multiplatform: Runs on many different CPUs, not just Intel.

4. Multiprocessor: SMP support is available on the Intel and SPARC platforms (with work
currently in progress on other platforms), and Linux is used in several loosely-coupled
MP applications, including Beowulf systems

5. Multithreading: has native kernel support for multiple independent threads of control
within a single process memory space.

6. Has memory protection between processes, so that one program can'tbring the whole
system down.
7. Demand loads executables: Linux only reads from disk those parts of a program that are
actually used.

8. Shared copy-on-write pages among executables: This means that multiple processes
can use the same memory to run in. When one tries to write to that memory, that page
(4KB piece of memory) is copied somewhere else.
9. Copy-on-write has two benefits: increasing speed and decreasing memory use.

10. Virtual memory using paging (not swapping whole processes) to disk:to a separate
partition or a file in the file system, or both, with the possibility of adding more swapping
areas during runtime (yes, they're still called swappingareas). A total of 16 of these 128
MB (2GB in recent kernels) swapping areas can be used at the same time, for a
theoretical total of 2 GB of useable swap space. It is simple to increase this if necessary,
by changing a few lines of source code.

11. A unified memory pool for user programs and disk cache, so that all free memory can
be used for caching, and the cache can be reduced when running large programs.

12. Dynamically linked shared libraries (DLL's), and static libraries too, of course.

13. Mostly compatible with POSIX , System V, and BSD at the source level.
History of LINUX
Linux is an operating system that was initially created as a hobby by a young student, Linus
Torvalds, at the University of Helsinki in Finland. Linus had an interest in Minix, a small UNIX
system, and decided to develop a system that exceeded the Minix standards. He began his work
in 1991 when he released version 0.02 and worked steadily until 1994 when version 1.0 of the
Linux Kernel was released. The kernel, at the heart of all Linux systems, is developed and
released under the GNU General Public License and its source code is freely available to
everyone. It is this kernel that forms the base around which a Linux operating system is
developed. There are now literally hundreds of companies and organizations and an equal
number of individuals that have released their own versions of operating systems based on the
Linux kernel. More information on the kernel can be found at our sister site, LinuxHQ and at the
official Linux Kernel Archives. The current full-featured version is 2.6 (released December
2003) and development continues.

Apart from the fact that it's freely distributed, Linux's functionality, adaptability and robustness,
has made it the main alternative for proprietary Unix and Microsoft operating systems. IBM,
Hewlett-Packard and other giants of the computing world have embraced Linux and support its
ongoing development. Well into its second decade of existence, Linux has been adopted
worldwide primarily as a server platform. Its use as a home and office desktop operating system
is also on the rise. The operating system can also be incorporated directly into microchips in a
process called "embedding" and is increasingly being used this way in appliances and devices.
Pros & Cons of LINUX

Pros

 Freedom: Most Linux distros are free. Users do not need to pay for a copy, but this is
only one aspect of freedom enjoyed by Linux users! In addition, Linux distros can be
freely downloaded and legally installed on as many computers as you want and freely
(and legally) given to other people. Because most distros are open source, you have
access to the source code and can customize Linux to be whatever you want it to be; you
can even create your own distro if you like!

 Linux is easy to install: In many instances, it is actually easier to install Linux to your
computerthan Windows.

 Linux is very stable: Linux systems rarely crash, and when they do, the whole system
normally does not go down. The "blue screen of death" familiar to Windows users is not
a worry for Linux users.

 Linux is less vulnerable to computer malware: Because most computer


malware are designed to attack Windows (often through Active X which is not typically
found in Linux) the odds are considerably less for Linux to be infected with a virus than
Windows. The same holds true with spyware, trojans, and worms. While Linux malware
does exist, they are relatively few in number and none have become widespread so far.
While Linux is very secure by its nature, users should still employ good sense while
surfing the Internet. As long as Linux users download and install only from their distro's
official software repository, then security is greatly increased.

 Linux can breathe new life into old computers: If you have an older computer
(especially Pentium III or later) laying around, you can install Linux and in essence have
a new computer. In many cases Linux will run faster and you can do all of the basics such
as browse the Internet, email, play games, and create and edit documents, spreadsheets,
and PowerPoint presentations. It should also be mentioned that Linux runs great on newer
computers as well.
Cons

 Understanding – Becoming familiar with the Linux operating system requires patience
as well as a strong learning curve. You must have the desire to read and figure things out
on your own, rather than having everything done for you.

 Compatibility – Because of its free nature, Linux is sometimes behind the curve when it
comes to brand new hardware compatibility. Though the kernel contributors and
maintainers work hard at keeping the kernel up to date, Linux does not have as much of a
corporate backing as alternative operating systems. Sometimes you can find third party
applications, sometimes you can’t.

 Alternative Programs – Though Linux developers have done a great job at creating
alternatives to popular Windows applications, there are still some applications that exist
on Windows that have no equivalent Linux application.

 There is a learning curve for people who are new to Linux: Despite this, most Linux
distros, especially the major ones, are very intuitive and user-friendly. Also, the desktop
environments in Linux are in many ways similar to Windows in their appearance. One
thing which should be emphasized is that there is also a learning curve for Windows XP
users who switch to Windows 7

 There is a smaller selection of peripheral hardware drivers for Linux: There is a


smaller selection of peripheral hardware drivers (for printers, scanners, and other devices)
in Linux as compared to Windows, though many new Linux hardware drivers are
constantly being added
Introduction about Shells
Shells are what allow a user to access the kernel. Shells are actually programs that interpret
command lines and run other programs. A shell can read command lines from a terminal or from
a file (called a shell script or shell program). Because the shell itself is a program, scripting is
possible without using a programming language such as Perl or C. Any command that can be
typed after a prompt can be used in a shell script.

The script is simply a file that holds a sequence of commands. UNIX is basically composed of
separate utilities that can run from a shell prompt. These utilities can be combined with tools like
pipes and I/O redirection. Shell scripting allows users to combine utilities and create programs
that meet specific needs.

Which shell to use for scripting is, for the most part, a matter of preference. However, as the
Bourne shell is the one distributed with UNIX operating systems, it will be the most consistently
available.

Types of Shells

 Sh
The Bourne shell, or sh, was the default Unix shell of Unix Version 7, and replaced the
Thompson shell, whose executable file had the same name, sh. It was developed by
Stephen Bourne, of AT&T Bell Laboratories, and was released in 1977 in the Version 7
Unix release distributed to colleges and universities. It remains a popular default shell for
UNIX accounts. The binary program of the Bourne shell or a compatible program is
located at /bin/sh on most UNIX systems, and is still the default shell for the root
superuser on many current UNIX implementations.

 ash
The Almquist shell (also known as A Shell or ash) was originally Kenneth Almquist’s
clone of the SVR4-variant of the Bourne shell; it is a fast, small, POSIX-compatible
UNIX shell designed to replace the Bourne shell in later BSD distributions. By intention
it did not feature line editing or command history mechanisms originally, because
Almquist felt that such should be moved into the terminal driver. Current variants have
emacs and vi modes.
 Dash
Debian Almquist shell (dash) is a POSIX-compliant Unix shell, much smaller than bash.
It requires less disk space but is also less feature rich. dash is a direct descendant of the
NetBSD version of the Almquist Shell (ash). It was ported to Linux by Herbert Xu in
early 1997. It was renamed to dash in 2002.dash executes scripts faster than bash and
depends on fewer libraries. It is believed to be more reliable in case of upgrade problems
or disk failures.

 Bash
Bash is a UNIX shell written for the GNU Project. The name of the actual executable is
bash. Its name is an acronym for Bourne-again shell, a pun on the name of the Bourne
shell (sh) (i.e. “Bourne again” or “born again”), an early and important Unix shell written
by Stephen Bourne and distributed with Version 7 Unix circa 1978. Bash was created in
1987 by Brian Fox. In 1990 Chet Ramey became the primary maintainer. Bash is the
default shell on most Linux systems as well as on Mac OS X and it can be run on most
Unix-like operating systems.

 Fish
Fish is a UNIX shell. Its name is an acronym for friendly interactive shell. fish focuses on
interactive use, discoverability, and user friendliness. The design goal of fish is to give
the user a rich set of powerful features in a way that is easy to discover, remember, and
1.Write a shell script to add two numbers.

echo "Program to add two numbers"


echo "Enter two numbers"
read num1 num2
sum=$(expr "$num1" + "$num2")
echo "The sum is = $sum"

Output :
msi@msi-desktop:~$ bash add.sh
Program to add two numbers
Enter two numbers
12
The sum is = 3
2. Write a shell script to subtract two numbers.

echo "Program to subtract two numbers"


echo "Enter two numbers"
read num1 num2
sum=$(expr "$num1" - "$num2")
echo "The sum is = $sum"

Output :
msi@msi-desktop:~$ bash sub.sh
Program to subtract two numbers
Enter two numbers
12 3
The sum is = 9
3. Write a shell script to multiply two numbers.

echo "Program to multiply two numbers”


echo "Enter two numbers"
read num1 num2
m=$(expr "$num1" * "$num2")
echo "The sum is = $m"

Output:
msi@msi-desktop:~$ bash mul.sh
Program to multiply two numbers
Enter two numbers
21 3
The number is = 63
4. Write a shell script to divide two numbers.

echo "Program to divide two numbers"


echo "Enter two numbers"
read num1 num2
d=$(expr "$num1" / "$num2")
echo "The sum is = $d”

Output :
msi@msi-desktop:~$ bash div.sh
Program to divide two numbers
Enter two numbers
24 12
The number is = 2
6.Write a shell script to perform modulus operation on two numbers.

echo "Program to perform the modulus operation on two numbers"


echo "Enter two numbers"
read num1 num2
d=$(expr "$num1" % "$num2")
echo "The sum is = $d"

Output:
msi@msi-desktop:~$ bash mod.sh
Program to divide two numbers
Enter two numbers
20 4
The number is = 5

7. Write a shell script to calculate the area of a triangle

echo "Enter the height and the base of the triangle : "
read b h
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
echo "Area of a triangle is $area"

Output:
msi@msi-desktop:~$ bash triangle.sh
Enter the height and the base of the triangle :
10 15
Area of a triangle is 75.00
8.Write a shell script to calculate the area of a rectangle

echo "Enter the length and the breadth of the rectangle : "
read l b
area=$(expr "$l" * "$b")
echo "Area of the rectangle is: "$area

Output:
msi@msi-desktop:~$ bash rectangle.sh
Enter the length and the breadth of the rectangle :
12 31
Area of the rectangle is: 372

9.Write a shell script to calculate the area of the square

echo "Enter the side of the square : "


read a
area=$(expr "$a" * "$a")
echo "Area of the square is: "$area

Output:
msi@msi-desktop:~$ bash square.sh
Enter the side of the square :
12
Area of the square is: 144
10.Write a shell script to find the area of a circle.

echo "Enter the radius of a circle : "


read r
area=$(echo "scale=2;3.14 * ($r * $r)" | bc)
d=$(echo "scale=2;2 * $r"|bc)
circumference=$(echo "scale=2;3.14 * $d"| bc)
echo "Area of circle is $area"
echo "Circumference of circle is $circumference”

Output:
msi@msi-desktop:~$ bash circle.sh
Enter the radius of a circle :
6
Area of circle is 113.04
Circumference of circle is 37.68
11.Write a shell script to find whether a number entered is even or odd.

echo "Enter a number: "


readnum

if [ `expr $num % 2` -eq 0 ]


then
echo "The number entered is even!"
else
echo "The number entered is odd!"
fi

Output:
msi@msi-desktop:~$ bash oddeven.sh
Enter a number:
12
The number entered is even!
12.Write a shell script to find the status of the student according to the given constraints:
If marks >=90… Outstanding
If marks >= 75 and < 90… Excellent
If marks >= 60 and < 75… Very Good
If marks >= 50 and < 60… Good
If marks < 50… Poor”

echo "Enter the marks of the student : "


read marks
if [ $marks -ge 90 ]
then
echo "Outstanding"

elif [ $marks -ge 75 ]


then
echo "Excellent"

elif [ $marks -ge 60 ]


then
echo "Very Good"

elif [ $marks -ge 50 ]


then
echo "Good"
else
echo "Poor"
fi
Output:
msi@msi-desktop:~$ bash status.sh
If marks >=90… Outstanding
If marks >= 75 and < 90… Excellent
If marks >= 60 and < 75… Very Good
If marks >= 50 and < 60… Good
If marks < 50… Poor
qs4.sh: line 6: Ans.: command not found
Enter the marks of the student :
88
Excellent
13.Write a shell script to find whether the year is leap or not.

echo "Enter year"


read y
check=0
if [ `expr $y % 4` -eq 0 ]
then
if [ `expr $y % 100` -eq 0 -a `expr $y % 400` -eq 0 ]
then
check=1
else
if [ `expr $y % 100` -ne 0 -a `expr $y % 4` -eq 0 ]
then
check=1
fi
fi
fi
if [ $check -eq 0 ]
then
echo "Not a Leap year"
else
echo "Leap year"
fi

Output:

msi@msi-desktop:~$ bash leap.sh


Enter year
2011
Not a Leap year

msi@msi-desktop:~$ bash leap.sh


Enter year
2004
Leap year
14.Write a shell script to find the largest number among three.

echo "Enter Three Numbers"


read x
read y
read z
if((x>y && x>z))
then
echo "First number Is Largest"
else
if((y>z))
then
echo "Second Number Is Largest"
else
echo "Third Number Is Largest"
fi
fi

Output:

msi@msi-desktop:~$ bash largest.sh


Enter Three Numbers
4
6
1
Second Number Is Largest
15.Write a shell script to find the larger of two numbers

echo "Enter 2 numbers with spaces in between"


read a b

if [ $a -gt $b ]
then
echo "$a is larger than $b"

else
echo "$b is larger than $a"
fi

Output:
msi@msi-desktop:~$ bash larger.sh
Enter 2 numbers with spaces in between
23 32
32 is larger than 23
16. Write a shell script to convert the temperature Fahrenheit into Celsius.

echo "enter temperature in fahrenheit"


read fhr
num1=0
num1=`expr $fhr - 32`
num2=0
num2=`expr $num1 \* 5`
celsius=0
celsius=`expr $num2 / 9`
echo "temperature in celsius is "$celsius

Output:

msi@msi-desktop:~$ bash temperature.sh


enter temperature in Fahrenheit
41
temperature in celsius is 5

msi@msi-desktop:~$ bash temperature.sh


enter temperature in Fahrenheit
54
temperature in celsius is 12
17.Write a shell script to illustrate case statement by making 12 cases. Each representing a
month in a year.

echo "Enter a number between 1 and 12. "


readmnth
case $mnth in
1) echo "January" ;;
2) echo "February" ;;
3) echo "March" ;;
4) echo "April" ;;
5) echo "May" ;;
6) echo "June" ;;
7) echo "July" ;;
8) echo "August" ;;
9) echo "September" ;;
10) echo "October" ;;
11) echo "November" ;;
12) echo "December" ;;
*) echo "Enter a valid number between 1 and 12!" ;;
esac

Output:
msi@msi-desktop:~$ bash month.sh
Enter a number between 1 and 12.
11
November
18. Write a shell script to create a menu driven program for performing arithmetic
operations on two numbers.

echo "Enter two numbers: "


read num1 num2
echo "Arithmetic operations menu: "
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Modulus operation"

echo -n "enter your choice: "


read choice

case $choice in
1) sum=$(expr "$num1" + "$num2")
echo "The addition of the two nubbers is: $sum";;

2) sub=$(expr "$num1" - "$num2")


echo "The subtraction of the two numbers is: $sub";;

3) mul=$(expr "$num1" * "$num2")


echo "The product of the two numbers is: $mul";;

4) div=$(expr "$num1" / "$num2")


echo "The division of the two number is: $div";;
5) mod=$(expr "$num1" % "$num2")
echo "The result of modulus operation is: $mod";;
*) echo "Enter a valid choice between 1 to 5!" ;;
esac

Output:
Arithmetic operations menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus operation
enter your choice: 1
The addition of the two numbers is: 10
msi@msi-desktop:~$ bash arith.sh
Enter two numbers:
64
Arithmetic operations menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus operation
enter your choice: 2
The subtraction of the two numbers is: 2
msi@msi-desktop:~$ bash arith.sh
Enter two numbers:
64
Arithmetic operations menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus operation
enter your choice: 3
The product of the two numbers is: 24
msi@msi-desktop:~$ bash arith.sh
Enter two numbers:
64
Arithmetic operations menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus operation
enter your choice: 4
The division of the two number is: 1
msi@msi-desktop:~$ bash arith.sh
Enter two numbers:
64
Arithmetic operations menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus operation
enter your choice: 5
The result of modulus operation is: 2
19.Write a shell script to illustrate case statement defining a case for each [A-Z], [a-z] and
[0-9]

echo "Enter any number between 0-9 or a character between A-Z or a-z"
readch

case $ch in

[a-z]) echo "You entered a lowercase alphabet" ;;


[A-Z]) echo "You entered an uppercase alphabet" ;;
[0-9]) echo "You have entered a digit" ;;
*) echo "Invalid input!" ;;

esac

Output:
msi@msi-desktop:~$ bash az.sh
Enter any number between 0-9 or a character between A-Z or a-z
G
You entered an uppercase alphabet
20.Write a shell script to illustrate date, ls, ps, time.

echo "Command execution menu"


echo "1. Date"
echo "2. Processes of the user"
echo "3. Process timing”
echo "4. List of files"
echo "5. Exit”

echo "Enter your choice: "


read choice

case $choice in

1) date ;;
2) ps -f ;;
3) timesort –o newlistinvoice.lst;;
4) ls ;;
5) exit ;;
*) echo "Please enter a valid number between 1 to 5!"

esac
Output:
msi@msi-desktop:~$ bash is.sh
Command execution menu
1. Date
2. Processes of the user
3. Time
4. List of files
5. Exit
Enter your choice:
4
input.txt
main.sh
21.Write a shell script to calculate sum of first n natural numbers.

echo "Enter no. of terms upto which you want to print the sum"
read n
sum=0
for((i=0;i<=n;i++))
do
sum=$(expr $sum + $i)
done
echo Sum of first $n natural numbers is= $sum

Output:

msi@msi-desktop:~$ bash natural.sh


Enter no. of terms upto which you want to print the sum
8
Sum of first 8 natural numbers is= 36
22.Write a shell script to sort n numbers.

echo Enter the data


read array
echo > data
for i in $array
do
echo $i >> data
done
echo The sorted order is:
sort -g data

Output:

msi@msi-desktop:~$ bash sort.sh


Enter the data
3 2 4 67 1 7
The sorted order is:
1
2
3
4
7
67
23. Write a shell script to print the pattern:

11111
22222
33333
44444
55555

echo "shell script for pattern"


for (( i=1;i<=5;i++ ))
do
for(( j=1;j<=5;j++ ))
do
echo -n "$i"
done
echo "" #### print the new line####
done

Output:

msi@msi-desktop:~$ bash pattern.sh


shell script for pattern
11111
22222
33333
44444
55555
24.Write a shell script to print the following patterns:

a.1
12
123

for (( i=1; i<=3; i++ ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done

Output:
msi@msi-desktop:~$ bash pattern2.sh
1
12
123
b. 1
12
1 2 3

for ((i=1;i<=3;i++))
do
for((s=2;s>=i;s--))
do
echo -n " "
done
for((j=1;j<=i;j++))
do
echo -n $j
echo -n " "
done
echo
done

Output:

msi@msi-desktop:~$ bash pattern1.sh


1
12
1 2 3
c. 1
22
333
4444

echo "can you see the following:"


for(( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n "$i"
done
echo ""
done

Output:

msi@msi-desktop:~$ bash onetwo.sh


can you see the following:
1
22
333
4444
55555
d.123
12
1

for (( i=3; i>=1; i-- ))


do
for (( j=1; j<=i; j++ ))
do
echo -n "$j"
done
echo ""
done

Output:
msi@msi-desktop:~$bash -f main.sh
123
12
1
e. 1 2 3
1 2
1

for ((i=3;i>=1;i--))
do
for((s=2;s>=i;s--))
do
echo -n " "
done
for((j=1;j<=i;j++))
do
echo -n $j
echo -n " "
done
echo
done

Output:

msi@msi-desktop:~$ bash pattern.sh


1 2 3
1 2
1
25.Write a shell script to check whether a number is prime or not.

echo "enter no"


read num
i=2
while [ $i -lt $num ]
do
if [ $(expr $num % $i) -eq 0 ]
then
echo "number is not prime"
break
fi
i=$(expr $i + 1)
done
if [ $i -eq $num ]
then
echo "no is prime"
fi

Output:

msi@msi-desktop:~$ bash prime.sh


enter no
2
no is prime
msi@msi-desktop:~$ bash prime.sh
enter no
4
number is not prime
26.Write a shell script to find the factorial of a given number.

echo "enter the no"


read num
fact=1
a=$num
while [ $a -gt 0 ]
do
fact=$(expr $fact \* $a)
a=$(expr $a - 1)
done
echo "the factorial of $num = $fact"

Output:

msi@msi-desktop:~$ bash factorial.sh


enter the no
7
the factorial of 7 = 5040
27.Write a shell script to print the Fibonacci series.

f1=0
f2=1
f3=1
echo "Fibonacci series : - "
for e in 1 2 3 4 5 6 7 8 9 10
do
echo "$f3"
f3=$(expr $f1 + $f2)
f1=$f2
f2=$f3
done
exit 0

Output:

msi@msi-desktop:~$ bash series.sh


Fibonacci series : -
1
1
2
3
5
8
13
21
34
55
28.Write a shell script to display multiplication table of a number.

echo "enter the number"


read num
i=1
while [ $i -le 10 ]
do
echo "$num * $i = $(expr $num \* $i)"
i=$(expr $i + 1)
done

Output

msi@msi-desktop:~$ bash table.sh


enter the number
2
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
29.Write a shell script to find the position of a word from the string.

echo "enter some text"


read text
echo "enter the word to be searched"
read keyword
count=0
for i in $text
do
count=$(expr $count + 1)
if [ $keyword = $i ]
then
echo "position of word is $count"
exit 0
fi
done
echo "no such word found"
exit 0

Output:

msi@msi-desktop:~$ bash search.sh


enter some text
welcome to linux enviornment
enter the word to be searched
enviornment
position of word is 4
30.Write a shell script to count number of words, characters and blank space.
echo "enter a text"
read text
choice=1
countwords()
{
echo "you choose to count the no. of words"
echo $text > textfile.text
a=$(wc -w textfile.text)
for file in $a
do
ans=$(expr $file)
echo "no. of words = $ans"
return
done
}
countchars()
{
echo "you choose to count the no. of chars"
echo $text > textfile.text
a=$(wc -m textfile.text)
for file in $a
do
ans=$(expr $file)
echo "no. of chars = $ans"
return
done
}
countblanks()
{
echo "you choose to count the no. of blanks"
echo $text > textfile.text
a=$(wc -l textfile.text)
for file in $a
do
ans=$(expr $file -1)
echo "no. of blanks = $ans"
return
done
}
printtext()
{
echo "the entered text are"
echo $text
return
}
while [ $choice -ne 0 ]
do
echo "menu"
echo "1. count no. of words"
echo "2. count no. of chars"
echo "3. count no. of blanks"
echo "4. count no. of print text"
echo "0. exit"
echo "your choice"
read choice
if test $choice -eq 1
then
countwords
elif test $choice -eq 2
then
countchars
elif test $choice -eq 3
then
countblanks
elif test $choice -eq 4
then
printtext
elif test $choice -ne 0
then
echo "not a choice"
fi
done
Output:
msi@msi-desktop:~$ bash count.sh
enter a text
winds its way down from the peaks
menu
1. count no. of words
2. count no. of chars
3. count no. of blanks
4. count no. of print text
0. exit
your choice
4
the entered text are
winds its way down from the peaks
menu
1. count no. of words
2. count no. of chars
3. count no. of blanks
4. count no. of print text
0. exit
your choice
1
you choose to count the no. of words
no. of words = 7
menu
1. count no. of words
2. count no. of chars
3. count no. of blanks
4. count no. of print text
0. exit
your choice

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy