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

LINUX AND SHELL-LAB PROGRAMS

The document outlines a Shell Programming Lab course with objectives to provide practical training in file commands, familiarize students with shell scripting, and build programs using filter commands. It includes a list of exercises that cover various shell scripting tasks such as file manipulation, system configuration, and implementing commands like pipes and redirection. Each exercise is accompanied by an aim, algorithm, coding examples, and results indicating successful execution.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LINUX AND SHELL-LAB PROGRAMS

The document outlines a Shell Programming Lab course with objectives to provide practical training in file commands, familiarize students with shell scripting, and build programs using filter commands. It includes a list of exercises that cover various shell scripting tasks such as file manipulation, system configuration, and implementing commands like pipes and redirection. Each exercise is accompanied by an aim, algorithm, coding examples, and results indicating successful execution.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

SHELL PROGRAMMING LAB

COURSE OBJECTIVE:

1. To impart Practical Training in file commands.


2. Familiarize with shell script for system configuration.
3. Build programs using filter commands.
4. Provide knowledge on working with simple programs with shell script.

LIST OF PROGRAMS:

1. Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

2. Write a shell script to show the following system configuration:

o Currently logged user and his log name.


o Current shell, home directory, Operating System type, current Path setting, current working directory.
o Show currently logged number of users, show all available shells
o Show CPU information like processor type, speed
o Show memory information.

3. Write a Shell Script to implement the following: pipes, Redirection and tee commands.

4. Write a Shell script for displaying current date, user name, file listing and directories by getting user
choice.

5. Write a Shell script to implement the filter commands.

6. Write a Shell script to remove the files which has file size as zero bytes.

7. Write a Shell script to find the sum of the individual digits of a given number.

8. Write a Shell script to find the greatest among the given set of numbers using command line arguments.

9. Write a Shell script for palindrome checking.

10. Write a Shell script to print the multiplication table of the given argument using for-loop.
Ex:no: 1.FILE COMMAND

Date:

AIM:
To implement the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

Algorithm:
Step1: list the contents of a directory - ls
Step2: display the contents of a file – more, less and cat
Step3: determine the type of a file - file
Step4: list the present working directory - pwd
Step5:create/delete a directory - mkdir, rmdir
Step6:Move from one directory to another, this stands for change directory- cd
Step7:Copy files - cp
Step8:delete a file – rm

CODINGS:
#rm(remove)
ls
rm file3
#cp(copy)
cat file1
cat file2
cp file1 file2
#cat(create a
file) cat>file3
Shell Programming
#mv(rename)
mv file5 newfile
#cmp(compare)
Cat>file1
Hi
Students
Cat>file2
Welcome to Computer
Lab cmp file1 file2
#wc(word count)
wc file3
#split(Duplicate)
ls
split file3
#diff(Difference)
diff file1 file2

OUTPUT:

add case duplicate file1 hello MD mul path sum2 users


cal cpu file file2 HELLO menu pal rm UL
rm: cannot remove 'file3': No such file or
directory hi
students
welcome to computer lab
cp: missing destination file operand after 'file3'

RESULT:

Thus the above program has been executed and verified successfully.
Ex.no: 2.SYSTEM CONFIGURATION

Date:

AIM:

To print the following system configuration.

ALGORITHM:
Step1: selects list of files from present working directory
Step 2:check for each file wither its is has read, write and execute permissions if true
goto step 3
Step 3: print
file Step 4 :stop

CODINGS:

a) Currently logged user and his log Name

echo -e "User name: $USER (Login name: $LOGNAME)"

b) Current Shell, home directory, Operating system type, current


path setting, current working directory

echo -e "Current Shell: $SHELL"


echo -e "Home Directory: $HOME"
echo -e "Your O/s Type:
$OSTYPE" echo -e "PATH:
$PATH"
echo -e "Current directory: `pwd`"

c) Show currently logged number of users, show all available shells

echo "Currently logged on


users:" who
d) Show CPU information like Processor type, speed
echo -e "Computer CPU Information:"
echo -e " -"
cat /proc/cpuinfo
echo -e "Computer CPU speed:"
echo -e " -"
lscpu | grep MHz
e) Show memory information
echo -e "Computer Memory
Information:" echo -e " -"
cat /proc/meminfo

OUTPUT:
user name :ug16(login
name:ug16) current shell:
/bin/bash
honee directory:
/home/ug16 your o/s type:
linux-gnu
PATH: /sbin:/bin:/usr/bin:/usr/local/bin:/snap/bin
current directory:'pwd'

computer cpu information:


.......
computer cpu speed:

CPU MHz: 3408.000


computer memory information:
.......

RESULT:

Thus the above program has been executed and verified successfully.
Ex:no: 3.PIPES,REDIRECTION &TEE COMMANDS

Date:

AIM:

To implement the following Pipes, Redirection and tee commands

Algorithm:
Step1: enter the name of the directory Read dir
Step2: if it is a directory Then
list the files present in that directory
By using ls command with –p option to list all directory files in a given
directory
Step 3: else enter the directory name
Step 4: stop

CODINGS:

#Pipes
ls
ls | head -3
ls | head -3 | tail -1
echo -e " "

#Redirection
ls
ls>myoutput
ls
echo -e " "

#tee command
cat file1
cho 'hi' | tee -a file1
echo 'welcome' | tee -a
file1 echo 'to' | tee -a file1
echo 'cs' | tee -a
file1 cat file1
OUTPUT:

2f cal cur file3 p1 p2 pd pd1 pd2 sn


2f cal
cur
cur

2f cal cur file3 p1 p2 pd pd1 pd2 sn


2f cal cur file3 myoutput p1 p2 pd pd1 pd2 sn

cat: file1: No such file or


directory hi
welcome
to
cs
hi
welcome
to
cs

RESULT:

Thus the above program has been executed and verified successfully.
Ex:no: 4.DISPLAY CURRENT
DATE,USER NAME,FILE LIST
&DIRECTORY

Date:

AIM:

To displaying current date, user name, file listing and directories by


getting user choice.

ALGORITHM:
Step 1: Create a file with 5-6 lines of data File can be created by vi sample.dat or
cat sample.dat
Step 2:Now write a shell script with vi 1.sh
Step3:Check the no of arguments for shell script if 0 arguments then
print no arguments else if 1 argument then print 1 argument else if 2 arguments then
print 2 arguments else $1 is name of file from which we are printing data between
the line numbers.
Step 4:Stop

CODINGS:
echo "current date: $(date)"
echo "your user name $(echo
$USER)" echo " 1 List of the File"
echo " 2 List of the File or
Directory" echo " 3 Exit"
while :
do
echo " Enter the Choice:"
read c
case $c in
1) ls -f
;;
2) Echo " Enter the file
name:" read t
if [ -f $t
] then
echo " It is a File"
echo " Number of
Lines:" wc -l $t
elif [ -d $t
] then
echo " It is a Directory"
fi
;;
3) Exit
;;
esac
done
output:
your username:
current date and time: Sun Sep 29 23:11:19 PDT 2019
1. list of the file
2. list of the file or directory
3. exit
enter the choice:
1
cal log p2a shell newfile pd2 case
lu p1 pd sod gn dup cal1
file 1 current . file3 s1 ..
pd1 bb pdd list ss mul ul
enter the choice:
2
enter the filename:
p2a
it is a file
number of
lines:
wc: invalid option -- '1'
Try 'wc --help' for more
information. enter the choice:
3
RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 5.FILTER COMMANDS

Date:

AIM:

To Modify "filter" command to display calendars of the specified range of


months.

Algorithm:
Step1: enter the name of the directory Read dir
Step2: if it is a directory Then
list the files present in that directory
By using ls command with –p option to list all directory files in a given
directory
Step 3: else enter the directory name
Step 4: stop

CODINGS:

ls
ls | head -3
ls | head -3 | tail -1
echo -e " "

#tee command
cat file1
cho 'hi' | tee -a file1
echo 'welcome' | tee -a
file1 echo 'to' | tee -a file1
echo 'cs' | tee -a
file1 cat file1
OUTPUT:

2f cal cur file3 p1 p2 pd pd1 pd2 sn


2f cal
cur
cur

2f cal cur file3 p1 p2 pd pd1 pd2 sn


2f cal cur file3 myoutput p1 p2 pd pd1 pd2 sn

hi
welcome
to
cs
hi
welcome
to
cs

RESULT:

Thus the above program has been executed and verified successfully.
Ex:no: 6.FILE MANIPULATION

Date:

AIM:

To compare two files and if found equal delete the duplicate file.

Algorithm:

Step1: selects list of files from present working directory

Step 2:check for each file wither its is has read, write and execute
permissions if true goto step 3

Step 3: print file

Step 4 :stop

CODINGS:

#!/bin/bash
echo "enter file1:"
read file1
echo "enter file2:"
read file2
cmp $file1 $file2 >newfile
x= 'wc newfile|cut -d "" -f2'
if [ '$x -eq 0' ]
then
rm -i
$file2 fi
OUTPUT:
enter file1:
hh
enter file2:
hh
rm:remove regular file:’hh’?y

RESULT:

Thus the above program has been executed and verified successfully.
Ex:no: 7. SUM OF DIGITS

Date:

AIM:

To find the sum of the individual digit of a given number.

ALGORITHM:
Step 1: read any number to find factorial
Step 2: initialize fact=1 and i=1
Step 3: while i less than do fact=fact* i i=i+1 done
Step 4:print fact
Step 5:stop.

CODINGS:

#!/bin/bash
#take input from user and calculation
sum read -p "enter first number:" num1
read -p "enter second number:"
num2 sum = $(($num1 + $num2))
echo " sum is :$sum"
OUTPUT:

enter first number:4


enter second
number:4 sum is : 8

RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 8. GREATEST NUMBER

Date:

AIM:

To find the greatest among the given set of numbers using command line
arguments.

ALGORITHM:
Step 1: create a file with 5 to10 lines of data
Step 2: write an awk script find the length of file store it in
chrcnt step3: count the no of fields (NF), store it in wordcount
step4: count the no of records (NR), store it in
NR step5: print chrcnt,NRwordcount
step6: stop

CODINGS:

#!/bin/sh
echo "enter first
no" read first
echo "enter second
no" read sec
echo "enter third
no" read third
if [$first -gt $sec];
then
if [$first -gt $third];
then
echo-e "$first is greatest
no" else
echo-e "$sec is greatest no"
fi
Else
if [$sec -gt $third];
then
echo-e "$sec is greatest
number" else
echo-e "$third is greatest
number" fi
fi

OUTPUT:

enter first
no 20
enter second
no 30
enter third
no 40
40 is greatest number

RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 9.PALINDROME

Date:

AIM:

To Write a shell script for palindrome checking.

ALGORITHM:
step 1: if arguments are less than 1 print Enter at least one input file name and
goto step 9
Step 2: selects list a file from list of arguments provided in command line
Step 3: check for whether it is directory if yes print is directory and goto step 9
Step 4: check for whether it is a regular file if yes goto step 5 else goto step 8
Step 5: print given name is regular file
Step 6: print No of lines in
file Step 7: goto step
Step 8: print not a file or
adirectory Step 9: stop

CODINGS:

#!/bin/bash
read -p "enter a string:" string
if [[ $(rev<<<"$string") == "$string" ]];
then echo palindrome
else
echo "**$name is not a
palindrome**" fi
OUTPUT:
enter a string:amma
palindrome
enter a string:hai
** is not a palindrome**

RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 10.MULTIPLICATION TABLE

Date:

AIM:

To print multiplication table of the given argument using for loop.

ALGORITHM:
step 1: if arguments are less than 1 print Enter at least one input file name and
goto step 9
Step 2: selects list a file from list of arguments provided in command line
Step 3: check for whether it is directory if yes print is directory and goto step 9
Step 4: check for whether it is a regular file if yes goto step 5 else goto step 8
Step 5: print given name is regular file
Step 6: print No of lines in
file Step 7: goto step
Step 8: print not a file or
adirectory Step 9: stop

CODING:

#!/bin/sh
echo " enter a
number" read n
i=0
while [ $i -le 10
] do
echo "$n\* $i =`expr $n \* $i`"
i=`expr $i + 1`
done
OUTPUT:
enter a
number 5
5\* 0 =0
5\* 1 =5
5\* 2 =10
5\* 3 =15
5\* 4 =20
5\* 5 =25
5\* 6 =30
5\* 7 =35
5\* 8 =40
5\* 9 =45
5\* 10 =50

RESULT:
Thus the above program has been executed and verified successfully.

You might also like