Linux 2 PDF
Linux 2 PDF
# Introduction to Linux
# History of Linux
# Features of Linux
# Commands
=> ShellScripts
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.
29 Write a shell script to find the position of a word from the string.
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 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
2. Multi-user: several users on the same machine at the same time (and no two-user
licenses!).
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 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
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.
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.
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.
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.
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.
Output:
msi@msi-desktop:~$ bash mod.sh
Program to divide two numbers
Enter two numbers
20 4
The number is = 5
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
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.
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.
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”
Output:
Output:
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.
Output:
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.
case $choice in
1) sum=$(expr "$num1" + "$num2")
echo "The addition of the two nubbers is: $sum";;
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
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.
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:
Output:
11111
22222
33333
44444
55555
Output:
a.1
12
123
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:
Output:
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:
Output:
Output:
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:
Output
Output: