Linux and Shell Programming Lab-1
Linux and Shell Programming Lab-1
21 27/7/2020 Shell script to sum up the following series 1/1! + 2/2! + 3/3! 59
1. COMPUTER ASSEMBLING
Plug or connect all hardware devices in Mother board (mother PCB (Printed Circuit
Board)) properly.
Processor, Hard drive, Floppy Drive, CD Drive, RAM, Display card, Sound card,
Network card etc….
Trouble shoots the system for its proper working.
/dev/xxyN
/dev/ - name of the directory in which all device files are stored
xx - type of device on which partition is created. hd( for IDE) or sd (for SCSI disk)
1
y - The device on which the partition is present.
/dev/hda indicates first hard disk. So it is first ide. If it is b, c means second and
third respectively.
N denotes partition number. Here 1-4 for primary and logical drives starts from 5.
Dev/hda5-first logical drive of first ide hard disk
Dev/sdb1-first primary partition of second scsi hard disk
Dev/hdc2-second primary partition of third id
4. Linux installation
/boot partition. It contains operating system kernel. It is always be the double the size of
RAM.
Root partition: Partition which root directory (/) is located. All the files of the system
except boot files are stored.
Installation methods
Local CD-ROM
NFS Image
FTP and HTTP
Hard Drive
Installation Classes
Workstation
Server
Laptop
Custom
RESULT
Linux installed successfully.
2
Program no: 2
Date: 22/6/2020
AIM: Demonstrate a College Management system in Linux using File and Directory commands.
3
$ cd..
# Make the second subdirectory inside college
$mkdir /tmp/college/students
# Inside the student directory make the subdirectory departments
$mkdir /tmp/college/students/departments
# Inside the department directory there is a file for managing the students details.
$ vi studentnames.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the third subdirectory inside college
$mkdir /tmp/college/course
# Inside the course directory make the subdirectory coursetype
$mkdir /tmp/college/course/coursetype
# Inside the coursetype directory there is a file for managing the duration of courses.
$ vi courseduration.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the fourth subdirectory inside college
$mkdir /tmp/college/lab
# Inside the lab directory make the subdirectory labtype
$mkdir /tmp/college/lab/labtype
# Inside the labtype directory there is a file for managing the duration of labs.
$ vi labduration.sh
# Back to the parent directory college
$ cd..
$ cd..
# Make the fifth subdirectory inside college
$mkdir /tmp/college/library
# Inside the lab directory make the subdirectory departments
$mkdir /tmp/college/library/departments
# Inside the departments directory there is a file for managing the books in each department.
$ vi books.sh
# Back to the parent directory
$ cd..
$ cd..
# Make the sixth subdirectory inside college
$mkdir /tmp/college/clubs
# Inside the clubs directory make the subdirectory artsclub
$mkdir /tmp/college/clubs/artsclub
# Inside the artsclub directory there is a file for managing the functions and activities in the arts
club.
$ vi functions.sh
# Back to the clubs directory
$ cd..
4
# Inside the clubs directory make the subdirectory sportsclub
$mkdir /tmp/college/clubs/sportsclub
# Inside the sportsclub directory there is a file for managing the functions and activities in the
sports club.
$ vi functions.sh
# Back to the club directory
$ cd..
# Inside the clubs directory make the subdirectory placementcell
$mkdir /tmp/college/clubs/placementcell
# Inside the placementcell directory there is a file for managing the functions and activities in the
placement cell.
$ vi functions.sh
# Back to the club directory
$ cd..
# Inside the clubs directory make the subdirectory iedc
$mkdir /tmp/college/clubs/iedc
# Inside the iedc directory there is a file for managing the functions and activities in the iedc.
$ vi functions.sh
# Back to the parent directory college
$ cd..
RESULT
5
Program no: 3
Date: 25/6/2020
Visual Editor
AIM: Familiarize and execute the commands of vi Editor.
vi /path/to/filename
Opening an already existing file
vi /path/to/filename
The Vi Modes
2. Move right: l
3. Move up: k
4. Move down: j
Common vi Commands
Editing a File
1. i : to insert text before the current cursor location
2. I: to insert text at the beginning of the line
3. a: to insert text after the current cursor location
4. o: to create a new line for text below the current cursor location
Deleting characters
1. x : delete the character under the current location
2. X : delete the character before the current location
3. dw : delete from the current location to the next word
4. D : delete from current location till end of the line
5. dd : delete the entire line
6
Copying and pasting
Changing text
RESULT
vi editor commands executed successfully.
7
Program no: 4
Date: 25/6/2020
Filter commands
Aim: Practice the Filter commands in Linux with suitable examples.
3) Create a file
$ cat>newfile
4) Copy the contents of one file to another file.
$ cat[filename-whose-contents-is-to-be-copied]>[destination-filename]
$ tac filename
$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
8
Jharkhand
Karnataka
Kerala
Madhya Pradesh
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Example:
$ head state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
3. tail command
Print the last N number of data of the given input.
tail [OPTION]... [FILE]...
$ cat state.txt
Kerala
Maharashtra
Manipur
Meghalaya
Mizoram
Nagaland
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
$ tail state.txt
9
Odisha
Punjab
Rajasthan
Sikkim
Tamil Nadu
Telangana
Tripura
Uttar Pradesh
Uttarakhand
West Bengal
4. The wc command
$ wc state.txt
5 7 63 state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
$ cut state.txt
10
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
6. The paste command
It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from
each file specified, separated by tab as delimiter, to the standard output.
paste [OPTION]... [FILES]...
$ cat state
Arunachal Pradesh
Assam
Andhra Pradesh
Bihar
Chhattisgrah
$ cat capital
Itanagar
Dispur
Hyderabad
Patna
Raipur
$ paste number state capital
1 Arunachal Pradesh Itanagar
2 Assam Dispur
3 Andhra Pradesh Hyderabad
4 Bihar Patna
5 Chhattisgrah Raipur
7. The pr command
The pr command formats a file to make it look better when printed.
pr [OPTION]...[FILE]...
11
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
9. The tr command
The tr command in LINUX is a command line utility for translating or deleting characters
$ tr [OPTION] SET1 [SET2]
$ cat greekfile
Output:
WELCOME TO
GeeksforGeeks
$ cat greekfile | tr “[a-z]” “[A-Z]”
Output:
WELCOME TO
GEEKSFORGEEKS
10. The Sort command
SORT command is used to sort a file.
Examples
$ cat > file.txt
abhishek
chitransh
satish
rajan
naveen
divyam
harsh
$ sort filename.txt
Command:
$ sort file.txt
abhishek
chitransh
divyam
harsh
naveen
12
rajan
satish
uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate
lines.
$uniq [OPTION] [INPUT[OUTPUT]]
$cat kt.txt
I love music.
I love music.
I love music.
I love music of Kartik.
I love music of Kartik.
$uniq kt.txt
I love music.
I love music of Kartik.
RESULT
Filter commands executed successfully
13
Program NO: 5
Date: 29/6/2020
Redirection
The process of changing the assignment for the standard input (keyboard), the standard output
(monitor) and the standard error file (monitor) is known as redirection.
Input redirection
Example:
Output Redirection
$ ls -l > listings
Example 1:
Output is “Very good morning”. Here the content of test1 will be overwritten to test2.
Error Redirection
Syntax:
Example
Cat Datafile 2> temp
Piping
It is used in Linux operating systems to send the output of one command/program/process to
another command/program/process for further processing.
14
Example:
1. Listing all files and directories and give it as input to more command.
$ ls -l | more
2. Use sort and uniq command to sort a file and print unique values.
$ sort record.txt | uniq
This will sort the given file and print the unique values only.
RESULT
Redirection and pipe commands executed successfully.
15
Program No: 6
Date: 2/7/2020
Listing Files
$ls
Creating Files
$ vi filename
$ vi filename
h key to move to the left side.
k key to move upside in the file.
j key to move downside in the file.
$ cat filename
$ wc filename
2 19 103 filename
$
Copying Files
$ cp source_file destination_file
$ cp filename copyfile
16
$
Renaming Files
$ mv old_file new_file
$ mv filename newfile
$
Deleting Files
$ rm filename
Directory management
$cd ~
$
$cd ~username
$
To go in your last directory, you can use the following command −
$cd -
$
Following are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
command pwd to print the current working directory −
$pwd
/user0/home/amrood
$
Listing Directories
$ls dirname
$ls /usr/local
X11 bin gimp jikes sbin
ace doc include lib share
atalk etc info man ami
Creating Directories
$mkdir dirname
Eg. $mkdir mydir
$
$mkdir /tmp/test-dir
$
17
Removing Directories
$rmdir dirname
$
$rmdir dirname1 dirname2 dirname3
$
Changing Directories
$cd dirname
$
$cd /usr/local/bin
$
Renaming Directories
$mv olddir newdir
$
RESULT
18
Program no: 7
Date: 2/7/2020
1. fdisk
Fdisk is the most commonly used command to check the partitions on a disk
2. sfdisk
19
3. cfdisk
It can be used to list out the existing partitions as well as create or modify them.
4. Parted
Parted is yet another command line utility to list out partitions and modify them if
needed.
20
5. df
Df is not a partitioning utility, but prints out details about only mounted file systems.
6. pydf
Improved version of df, written in python. Prints out all the hard disk partitions in a easy to read
manner.
7. lsblk
List all the storage blocks, which include disk partitions and optical drives.
21
8. blkid
Prints the block device (partitions and storage media) attributes like uuid and file system type..
9. hwinfo
The hwinfo is a general purpose hardware information tool and can be used to print out the disk
and partition list.
22
10. Inxi
Inxi is a very useful command line program that can display information
RESULT
23
Program no: 8
Date: 6/7/2020
SSH
Eg. ls
Ping
This utility is commonly used to check whether your connection to the server is healthy or not.
FTP
FTP is file transfer protocol. It's the most preferred protocol for data transfer amongst
Telnet
Syntax
Example:
telnet localhost
RESULT
25
Program no: 9
Date: 6/7/2020
Algorithm
Step1: Start
Step2: Input two numbers (a, b)
Step3: Sum= (a + b)
Step4: Difference= (a - b)
Step5: Quotient = (a * b)
Step6: Remainder= (a % b)
Step7: Display Sum, Difference, Quotient, Remainder
Step8: Stop
Shell Script
#!/bin/bash
echo "Enter two numbers"
read a
read b
sum=$(echo "$a + $b" | bc -l)
difference=$(echo "$a - $b" | bc -l)
quotient=$(echo "$a * $b" | bc -l)
reminder=$(echo "$a / $b" | bc)
echo "The sum of $a and $b is : $sum"
26
RESULT
Script executed successfully.
27
OUTPUT
28
Program no: 10
Date: 9/7/2020
Algorithm
Step 1: start
Step 2: Input length and breadth (a, b)
Step 3: Area= a + b;
Step 4: Perimeter= 2*(a + b)
Shell Script
#!/bin/bash
#For Rectangle
echo -n "Enter the Length of a Rectangle:"
read l
echo -n "Enter the Breadth of a Rectangle:"
read b
# For Circle
echo -n "Enter the Radius of a Circle:"
read r
echo -------------------------------------------------
echo "Area of Rectangle : $(($l*$b))"
echo "Perimeter of Rectangle : $((2*$(($l+$b))))"
pi=3.14
area=$(echo "$pi*$r*$r" | bc -l)
circumference=$(echo "$pi*2*$r" | bc -l)
echo "area of the circle is : $area"
echo "circumference of the circle is: $circumference"
echo ------------------------
29
RESULT
30
OUTPUT
31
Program no: 11
Date: 13/7/2020
SHELL PROGRAMS
AIM: - Write a shell program to find
a) Sum of digits of a number
b) Reverse of the number
Algorithm
Step 1: start
Step 2: Input a number (num)
Step 3: Assign num to variable g
Step 4: Assign s=0
#!/bin/bash
echo "Enter a number "
read Num
g=$Num
s=0
while [ $Num -gt 0 ]
do
k=$(( $Num % 10 ))
Num=$(( $Num / 10 ))
32
s=$(( $s + $k ))
done
echo "sum of digits of $g is : $s"
RESULT
Shell script executed successfully.
33
OUTPUT
34
Algorithm
Step 1: start
Step 8: Stop
RESULT
Shell script executed successfully.
35
OUTPUT
36
Algorithm
Step 1: start
Step 8: Stop
37
fi
RESULT
Shell script executed successfully.
38
OUTPUT
39
Program no: 12
Date: 13/7/2020
ODD POSITION
AIM: - Write a shell script to display the digits which are in odd position in a given integer.
Algorithm
Step 1: Start
Step 2: Input a number(num)
Step 3: Assign n=1
Step 4: Repeat step 5 until $n<10
Step 5: a=`echo $num | cut -c $n`, echo $a(prints the value of a), n=`expr $n + 2`
Step 6: Stop
Shell Script
s#!/bin/bash
echo $a
n=`expr $n + 2`
done
RESULT
Shell script executed successfully.
40
OUTPUT
41
Program no: 13
Date: 13/7/2020
SALARY OF EMPLOYEE
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 >=5000, DA=50% of BP, HRA=15%, TA=1000.
Algorithm
Step 1: Start
Step 2: Read the number employees(n)
Step 3: repeat for( i = 0; i < $n; i++ )
Step 4: Read the basic salary(bsal)
Step 5: check if $bsal<15000, then calculate gross salary and print the value
Step 6: otherwise,check if $basl>5000, then calculate gross salary and print the value
Step 7:Stop
Shell script
#!/bin/bash
echo "enter the number of employees"
read n
for (( i = 0; i < $n; i++ )); do
echo "enter the basic salary:"
read bsal
if [ $bsal -lt 15000 ]
then
gsal=$((($bsal+500)+($bsal/100*30)))
echo "The gross salary : $gsal"
elif [ $bsal -ge 5000 ]
then
gsal=$((($bsal+$bsal/100*15)+($bsal/100*50)))
echo "the gross salary : $gsal"
fi
done
RESULT
Shell script executed successfully.
42
OUTPUT
43
Program no: 14
Date: 16/7//2020
Algorithm
Step 1: Start
Step 2: Read the Cost price (cp)
Step 3: Read the Selling price (sp)
Step 4: check if $sp>$cp, then calculate profit and print the profit value.
Step 5: Otherwise, calculate the loss and print the loss value.
Step 6: Stop
Shell script
#!/bin/bash
RESULT
Shell script executed successfully.
44
OUTPUT
45
Program no: 15
Date: 16/7/2020
STUDENT REPORT
AIM: - Write a script to read 5 marks of n students .Find the total and return distinction if the
total percentage >= 80. [Distinction] if total % is >=60 and <80 [first class].if total % is
>=50 and <60 [second class] else print failed [<50].
Algorithm
Step 1: Start
Step 2: Read the number of students (n)
Step 3: Repeat for(i=0;i<$n;i++)
Step 4: Read the name of the student (name)
Step 5: Read the marks of 5 subjects (m1,m2,m3,m4,m5)
Step 6: Calculate the sum of marks
Step 7: Print the sum of 5 subjects
Step 8: Calculate percentage
Step 9: Print percentage
Step 10: Check if $per>80, print you get distinction
Step 11: Otherwise, check if $per>60 & $per< 80, then print you get first class
Step 12: Otherwise, check if $per>50 & $per<60, then print you get second class
Step 13: Else print you get fail
Step 14: Stop
Shell script
#!/bin/bash
echo "Enter the number of students:"
read n
for (( i = 0; i < $n; i++ )); do
echo "Enter the name of the student:"
read name
echo "Enter the mark1:"
read m1
echo "Enter the mark2:"
read m2
echo "Enter the mark3:"
read m3
echo "Enter the mark4:"
46
read m4
echo "Enter the mark5:"
read m5
sum1=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
echo "Sum of 5 subjects are: " $sum1
per=$(expr $sum1 / 5 )
echo " Percentage: " $per
if [ $per -ge 80 ]
then
echo "You get Distinction"
elif [ $per -ge 60 -a $per -lt 80 ]
then
echo "You get First class"
elif [ $per -ge 50 -a $per -lt 60 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
done
RESULT
Shell script executed successfully.
47
OUTPUT
48
Program no: 16
Date: 16/7//2020
READ A CHARACTER
AIM: - Write a script to read a character and to display if it is lowercase, uppercase, digit or
special character or not a character.
Algorithm
Step 1: Start
Step 2: Read a character (c)
Step 3: Check if $c is in the range [a-z], then print lowercase.
Step 4: Otherwise, check $c in range [A-Z], then print uppercase.
Step 5: Otherwise, check $c in range [0-9], then print digit.
Step 6: Otherwise, check $c is a special character, then print special character.
Step 7: Otherwise, print not a character.
Step 6:Stop
Shell script
#!/bin/bash
echo "Type a character"
read c
if [[ "$c" =~ [a-z] ]]; then
echo "lowercase"
elif [[ "$c" =~ [A-Z] ]]; then
echo "uppercase"
elif [[ "$c" =~ [0-9] ]]; then
echo "Digit"
elif [[ "$c" =~ ['!'@#\$%^\&*()_+] ]]; then
echo "Special character"
else
echo "Not a character"
fi
RESULT
Shell script executed successfully.
49
OUTPUT
50
Program no: 17
Date:23/7 /2020
MULTIPLICATION TABLE
AIM: - Write a script to prepare multiplication table of a given number to any order.
Algorithm
Step 1: Start
Step 2: Read the number (n)
Step 3: Assign i=1
Step 4: Repeat step 5 until $i<10
Step 5: Print, $n X $i = `expr $n \* $i and increment i by 1
Step 6:Stop
Shell script
#!/bin/bash
echo "Enter a Number"
read n
i=1
while [ $i -le 10 ]
do
echo " $n X $i = `expr $n \* $i`"
i=$(expr $i + 1)
done
RESULT
Shell script executed successfully.
51
OUTPUT
52
Program no: 18
Date:23 /2020
POWER OF A NUMBER
AIM: - Write a script to find the value of one number raised to the power.
Algorithm
Step 1: Start
Step 2: Read a number (n)
Step 3: Read the power (power)
Step 4: Assign counter=0 and ans=1.
Step 5: Repeat step 6 until $power!=$counter.
Step 6: ans=$(expr $ans \* $n), counter=$(expr $counter + 1)
Step 8: Prints the power of the number ($ans)
Step 7:Stop
Shell script
#!/bin/bash
echo "Enter a number"
read n
echo "Enter the power"
read power
counter=0
ans=1
while [ $power -ne $counter ]
do
ans=$(expr $ans \* $n)
counter=$(expr $counter + 1)
done
echo "$n power of $power is $ans"
RESULT
Shell script executed successfully.
53
OUTPUT
54
Program no: 19
Date: 23/7/2020
PRIME NUMBER
AIM: - Write a script to print all prime numbers from 1 to n.
Algorithm
Step 1: Start
Step 2: Read the limit (num)
Step 3: Repeat step 4 & 6, for(i=2;i<=num;i++)
Set f=0
Step 4: Repeat 6 for(j=2;j<i;j++)
Step 5: check if [‘expr $i % $j` -eq 0], then set f=1
Step 6: check if $f=0, then print $i.
Step 7 : Stop
Shell script
#!/bin/bash
echo "Enter the limit "
read num
echo "Primes of 1 to $num are :"
for((i=2;i<=$num;i++))
do
f=0
for((j=2;j<i;j++))
do
if [ `expr $i % $j` -eq 0 ]
then
f=1
fi
done
if [ $f -eq 0 ]
then
echo " "$i
fi
done
RESULT
Shell script executed successfully.
55
OUTPUT
56
Program no: 20
Date: 23/7/2020
COMBINATION OF a , b and c
AIM: - Write a script to generate all combinations of a, b and c.
Algorithm
Step 1: Start
Step 2: Assign p=a, q=b, s=c
Step 3: Repeat step 4 & 7 , for((i=0;i<3;i++))
Step 4: Repeat step 5 & 6 , for((j=1;j<=2;j++))
Step 5: Print $p,$q,$s
Step 6: Swap the values: t=$q, q=$s, s=$t
Step 7: Swap the values: t=$p, p=$q, q=$s, s=$t
Step 6:Stop
Shell script
#!/bin/bash
p=a
q=b
s=c
for((i=0;i<3;i++))
do
for((j=1;j<=2;j++))
do
echo "$p$q$s"
t=$q
q=$s
s=$t
done
t=$p
p=$q
q=$s
s=$t
done
RESULT
Shell script executed successfully.
57
OUTPUT
58
Program No: 21
Date: 27/7/2020
Sum of series
Aim: Write a shell script to sum up the following series 1/1! + 2/2! + 3/3! + ….
Algorithm
Step 1: Start
Shell Script
#!/bin/bash
for((i=1;i<=$l;i++))
do
for((j=$i;j<=$i;j++))
do
fact=$(($fact * $j))
done
59
done
echo $n
else
echo "It is a non-negative number"
fi
RESULT
60
OUTPUT
61
Program No: 22
Date: 27/7/2020
AIM: Write a script to read a year and to decide whether it is a leap year or not. If no year is
supplied then the current year is assumed.
Algorithm
Step 1: Start
Step 6: Stop
Shell Script
#!/bin/bash
echo -n "Enter year (YYYY): "
read y
a=$((y%4))
b=$((y%100))
c=$((y%400))
if [ $a == 0 -a $b != 0 -o $c == 0 ]
then
echo "$y is leap year"
else
echo "$y is not a leap year"
Fi
RESULT
62
OUTPUT
63
Program No: 23
Date: 27/7/2020
Operations in a file
AIM: Shell script to perform operations like display, list, and make directory and copy, rename,
delete, edit file.
Algorithm
Step 1: Start
Step 5: Stop
Shell Script
#!/bin/bash
i="y"
while [ $i = "y" ]
do
echo "1. Display the directory"
echo "2. Listing the directory"
echo "3. Make a directory"
echo "4. Copy a file"
64
1)echo "Current directory is: "
pwd;;
2)echo "Directories are "
ls;;
read f1
echo "Enter new name of the file: "
read f2
mv $f1 $f2
echo $f1 " is renamed as " $f2;;
6)echo "Enter the file name to delete: "
read f
rm $f
echo $f " is deleted successfully";;
7)echo "Enter the filename you wanted to edit: "
read f
65
vi $f;;
8)echo "Thank you...Now you are exiting from the program"
exit;;
*)echo "Invalid input"
esac
echo "Do you want to continue(y/n): "
read i
if [ $i != "y" ]
then
exit
fi
done
RESULT
66
OUTPUT
67
Program no: 24
Date: 30/7/2020
Algorithm
Step 1: Start
Step 5: Stop
Shell Script
#!/bin/bash
choice=y
while [ $choice = "y" ]
do
echo "1. Contents of etc/passwd"
68
2)echo "List of users currently logged:"
who | more
read a
;;
3)pwd
read a
echo "You are in $a directory";;
4)echo "Thank you...Now you are exiting from the program"
exit;;
*)echo "Invalid input"
esac
echo "Do you want to continue(y/n): "
read choice
if [ $choice != "y" ]
then
exit
fi
done
RESULT
69
OUTPUT
70
Program no: 25
Date: 30/7/2020
Terminals
AIM: Write a shell script to find how many terminals this user logged in.
Algorithm
Step 1: Start
Step 2: get the information about currently logged in user by using who command
cur_log=`who | wc -l`
#!/bin/bash
cur_log=`who | wc -l`
RESULT
71
OUTPUT
72
Program no: 26
Date: 30/7/2020
Algorithm
Step 1: Start
Step 3: if [[ -r $f && -w $f ]]
Step 4: Display the file has read write permission
Step 5: enter the content you wanted to add
Step 6: Stop
Shell Script
#!/bin/bash
echo "enter the filenname: "
read f
if [[ -r $f && -w $f ]];
then
echo "the file has read write permission"
echo "enter the content you wanted to add: "
cat >>$f
fi
cat $f
RESULT
73
OUTPUT
74
Program no: 27
Date: 3/8/2020
File permissions
AIM: Write a shell script which displays a list of all files in the current directory to which you
have read, write & execute permissions.
Algorithm
Step 1: Start
Shell Script
#!/bin/bash
echo "The list of File Names in the curent directory."
echo "Which have Read,Write and Execute permisions. "
for file in *
do
if [ -f $file ]
then
if [ -r $file -a -w $file -a -x $file ]
then
ls -l $file
fi
fi
done
RESULT
Shell script executed successfully.
75
OUTPUT
76
Program no: 28
Date: 3/8/2020
Algorithm
Step 1: Start
Step 3: compare two files cmp $f1 $f2 > error, total=`wc -c error | tr -s " " | cut -d ":" -f4`
Shell Script
#!/bin/bash
echo $total
if [ $total -le 0 ]
then
echo "Both file's contents are same"
else
echo "Both file's contents are not same"
77
fi
RESULT
Shell script executed successfully.
78
OUTPUT
79
Program no: 29
Date: 3/8/2020
Algorithm
Step 1: Start
Step 2: enter the file
Step 3: read f1
Step 4: for i in $f1
Step 5: if [ -f $i ]
Step 6: Display File is already exist or File is not exist
Step 6: Stop
Shell Script
#!/bin/bash
echo "ente a file"
read f1
for i in $f1
do
if [ -f $i ]
then
echo "File $i is already exist."
else
RESULT
Shell script executed successfully.
80
OUTPUT
81
Program no: 30
Date: 03/08/2020
Student data
AIM: Write a shell script to perform operations for student data like view, add and delete
records.
Algorithm
Step 1: Start
Step 2: Enter database name
Step 3: while [$i=’Y], Enter each operation as choice and read them
Step 4: check each operation
Step 5: Display the operation by selecting the choice
Step 6: Stop
Shell script
#!/bin/bash
clear
i="y"
echo "Enter name of database "
read db
while [ $i = "y" ]
do
clear
echo "1.View the Data Base "
echo "2.View Specific Records "
echo "3.Add Records "
echo "4.Delete Records "
echo "5.Exit "
82
case $ch in
1)cat $db;;
2)echo "Enter id "
read id
read des
echo "Enter college name"
read college
echo "$tid $tnm $des $college">>$db;;
4)echo "Enter Id"
read id
# set -a
# sed '/$id/d' $db>dbs1
grep -v "$id" $db >dbs1
echo "Record is deleted"
cat dbs1;;
5)exit;;
83
then
exit
fi
done
RESULT
Shell script executed successfully.
84
OUTPUT
85
Program no: 31
Date: 03/08/2020
BUBBLE SORT
AIM: Write a shell script to sort the given numbers in descending order using Bubble sort.
Algorithm
Step 1: Start
Step 5: Move to the next number along in the list and make this the current number
Step 6: Repeat from step 2 until the last number in the list has been reached
Step 7: If any numbers were swapped, repeat again from step 1
Step 8: If the end of the list is reached without any swaps being made, then the list is ordered
Step 9: Stop
Shell script
#!/bin/bash
echo
i=1
k=1
echo "Enter number of integers to be sorted"
read n
echo "Enter the numbers"
while [ $i -le $n ]
do
read num
x[$k]=$num
86
i=$((i + 1))
k=$((k + 1))
done
x[$k]=0
k=1
echo "The number you have entered are "
while [ ${x[$k]} -ne 0 ]
do
echo "${x[$k]}"
k=$((k + 1))
done
k=1
while [ $k -le $n ]
do
j=1
while [ $j -lt $n ]
do
y=$((j + 1))
if [ ${x[$j]} -lt ${x[$y]} ]
then
temp=${x[$j]}
x[$j]=${x[$y]}
x[$y]=$temp
fi
j=$((j + 1))
done
k=$((k + 1))
87
done
k=1
echo "Number in sorted order..."
while [ ${x[$k]} -ne 0 ]
do
echo "${x[$k]}"
k=$((k + 1))
done
RESULT
Shell script executed successfully.
88
OUTPUT
89
Program no: 32
Date: 06/08/2020
Algorithm
Step 1: Start
Step 3: Use while loop to compute the factorial inside the function fact() by using the
below formula
Step 4: fact=$((fact * num
num=$((num - 1))
Step 5: Stop
Shell Script
echo "Enter a number"
read num
function fact()
{
fact=1
while [ $num -gt 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo $fact
}
fact $num
RESULT
Shell script executed successfully.
90
OUTPUT
91
Program no: 33
Date: 06/08/2020
Algorithm
Step 1: Start
revstr=${str:$i:1}$revstr
Step 4: if [ "$str" = "$revstr" ]
Step 5: Display “It is a palindrome” or else” It is not palindrome”
Step 6: Stop
Shell Script
echo "Input the string without space"
read str
function pali()
{
92
echo "It is not a palindrome."
fi
}pali $str
RESULT
Shell script executed successfully.
93
OUTPUT
94
Program no: 34
Date: 10/08/2020
Rename files
AIM: Write a script to rename all c files to cpp files.
Algorithm
Step 1: Start
Shell Script
#!/bin/ksh
ls *.c >amit.temp
while read line
do
echo $line
new=${line}pp
echo $new
mv $line $new > /dev/null
echo "Renamed all c files to cpp files"
done < amit.temp
RESULT
Shell script executed successfully.
95
OUTPUT
96
Program no: 35
Date: 10/08/2020
File Searching
AIM: The word “mca” is present in some of the files supplied as arguments. Write a script to
search each of these files, and to stop at the first file containing the word “mca” and report it.
Algorithm
Step 1: Start
Shell Script
if [ $# -lt 1 ]
then
echo " Invlaid Arguments....... "
exit
fi
for x in $*
do
grep -w "^mca" $x > output
if [ $? -eq 0 ]
then
cat output
exit
fi
done
97
RESULT
Shell script executed successfully.
98
OUTPUT
99
Program no: 36
Date: 10/08/2020
Algorithm
Step 1: Start
Step 2: for x in $* do if [ -f $x ]
Step 3: Display File name
Step 4: Display number of lines
Step 5: elif [ -d $x ]
Step 6: Display directory name
Step 7: Stop
Shell Script
for x in $* # for receiving any no of arguments
do
if [ -f $x ]
then
echo "File name is : $x"
echo "no of lines are"
wc -l $x
elif [ -d $x ]
then
echo "Give name is directory"
fi
100
done
RESULT
Shell script executed successfully.
101
OUTPUT
102
Program no: 37
Date: 13/08/2020
Algorithm
Step 1: Start
Shell Script
for file in ${*}
do
echo #printing blank line
103
RESULT
Shell script executed successfully.
104
OUTPUT
105
Program no: 38
Date: 13/08/2020
Algorithm
Step 1: Start
Step 6: Stop
Shell Script
#!/bin/bash
r=$#
if [ $((r%2)) -ne 0 ]
then
else
for((i=0;i<$r;i=i+2))
do
f1=$1
f2=$2
cp $f1 $f2
shift 2
done
106
fi
RESULT
Shell script executed successfully.
107
OUTPUT
108
Program no: 39
Date: 13/08/2020
Algorithm
Step 1: Start
Shell Script
hour=$(date +"%H")
# if it is midnight to midafternoon will say G'morning
if [ $hour -ge 0 -a $hour -lt 12 ]
then
greet="Good Morning, $USER"
# if it is midafternoon to evening ( before 6 pm) will say G'noon
echo $greet
109
RESULT
Shell script executed successfully.
110
OUTPUT
111