LINUX AND SHELL-LAB PROGRAMS
LINUX AND SHELL-LAB PROGRAMS
COURSE OBJECTIVE:
LIST OF PROGRAMS:
1. Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.
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.
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.
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:
RESULT:
Thus the above program has been executed and verified successfully.
Ex.no: 2.SYSTEM CONFIGURATION
Date:
AIM:
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:
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'
RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 3.PIPES,REDIRECTION &TEE COMMANDS
Date:
AIM:
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:
RESULT:
Thus the above program has been executed and verified successfully.
Ex:no: 4.DISPLAY CURRENT
DATE,USER NAME,FILE LIST
&DIRECTORY
Date:
AIM:
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:
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:
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:
Step 2:check for each file wither its is has read, write and execute
permissions if true goto step 3
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:
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:
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:
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:
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.