Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 111

1

Linux &shell programming

1. INSTALLATION OF UBUNTU 14

1) Booting Computer with Ubuntu 14.04 installation media.


Boot your computer with Ubuntu 14.04 installation media like (Burned CD/DVD or ISO
image). After booting from your installation media it will display the below screen.

2) Choosing Ubuntu installation


Choose install Ubuntu to install Ubuntu 14.04 on your system

Dept. Of Computer Applications ,SNGCE


2

Linux &shell programming

3) Preparing to Install Ubuntu


Verifying Hard disk whether sufficient space available or not to install and checking
internet connection to download required files, and hit Continue button to move forward.

4) Installation Type
This step is important one but simple. For Fresh installation choose first option Erase disk
and install Ubuntu and it will erase whole disk and create partition table automatically
then move forward. If you choose fourth option something else and it will also erase
wholedisk but need to create partition table manually then move forward.

Dept. Of Computer Applications ,SNGCE


3

Linux &shell programming

Its fresh installation, so I’m choosing first option and hit Install Now

5) Choose your location


Choose your location then hit Continue.

Dept. Of Computer Applications ,SNGCE


4

Linux &shell programming

6) Keyboard layout Selection


Choose your Keyboard layout here, then hit Continue.

7) Create new user and password


Create new user and password on your system then hit Continue.

Dept. Of Computer Applications ,SNGCE


5

Linux &shell programming

8) Copying files
Ubuntu Installer now copying all the system files to your hard disk.

9) Installation process is on-going


Ubuntu 14.04 LTS (Trusty Tahr) Installation process is on-going and it will take shot
while to complete the installation

Dept. Of Computer Applications ,SNGCE


6

Linux &shell programming

10) Installation got completed.


Now, installation got completed and Remove your installation media then hit Restart
Now button.

11) Ubuntu 14.04 login screen


Ubuntu 14.04 LTS (Trusty Tahr) login screen, enter your password and hit Enter button to
launch your Desktop

Dept. Of Computer Applications ,SNGCE


7

Linux &shell programming

12) Ubuntu 14.04 LTS Desktop screen


Ubuntu 14.04 LTS (Trusty Tahr) Desktop screen and play with new Ubuntu.

Dept. Of Computer Applications ,SNGCE


8

Linux &shell programming

2. DESCRIPTION ON LINUX DISTRIBUTION


A Linux distribution is an operating system made as a collection of software based around the
Linux kernel and often around a package management system. Linux distributions are available
for a wide variety of systems, from embedded devices and personal computers up to the
powerful supercomputers with specialized functionalities or down to small embedded systems.

Linux distributions are primarily based on free and open-source software. There are
currently over six hundred Linux distributions. The some Linux distributions are discussed here.

1. Mandriva
2. Ubuntu
3. Redhat
4. Fedora
5. SUSE
6. Debian

Mandriva

Mandriva Linux (formerly Mandrake Linux) is a Red Hat-based operating system created by the
French software company Mandriva (formerly Mandrakesoft). It is the result of the technical
merging of the former Mandrake Linux and Conectiva distributions. Mandriva is released in
both desktop and server flavors. The latest version is Mandriva Linux 2011, "Hydrogen".

Mandriva provides multiple editions of Linux. Some were freely distributable until 2012, while
now they are only sold online and via a network of authorized resellers. Prior open source code

of the old Mandriva Free was later forked into free derivatives like Mageia
and OpenMandriva Lx.

Dept. Of Computer Applications ,SNGCE


9

Linux &shell programming

Ubuntu

The vision for Ubuntu is part social and part economic: free software, available to everybody on
the same terms, and funded through a portfolio of services provided by Canonical.

Ubuntu is different from the commercial Linux offerings that preceded it because it doesn't
divide its efforts between a high-quality commercial version and a free 'community' version.
The commercial and community teams collaborate to produce a single, high-quality release,
which receives ongoing maintenance for a defined period. Both the release and ongoing
updates are freely available to all users.

The first official Ubuntu release -- Version 4.10, codenamed the 'Warty Warthog' — was
launched in October 2004, and sparked dramatic global interest as thousands of free software
enthusiasts and experts joined the Ubuntu community.

Ubuntu today has eight flavors and dozens of localized and specialized derivatives. There are
also special editions for servers, OpenStack clouds, and mobile devices. All editions share
common infrastructure and software, making Ubuntu a unique single platform that scales from
consumer electronics to the desktop and up into the cloud for enterprise computing.

Dept. Of Computer Applications ,SNGCE


10

Linux &shell programming

Red hat

Red Hat was one of the first companies to realize that "free" software could be sold as a
product. After examining the successful marketing campaign of Evian water, Red Hat concluded
that to achieve success, the company had to create more Linux users and brand Red Hat as the
Linux name that customers preferred. Today, the "Red Hat Plan" is discussed as a model in
business schools.

The advantage to buying the distribution from Red Hat, rather than assembling it yourself at no
cost, is the support you receive. Red Hat offers services that aren't provided as quickly by
individual component developers, including members of the Free Software Foundation. Like all
free software, Red Hat's packages allow the buyer to modify and even resell modified versions
of code as long as they do not restrict anyone else from further modification.

Red Hat provides over 400 different software packages, including the C language compiler from
Cygnus, a Web server from Apache, and the X Window System from X Consortium.

Dept. Of Computer Applications ,SNGCE


11

Linux &shell programming

Fedora

Fedora has a relatively short life cycle: version X is supported only until 1 month after version
X+2 and with approximately 6 months between versions, this means that a version of Fedora is
supported for approximately 13 months. This promotes leading-edge software because it frees
developers from some backward compatibility restraints, but it also makes Fedora a poor
choice for product development, which usually requires long-term vendor-support. Fedora
users can upgrade from version to version using FedUp

Fedora comes installed with a wide range of software such as LibreOffice and Firefox.
Additional software is available from the software repositories and can be installed using the
yum or dnf package managers with graphical interfaces, such as GNOME Software, also being
available.

Suse

SUSE (suu-zah) is a German-based, multinational, open-source software Company that develops


and sells Linux products to business customers. Founded in 1992, it was the first company to
market Linux for the enterprise. It is also the primary sponsor of the community-supported
openSUSE Project which develops the openSUSE Linux distribution. The company is a wholly
owned subsidiary of The Attachmate Group.

Starting with the launch of the SUSE Linux Enterprise 10 platform in July 2006, the SUSE Linux
Enterprise 10 platform was the basis for both the server and desktop with an almost identical
code base.

Dept. Of Computer Applications ,SNGCE


12

Linux &shell programming

Debian

Debian (/ˈdɛbiən/) is an operating system which is composed primarily of free software, most
of which is under the GNU General Public License, and developed by a group of volunteers
which relies on the Internet to collaborate. This collective group of individuals is known as the
Debian project. Debian is one of the most popular Linux distributions for personal computers
and network servers, and has been used as a base for several other Linux distributions.

Debian 6.0 introduced support for a new kernel aside from the Linux kernel: the kFreeBSD
kernel. This project's new kernel is released as a technology preview and still lacks the amount
of software available in Debian's Linux distribution. GNU/kFreeBSD is offered for Intel/AMD 32-
bit and 64-bit architecture machines. Debian now supports two kernels, Linux and kFreeBSD,
and offers GNU Hurd unofficially.

Dept. Of Computer Applications ,SNGCE


13

Linux &shell programming

3. BASIC COMMANDS IN LINUX


1. ls

List information about the FILEs (the current directory by default).

Syntax

ls [OPTION]... [FILE]...

Option Description

-l use a long listing format

Example:
$ ls -l
Output:
total 8
drwxrwxr-x 2 it it 80 Aug 20 12:13 linux
-rw-rw-r-- 1 it it 9 Aug 20 12:10 LinuxLabCycle.docx
drwxrwxr-x 2 it it 40 Aug 20 12:02 networking
drwxrwxr-x 2 it it 40 Aug 20 12:01 php
-rw-rw-r-- 1 it it 10 Aug 20 12:11 PhpLabCycle.docx
-r reverse order while sorting

Example::
$ ls -r
Output::
PhpLabCycle.docx php networking LinuxLabCycle.docx linux

-R list subdirectories recursively

Example:
ls -R
Output:
./linux:
mod1.docx mod2.pdf
./networking:
./php:

-s print the allocated size of each file, in blocks

Dept. Of Computer Applications ,SNGCE


14

Linux &shell programming

Example:
ls -s
Output:
total 8
0 linux 4 LinuxLabCycle.docx 0 networking 0 php 4
PhpLabCycle.docx

-c sort by ctime, newest first

Example:
ls -c
Output:
linux PhpLabCycle.docx LinuxLabCycle.docx networking php

-m fill width with a comma separated list of entries

Example:
ls -m
Output:
linux, LinuxLabCycle.docx, networking, php,
PhpLabCycle.docx

2.grep

grep, egrep, fgrep, rgrep - print lines matching a pattern

Syntax
grep [OPTIONS] PATTERN [FILE...]

Option Description

-v Print the version number of grep to the standard output stream.

Example:
$ grep -v a f1
Output:
ANju
dEEpu
ACHU
AniL

Dept. Of Computer Applications ,SNGCE


15

Linux &shell programming

-c Suppress normalOutput; instead print a count of matching lines for each input file.

Example:
~$ grep -c A f1
Output:
3

-I Ignore case distinctions in both the PATTERN and the inputFiles.

Example:
~$ grep -i e f1
Output:
meera
meera
dEEpu

-o Print only the matched (non-empty) parts of a matching line.

Example:
~$ grep ra -o f1
Output:ra
ra

-n Prefix each line of Output: with the 1-based line number within its input file.

Example:
~$ grep -n a f1
Output:
1:meera
2:Susan
3:meera
6:aCHU

-l Suppress normal Output; instead print the name of each input file from
which Output:would normally have beenprinted.

Dept. Of Computer Applications ,SNGCE


16

Linux &shell programming

Example:
~$ grep -l a f1 f2

Output:
f1
f2
-L Suppress normaloutput; instead print the name of each input file from which no output
would normally have been printed.

Example:
~$ grep -L meera f1 f2
Output:
f2

-w Select only those lines containing matches that form whole words.

Example:
~$ grep -w meera f1 f2
Output:
f1:meera
f1:meera

3.rm

Remove files or directories

Syntax

rm [OPTION]... FILE...

Option Description

-I prompt before every removal.

Example:
~$ rm -i f3
Output:
rm: remove regular file „f3‟? y

Dept. Of Computer Applications ,SNGCE


17

Linux &shell programming

-f ignore nonexistent files and arguments, never prompt.

Example:
~$ rm -f f5

-r remove directories and their contents recursively.

Example:
~$ rm -v -r flag
Output:
removed directory: „flag‟

-v explain what is being done.

Example:
~$ rm -v f2
Output:
removed „f2‟

4. wc

print newline, word, and byte counts for each file.

Syntax

wc [OPTION]... [FILE]...

Option Description

-c print the byte counts.

Example:
~$ wc -c f1
Output:
44 f1
-m print the character counts.

Example:
~$ wc -m f2
Output:
19 f2

Dept. Of Computer Applications ,SNGCE


18

Linux &shell programming

-l print the newline counts.

Example:
~$ wc -l f2
Output:
7 f2

-L print the length of the longest line.

Example:
~$ wc -L f1
Output:
5 f1

-w print the word counts

Example:
~$ wc -w f1
Output:
8 f1

5.pwd

print name of current/working directory.

Syntax

pwd [OPTION]...

Example:
~$ pwd
Output:
/home/chinjunv

Dept. Of Computer Applications ,SNGCE


19

Linux &shell programming

6. cat

Concatenate files and print on the standard output.

Syntax

cat [OPTION]... [FILE]...

Option Description

-A equivalent to -v

Example:
~$ cat -A f2

Output:anu$
govind$
radhu$
jiji$
lolitta$
appu$
-b number nonempty output lines, overrides –n.

Example:
~$ cat -b f2
Output:
1 anu
2 govind
3 radhu
4 jiji
5 lolitta

-s suppress repeated empty output lines.

Example:
~$ cat -s f4
Output:
devu
sain
binu

Dept. Of Computer Applications ,SNGCE


20

Linux &shell programming

-E display $ at end of each line.

Example:
~$ cat -E f1

Output:
meera$
Susan$
meera$
ANju$
dEEpu$
aCHU$
ACHU$
AniL$

-n number all output lines.

Example:
~$ cat -n f2
Output:
anu
govind
radhu
jiji
lolitta

7.date

print or set the system date and time.

Syntax

date [OPTION]... [+FORMAT]

Option Description

-d display time described by STRING, not 'now'.

Example:
~$ date --date='15 days ago'

Dept. Of Computer Applications ,SNGCE


21

Linux &shell programming

Output:
Fri Sep 5

-r display the last modification time of FILE.

Example:
~$ date -r f3
Output:
Sat Sep 20 13:12:50 IST 2014

-u print or set Coordinated Universal Time.

Example:
~$ date –u
Output:
Sat Sep 20 07:54:11 UTC 2014

-R output date and time in RFC 2822 format.

Example:
~$ date -R
Output:
Sat, 20 Sep 2014 13:25:16 +0530

8. mv

move (rename) files

Syntax

mv [OPTION]... [-T] SOURCE DEST

Option Description

-l use a long listing format

Example:
~$ mv f newfile

Dept. Of Computer Applications ,SNGCE


22

Linux &shell programming

-f do not prompt before overwriting

Example:
~$ mv -f newfile

-I prompt before overwrite

Example:
~$ mv -i f newfile
Output:
mv: overwrite „newfile‟? y

-n do not overwrite an existing file

Example:
mv -n newfile file

-v explain what is being done

Example:
~$ mv -v filelinux
Output:
„file‟ -> „linux‟

9. cat

concatenate files and print on the standard output

Syntax

cat [OPTION]... [FILE]...

Example:
~$ cat linux

Dept. Of Computer Applications ,SNGCE


23

Linux &shell programming

Output:

file
directory
bash
ash
zsh

Option Description

-A show all

Example:
~$ cat -A linux

Output:file$
directory$
bash$ ash$

-b number nonempty output lines, overrides –n

Example:
~$ cat -b flower
Output:
1 rose

2 jasmine
3 lotus

4 lilly

-s suppress repeated empty output lines

Example:
~$ cat -b -s flower
Output:
1 rose

2 jasmine

Dept. Of Computer Applications ,SNGCE


24

Linux &shell programming

3 lotus

4 lilly

-n number all output lines

Example:
~$ cat -n flower
Output:
1 rose
2 jasmine
3 lotus
4 lilly

-E display $ at end of each line

Example:
$ cat -E flower
Output:rose$
$
$
$
jasmine$
lotus$
$
$
lilly$
$

10. head

output the first part of files

Syntax

head [OPTION]... [FILE]...

Dept. Of Computer Applications ,SNGCE


25

Linux &shell programming

Option Description

-c print the first K bytes of each file

Example:
~$ head -c 10 linux
Output:
File

-n print the first K lines instead of the first 10

Example:
~$ head -2 linux
Output:file
directory

-q never print headers giving file names

Example:
~$ head -q linux

Output:file
directory
bash
ash

11. tail

output the last part of files

Syntax

tail [OPTION]... [FILE]...

Option Description

-c output the last K bytes

Dept. Of Computer Applications ,SNGCE


26

Linux &shell programming

Example:
~$ cat linux
Output:

file
directory
bash
ash

Example:
~$ tail -c 3 linux
Output:
sh

-f outputappended data as the file grows

Example:
~$ tail –f linux

Output:file
directory
bash
ash
new data

-n output the last K lines

Example:
~$ tail -3 linux
Output:directory
bash
ash

12. paste

merge lines of files

Dept. Of Computer Applications ,SNGCE


27

Linux &shell programming

Syntax

paste [OPTION]... [FILE]...

Option Description

-d reuse characters from LIST instead of TABs

Example:
linux
Output: new
new
exam
exam

-s paste one file at a time instead of in parallel

Example:
~$ paste -s linux
Output:
file directory bash ash

13. file

determine file type

Syntax

file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-


type][-e testname] [-F separator] [-f namefile] [-m magicfiles]
file ...

Option Description

-b do not prepend filenames to output lines (brief mode)

Dept. Of Computer Applications ,SNGCE


28

Linux &shell programming

Example:
~$ file -b flower
Output:
ASCII text

-c cause a checking printout of the parsed form of the magic file

Example:
~$ file -c flower
Output:
cont offset type opcode mask value desc

-e exclude the test named in testname from the list of tests made to determine the
file type

Example:
~$ file -e ascii flower
Output:
flower: data

-F Use the specified string as the separator between the filename and the file
result returned. Defaults to ‘:’

Example:
~$ file -F @ flower
Output:
flower@ ASCII text

14. chmod

change file mode bits

Syntax

chmod [OPTION]... MODE[,MODE]... FILE...


chmod [OPTION]... OCTAL-MODE FILE ...
chmod [OPTION]... --reference=RFILE FILE...

Dept. Of Computer Applications ,SNGCE


29

Linux &shell programming

Option Description

-v --verbose output a diagnostic for every file processed

Example:
chmod o-rf1
Output:
-rwxrw---- 1 lb233 lb233 10 Jan 8 16:10 f1

15.cp

copy files and directories

Syntax

cp [OPTION]... [-T] SOURCE DEST


cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Example:
cat>>file1
12
13

cat>>file2
flag color

Example:
cp file1 file2
cat file2
Output:
12
13

16.tee

read from standard input and write to standard Output: and files

Dept. Of Computer Applications ,SNGCE


30

Linux &shell programming

Syntax

tee [OPTION]... [FILE]...

Option Description

-a --append append to the given FILEs, do not overwrite

Example:
tee -a f1
apple
orange

Output:
1
2
3
4
5
apple
orange

17.tr
translate or delete characters

Syntax
tr [OPTION]... SET1 [SET2]

Example:
tr "[a-z]" "[A-Z]" <f2
Output:
A
B
C
D
E

Dept. Of Computer Applications ,SNGCE


31

Linux &shell programming

18.echo
display a line of text

Syntax
echo [SHORT-OPTION]... [STRING]...

echo LONG-OPTION

Example:
echo sree narayana gurukulam
Output:
sree narayana gurukulam

19.sort
sort lines of text files

Syntax
sort [OPTION]... [FILE]...

Option Description

-n --numeric -sort compare according to string numerical value

Example:
cat>>f5
Output:
debian
suse
fedora
mandriva
redhat
Example:
sort -n f5

Output:debian
fedora
mandriva
redhat
suse

Dept. Of Computer Applications ,SNGCE


32

Linux &shell programming

20. find
find - search for files in a directory hierarchy

Syntax

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...]


[expression]

Option Description

-d A synonym for -depth, for compatibility

Example:
find /home/mca/India -type d
Output:
/home/mca/India
/home/mca/India/bharat

Example:
find /home/mca -name f?
Output:
/home/mca/India/f1
/home/mca/India/f2
/home/mca/f4
21. pipe

the standard output of one command can be send on standard input to another command

Example:
cat f2 | tee mca
Output:
Unix
Redhat
Fedora
Open SUSE

Dept. Of Computer Applications ,SNGCE


33

Linux &shell programming

Example:
cat f2 | head -3 | tail -1
Output:
3. Fedora

22.chown

chown - change file owner and group

Syntax
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

Option Description

-v --verbose output a diagnostic for every file processed


-R --recursive operate on files and directories recursively

Example:
sudo chown mca
f2 ls -l
Output:
-rw-rw-r-- 1 mca mca 41 Jan 8 20:39 f2

Dept. Of Computer Applications ,SNGCE


34

Linux &shell programming

4.SHELL

PROGRAMMING

PROGRAM NO:1

DATE: 14-07-2015

AIM

The length & breadth of a rectangle & radius of a circle are input through the keyboard.
Write a shell program to calculate the area & perimeter of rectangle, and the area &
circumference of the circle.

PROGRAM

echo "Enter length and breadth of


rectangle" read l
read b
echo "Enter radius of
circle" read r

echo "area of
rectangle=" area_=`echo
"$l*$b"|bc` echo $area_

echo "Perimeter of
rectangle=" s=`echo "$l +
$b"|bc` pr=`echo "$s * 2"|bc`
echo $pr

echo "area of circle="


area_c=`echo "3.14 * $r *
$r"|bc` echo $area_c

echo "Circumference of
circle" cir=`echo "2 * 3.14 *
$r "|bc` echo $cir

OUTPUT

lb245@lb245:~/reenu$ bash

Dept. Of Computer Applications ,SNGCE


35

Linux &shell programming

area.sh Enter length and breadth


of rectangle 5 6

Enter radius of
circle 34
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


36

Linux &shell programming

Linux & Shell Programming


35
area of
rectangle= 30
Perimeter of
rectangle= 22
area_ea of circle=
3629.84
Circumference of
circle 213.52

PROGRAM NO:2

DATE: 21-07-2015

AIM

7. Write a shell program to read marks scored in 3 subjects (out of 100). Calculate

the percentage of marks & display the grade based on the following conditions.

% Grade
80-100 A
70-79 B
60-69 C
<=59 D(Failed)

PROGRAM

echo " GRADE OF STUDENTS


" echo "enter 3 marks"
read m1
read m2
read m3
tot=`echo "$m1+$m2+$m3"|bc`
echo "TOTAL MARK=" $tot
percent=`echo "($tot*100)/300"|bc`
echo "PERCENTAGE=" $percent

if [ $per -ge 80 -a $percent -le


100 ] then echo "A grade"
elif [ $percent -ge 70 -a $percent -le
79 ] then echo "B grade"
Dept. Of Computer Applications ,SNGCE
37

Linux &shell programming

elif [ $percent -ge 60 -a $percent -le


69 ] then echo "C grade"
else
echo "D grade-FAILED"
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


38

Linux &shell programming

Linux & Shell Programming


36
fi

OUTPUT

GRADE OF STUDENTS
enter 3 marks
56
78
89
TOTAL MARK= 223
PERCENTAGE= 74
B grade

PROGRAM NO:3

DATE: 31-05-2015

AIM

Write a script that receives year as argument and check whether it is leap year. If no
argument is given assume the current year.

PROGRAM

year=$1

11. if [ $# -eq 0 ]

12. then

13. year=2015

14.

15. fi leap=$(($year

% 4))

l=$(($year%100))

a=$(($year%400))

Dept. Of Computer Applications ,SNGCE


39

Linux &shell programming

if [ $leap -eq 0 -a $l -ne

0 ] then
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


40

Linux &shell programming

Linux & Shell Programming


37

echo "$year is leap

year" elif [ $a -eq 0 ]

then

echo "$year is leap year "

else

echo "$year is not a leap year"

fi

OUTPUT

lb245@lb245:~/reenu$ bash

LeapYear.sh 2015 is not a leap year

lb245@lb245:~/reenu$ bash LeapYear.sh

2012 2012 is leap year

PROGRAM NO:4

DATE:28-07-2015

AIM

Write a shell program to find,


a) Sum of digits of a number

b) Reverse of the number

c) Determine whether the given number is a palindrome

Dept. Of Computer Applications ,SNGCE


41

Linux &shell programming

PROGRAM

echo "ENTER THE NUMBER"

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


42

Linux &shell programming

Linux & Shell Programming


38
read n

echo "NUMBER=$n"

n1=$n

s=0

while [ $n -gt 0

] do

r=`echo "$n%10"|bc`

s=`echo "($s*10)+$r"|bc`

n=`echo "$n/10"|bc`

done

echo "reverse is $s"

if [ $n1 -eq $s ]

then

echo

"palindrome" else

echo "not palindrome"

fi

s1=0

while [ $n1 -gt 0

] do

r1=`echo "$n1%10"|bc`

Dept. Of Computer Applications ,SNGCE


43

Linux &shell programming

s1=`echo "$s1+$r1"|bc`

n1=`echo "$n1/10"|bc`

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


44

Linux &shell programming

Linux & Shell Programming


39
done

echo "sum of digit=$s1"

OUTPUT

lb245@lb245:~/reenu$ bash number.sh

ENTER THE NUMBER

567

NUMBER=567

reverse is 765

not palindrome

sum of digit=18

lb245@lb245:~/reenu$ bash number.sh

ENTER THE NUMBER

121

NUMBER=121

reverse is 121

palindrome

sum of digit=4

PROGRAM NO:5

DATE:01-09-2015

AIM

Write a shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory and reports accordingly. Whenever

Dept. Of Computer Applications ,SNGCE


45

Linux &shell programming

the argument is a file, the number of lines on it is also reported.

PROGRAM

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


46

Linux &shell programming

Linux & Shell Programming


40
if [ $# -eq 0
] then
echo "Invalid
selection" else
file=$1
fi
if [ -d $file ]
then
echo "It is a directory"
elif [ -f $file ]
then
echo "File name is : $file"
echo "No of line in file is :"
echo " `wc -l $file| cut -d "" -f
1`" fi

OUTPUT

lb245@lb245:~/reenu$ bash 4file.sh


4file.sh File name is : 4file.sh
No of line in file is
: 15 4file.sh

PROGRAM NO:6

DATE: 11-08-2015

AIM

Write a shell program to display Fibonacci series using any looping construct.

PROGRAM

echo "Enter the


number" read n
echo "result:"
f1=0
f2=1
for ((i=0 ; i<n;
i++)) do

Dept. Of Computer Applications ,SNGCE


47

Linux &shell programming

echo $f1
fsum=$((f1+f2))
f1=$f2
f2=$fsum
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


48

Linux &shell programming

Linux & Shell Programming


41
done

OUTPUT

lb245@lb245:~/reenu$ bash
fib.sh Enter the number
5
result:
0
1
1
2
3

PROGRAM NO:7

DATE: 18-08-2015

AIM

Write a shell program to find out reverse string of the given string and check the given
string is palindrome or not.

PROGRAM

echo "Enter the


string" read str
s=` echo "$str" |
rev` if [ $str == $s ]
then
echo
"palindrome" else
echo "Not
palindrome" fi

OUTPUT

Dept. Of Computer Applications ,SNGCE


49

Linux &shell programming

lb245@lb245:~/reenu$ bash
palin.sh Enter the string
ini
palindrome
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


50

Linux &shell programming

Linux & Shell Programming


42

PROGRAM NO:8

DATE: 04-09-2015

AIM

Write a shell script to search an element in the list.

PROGRAM

echo "enter the


limit" read n
echo "enter the numbers"
for((i=0;i<n;i++))
do
read a[$i]
done
echo " enter the number to be
searched " read m
for((i=0;i<n;i++))
do
if [ $m -eq
$((a[$i])) then
f=1 fi

done

if [ $f -eq 1
] then
echo "The given element $m is present at
position $i " else
echo "The element $m not present"
fi
OUTPUT

Dept. Of Computer Applications ,SNGCE


51

Linux &shell programming

lb245@lb245:~/reenu$ bash searchElement.sh


Enter the number of elements

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


52

Linux &shell programming

Linux & Shell Programming


43
3
enter the
numbers 45 65
2

enter the number to be


searched 45
The given element 45 is present at position 1.

PROGRAM NO:9

DATE: 18-08-2015

AIM

Write a shell program to sort the given numbers in ascending order using bubble sort

PROGRAM

echo " enter the limit of


array " read n
echo "enter the elements"
for((i=1;i<=n;i++))
do
read a[$i]
done
for((i=1;i<=n;i++))
do
for((j=i;j<=n;j++))
do
if [ ${a[$i]} -gt ${a[$j]} ]
then
temp=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$temp
fi
done
done

Dept. Of Computer Applications ,SNGCE


53

Linux &shell programming

echo "sorted numbers


are" for((i=1;i<=n;i++))
do
echo
${a[$i]} done
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


54

Linux &shell programming

Linux & Shell Programming


44

OUTPUT

lb245@lb245:~/reenu$ bash
BubbleSort.sh enter the limit of array
9
enter the
elements 12 99
63 12 8 1 75 54

63
sorted numbers
are 1 8 12 12 54
63 63 75 99

PROGRAM NO:10

DATE: 04-09-2015

AIM

Dept. Of Computer Applications ,SNGCE


55

Linux &shell programming

Write a shell script to display the digits which are in odd position in a given 5 digit number

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


56

Linux &shell programming

Linux & Shell Programming


45
PROGRAM

echo "Enter the five digit

number" read x

if [ $(($x/10000)) -ge 1 -a $(($x/100000)) -lt

1 ] then

n=1

while [ $n -le 5

] do

a=`echo $x | cut -c

$n` echo $a

n=`expr $n +

2` done

else

echo "Not a five digit

number" fi

OUTPUT

lb245@lb245:~/reenu/cycle4$ bash 4_3.sh

Enter the five digit number

58943

Dept. Of Computer Applications ,SNGCE


57

Linux &shell programming

PROGRAM NO:11

DATE: 30-09-2015
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


58

Linux &shell programming

Linux & Shell Programming


46

AIM

Write a menu driven program to perform the following:

Write script that takes a number as command line argument and prints its table in
below
format: 2 * 1 = 2
2*2=4

2 * 10 = 20

Write a shell script called compare.sh that takes two integers, compares them and
does the following : add them if the first one is smaller than the second, multiply them
if the first one is greater than the second and do nothing if they are equal.

PROGRAM

while [ 1

] do

echo "Enter your choice"

echo "1. Multiplication

table" echo "2. Number

checking" echo "3. Exit"

read c

case $c in

1) echo "enter the value as argumet for multiplication

table" read x

bash mul.sh $x;;

2) bash compare.sh;;

Dept. Of Computer Applications ,SNGCE


59

Linux &shell programming

3) exit;;

esac

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


60

Linux &shell programming

Linux & Shell Programming


47
done

echo "enter two

numbers" read x

read y

if [ $x -gt $y

] then

r=$((x*y))

echo "$x * $y =

$r" elif [ $x -lt $y ]

then

a=$((x+y))

echo "$x + $y =

$a" else

echo "arguments are

same" fi

if [ $# -eq 0

] then

echo "enter

argument" exit;

else

Dept. Of Computer Applications ,SNGCE


61

Linux &shell programming

n=$1;

fi

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


62

Linux &shell programming

Linux & Shell Programming


48
for ((i=1;i<=10;i++))

do

p=$((n*i));

echo "$n * $i =

$p" done

compare.sh

echo "enter two

numbers" read x

read y

if [ $x -gt $y

] then

r=$((x*y))

echo "$x * $y =

$r" elif [ $x -lt $y ]

then

a=$((x+y))

echo "$x + $y =

$a" else

echo "arguments are

same" fi

mul.sh

Dept. Of Computer Applications ,SNGCE


63

Linux &shell programming

if [ $# -eq 0 ]

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


64

Linux &shell programming

Linux & Shell Programming


49
then

echo "enter

argument" exit;

else

n=$1;

fi

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

do

p=$((n*i));

echo "$n * $i =

$p" done

OUTPUT

lb245@lb245:~/reenu/cycle4$ bash

4_4.sh Enter your choice

1. Multiplication table

2. Number checking

3. Exit

enter the value as argumet for multiplication

table 5 5 * 1 = 5

5 * 2 = 10
5 * 3 = 15
Dept. Of Computer Applications ,SNGCE
65

Linux &shell programming

5 * 4 = 20

5 * 5 = 25
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


66

Linux &shell programming

Linux & Shell Programming


50
5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50 Enter

your choice

1. Multiplication table

2. Number checking

3. Exit

enter two

numbers 9 8

9 * 8 = 72

Enter your choice

1. Multiplication table

2. Number checking

3. Exit

enter two

numbers 1 9

1 + 9 = 10

Dept. Of Computer Applications ,SNGCE


67

Linux &shell programming

Enter your choice

1. Multiplication table

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


68

Linux &shell programming

Linux & Shell Programming


51
2. Number checking

3. Exit

PROGRAM NO:12

DATE: 30-09-2015

AIM

Write a shell script to sum up the following series


1/1! + 2/2! + 3/3! + ….

PROGRAM

echo "enter the

limit" read n

s=0

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

do

f=1

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

do

f=$((f*j))

done

s=`expr "scale=2;$s + $i / $f " |bc

-l` done

Dept. Of Computer Applications ,SNGCE


69

Linux &shell programming

echo "sum is $s"

OUTPUT

lb245@lb245:~/reenu/cycle4$ bash 4_5.sh


Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


70

Linux &shell programming

Linux & Shell Programming


52
4_5.sh: line 1: 1/1!: No such file or

directory enter the limit

sum is 2.70

PROGRAM NO:13

DATE: 30-09-2015

AIM

Write an alternative script for the command tail called TAIL which works exactly
like tail command with options. (Do not use tail command in the script).

PROGRAM

if [ $# -ne 0

] then

if [ -e $1

] then

c=` wc -l $1 | cut -d " " -f

1 ` if [ $# -eq 2 ]

then

d=$(($c-$2))

d=$(($d+1))

sed -n "$d,$ p"

$1 elif [ $# -eq 1 ]

then
Dept. Of Computer Applications ,SNGCE
71

Linux &shell programming

s=$(($c-10))

s=$(($s+1))

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


72

Linux &shell programming

Linux & Shell Programming


53
sed -n "$s,$ p" $1

fi

else

echo "File does not

exist" fi

else

echo "No arguments entered"

fi

OUTPUT

f2 .txt

**************

reenu COLORS

yellow

blue

green

reenu

sky

is

blue

Dept. Of Computer Applications ,SNGCE


73

Linux &shell programming

roses

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


74

Linux &shell programming

Linux & Shell Programming


54
red

violet

green

orange

silver

lb245@lb245:~/reenu/cycle4$ bash

4_6.sh f2 6 r

red

violet

green

orange

silver

PROGRAM NO:14

DATE: 30-09-2015

AIM

Write an alternative script for the command head called HEAD which works exactly
like head command with options. (Do not use head command in the script).

PROGRAM

if [ $# -ne 0

] then

Dept. Of Computer Applications ,SNGCE


75

Linux &shell programming

if [ -e $1

] then

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


76

Linux &shell programming

Linux & Shell Programming


55
c=` wc -l $1 | cut -d " " -f

1 ` if [ $# -eq 2 ]

then

sed -n "1,$2 p"

$1 elif [ $# -eq 1 ]

then

sed -n "1,10 p" $1

fi

else

echo "File does not

exist" fi

else

echo "No arguments entered"

fi

OUTPUT

f2 .txt

**************

reenu COLORS

yellow

blue

green

Dept. Of Computer Applications ,SNGCE


77

Linux &shell programming

reenu

sky

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


78

Linux &shell programming

Linux & Shell Programming


56
is

blue

roses

r
red

violet

green

orange

silve

lb245@lb245:~/reenu/cycle4$ bash 4_7.sh f2 6

**************

reenu COLORS

yellow

blue

green

reenu

PROGRAM NO:15

DATE: 30-09-2015

AIM

Write a shell program to generate all combinations of 1, 2, 3 using for loop.

PROGRAM

Dept. Of Computer Applications ,SNGCE


79

Linux &shell programming

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

do

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


80

Linux &shell programming

Linux & Shell Programming


57
for

((j=1;j<=3;j++)) do

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

do

if [ $i -ne $j -a $i -ne $k -a $j -ne

$k ] then

echo $i $j

$k fi
done

done

done

OUTPUT

lb245@lb245:~/reenu/cycle4$ bash

4_8.sh f2 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3

21

PROGRAM NO:16

DATE: 30-09-2015

Dept. Of Computer Applications ,SNGCE


81

Linux &shell programming

AIM

The word Unix is present in only some of the files supplied as arguments to the shell script.
Your shell script should search each of these files in turn & stop at the first file that
encounters containing the word Unix. This filename should be displayed on the screen.
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


82

Linux &shell programming

Linux & Shell Programming


58

PROGRAM

x=$*

for i in $x

do

grep -lq "unix"

$i if [ $? -eq 0 ]

then

echo $i

break

fi

done

OUTPUT

f2.txt

**************

reenu COLORS

yellow

blue

green

reenu

sky

is

Dept. Of Computer Applications ,SNGCE


83

Linux &shell programming

blue

roses

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


84

Linux &shell programming

Linux & Shell Programming


59
r

red

violet

green

orange

silver

new.txt

COLORS

yellow

blue

lb245@lb245:~/reenu/cycle4$ bash 4_8.sh f2 new f3

f3

PROGRAM NO:17

DATE: 30-09-2015

AIM

Write a shell script to print the following


patterns 1

12

123

Dept. Of Computer Applications ,SNGCE


85

Linux &shell programming

12 3 4

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


86

Linux &shell programming

Linux & Shell Programming


60

PROGRAM

echo "enter the

limit" read n

for ((i=1 ;i<=n

;i++)) do

for ((j=1 ;j<=i

;j++)) do

echo -n " " $j

done

echo " "

done

OUTPUT

lb245@lb245:~/reenu/cycle4$ bash 4_10.sh

enter the limit

12

123

1234

12345

Dept. Of Computer Applications ,SNGCE


87

Linux &shell programming

PROGRAM NO:18
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


88

Linux &shell programming

Linux & Shell Programming


61

DATE: 30-09-2015

AIM

Write a menu driven program to convert decimal number to binary,octal &


hexadecimal equivalent.

PROGRAM

while [ 1 ]

do

echo "enter your

option" echo "1. Binary"

echo "2. Octal"

echo "3. Hexadecimal"

echo "4. exit"

read c

case $c in

1)echo "enter the decimal

number" read a

b=

n=$a

while [ $a -ne 0

] do

d=$(expr $a %

2) b=$d$b

Dept. Of Computer Applications ,SNGCE


89

Linux &shell programming

a=$(expr $a / 2)

done

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


90

Linux &shell programming

Linux & Shell Programming


62
echo $b;;

2)echo "enter the decimal

number" read a

b=

n=$a

while [ $a -ne 0

] do

d=$(expr $a %

8) b=$d$b

a=$(expr $a / 8)

done

echo $b;;

3)echo "enter the decimal

number" read a

b=

n=$a

while [ $a -ne 0

] do

d=$(expr $a % 16)

if [ $d == 10 ]

then

Dept. Of Computer Applications ,SNGCE


91

Linux &shell programming

d=A

elif [ $d == 11 ]

then
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


92

Linux &shell programming

Linux & Shell Programming


63
d=B

elif [ $d == 12 ]

then

d=C

elif [ $d == 13 ]

then

d=D

elif [ $d == 14 ]

then

d=E

elif [ $d == 15 ]

then

d=F

fi

b=$d$b

a=$(expr $a / 16)

done

echo $b;;

4)exit;;

esac

done

OUTPUT

lb245@lb245:~/reenu/cycle5$ bash

Dept. Of Computer Applications ,SNGCE


93

Linux &shell programming

5_1.sh enter your option

1. Binary

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


94

Linux &shell programming

Linux & Shell Programming


64
2. Octal

3. Hexadecimal

4. exit

enter the decimal

number 45 101101

enter your option

1. Binary

2. Octal

3. Hexadecimal

4. exit

enter the decimal

number 45 55

enter your option

1. Binary

2. Octal

3. Hexadecimal

4. exit

enter the decimal

Dept. Of Computer Applications ,SNGCE


95

Linux &shell programming

number 45

2D

enter your option


Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


96

Linux &shell programming

Linux & Shell Programming


65
1. Binary

2. Octal

3. Hexadecimal

4. exit

PROGRAM NO:19

DATE: 30-09-2015

AIM

Write a shell script to perform database operations for student data like view, add
and delete records and view database.

PROGRAM

echo "Enter name of database

" read db

while [ 1 ]

do

echo "1. View "

echo "2. Add Records "

echo "3. Delete Records

" echo "4. Exit "

echo "Enter your choice "

read c

Dept. Of Computer Applications ,SNGCE


97

Linux &shell programming

case $c in

1)cat $db;;

2)echo "Enter new roll no "


Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


98

Linux &shell programming

Linux & Shell Programming


66
read rno

echo "Enter new

name:" read name

echo "Enter branch name

" read branch

echo "Enter college

name" read college

echo "$rno $name $branch $college">>$db;;

3)echo "Enter Id"

read id

grep -v "$id" $db >dbs1

echo "Record is deleted"

cat dbs1;;

4)exit;;

*)echo "Invalid choice

";; esac

done

OUTPUT

lb245@lb245:~/reenu/cycle5$ bash

5_2.sh Enter name of database

document

1. View

2. Add Records

Dept. Of Computer Applications ,SNGCE


99

Linux &shell programming

3. Delete Records

4. Exit

Enter your choice


Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


100

Linux &shell programming

Linux & Shell Programming


67
2

Enter new roll no

45

Enter new name:

reenu

Enter branch name

mca-le

Enter college

name sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Enter your

choice 1

45 reenu mca-le sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Enter your

choice 1

45 reenu mca-le sngce

Dept. Of Computer Applications ,SNGCE


101

Linux &shell programming

1. View

2. Add Records

3. Delete Records
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


102

Linux &shell programming

Linux & Shell Programming


68
4. Exit

Enter your

choice 2

Enter new roll no

Enter new name:

jo

Enter branch name

cs

Enter college

name sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Enter your

choice 1

45 reenu mca-le sngce

2 jo cs sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Dept. Of Computer Applications ,SNGCE


103

Linux &shell programming

Enter your

choice 3

Enter Id
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


104

Linux &shell programming

Linux & Shell Programming


69
45

Record is deleted

2 jo cs sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Enter your

choice 1

45 reenu mca-le sngce

2 jo cs sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Enter your

choice 1

45 reenu mca-le sngce

2 jo cs sngce

1. View

2. Add Records

3. Delete Records

4. Exit

Dept. Of Computer Applications ,SNGCE


105

Linux &shell programming

Enter your choice

PROGRAM NO:1
Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


106

Linux &shell programming

Linux & Shell Programming


70

DATE: 30-09-2015

AIM

Write a script to read the basic salary of n employees and calculate the gross
salary If BP<15000, DA=30% of BP, HRA =Rs 500. TA = 10% of BP.
If BP >=15000, DA=50% of BP, HRA=15%, TA=1000.

PROGRAM

echo "Enter the

limit" read n

echo "Enter the basic salary of $n

emp" for((i=0;i<n;i++))

do read

sal[i]=$s

done

for((i=0;i<n;i++))

do

bs=${sal[i]}

echo

if [ $bs -lt 15000 ]

then

Dept. Of Computer Applications ,SNGCE


107

Linux &shell programming

da=$((bs*30/100))

ta=$((bs*10/100))

hra=500

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


108

Linux &shell programming

Linux & Shell Programming


71
gs=$((bs+da+ta+hra))

else

da=$((bs*50/100))

hra=$((bs*15/100))

ta=1000

gs=$((bs+da+ta+hra))

fi

echo "Gross salary of $((i+1)) th emp is $gs"

done

OUTPUT

lb245@lb245:~/reenu/cycle5$ bash

5_6.sh Enter the limit

Enter the basic salary of 3

emp 35670 1200 3900

Gross salary of 1 th emp is 59855

Gross salary of 2 th emp is 2180

Gross salary of 3 th emp is 5960

Dept. Of Computer Applications ,SNGCE


109

Linux &shell programming

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE


110

Linux &shell programming

Linux & Shell Programming


72

Dept. Of Computer Applications ,SNGCE


111

Linux &shell programming

Dept. Of Computer Applications ,SNGCE

Dept. Of Computer Applications ,SNGCE

You might also like