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

UNIX Lab Programs BMS

The document contains scripts that perform various file operations like: 1) Counting number of books in a library file 2) Changing file permissions and displaying old and new permissions 3) Counting number of regular and directory files 4) Comparing two files and removing one if they are identical 5) Extracting a field from a file using a delimiter 6) Adding line numbers to a file

Uploaded by

RevathiSBC
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

UNIX Lab Programs BMS

The document contains scripts that perform various file operations like: 1) Counting number of books in a library file 2) Changing file permissions and displaying old and new permissions 3) Counting number of regular and directory files 4) Comparing two files and removing one if they are identical 5) Extracting a field from a file using a delimiter 6) Adding line numbers to a file

Uploaded by

RevathiSBC
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

#!

/bin/sh
echo -e "Enter the name of the library file: \c"
read fname
echo "There are `wc -l < $fname` books in the library"

#!/bin/sh
echo -e "Enter the filename: \c"
read fname
echo -e "Enter the new absolute permissions: \c"
read perm
echo "Old permissions: `ls -l $fname | cut -c 2-10`"
chmod $perm $fname
echo "New permissions: `ls -l $fname | cut -c 2-10`"

#!/bin/sh
echo "The number of regular files in the current directory are : `find . -type f | wc -l`"
echo "The number of directory files in the current directory are : `find . -type d | wc -l`"

#!/bin/sh
echo -e "Enter the two filenames: "
read fname1 fname2
cmp $fname1 $fname2
if [ $? -eq 0 ] ; then
echo "The files are not unique"
rm -i $fname2
else
echo "The files are unique"
fi

#!/bin/sh
echo -e "Enter the filename: \c"
read fname
echo -e "Enter the field number to be extracted: \c"
read num
echo -e "Enter the delimiter used: \c"
read delim
cut -d "$delim" -f $num $fname

#!/bin/sh
echo -e "Enter the filename to be indexed: \c"
read fname
echo "Before indexing:"
cat $fname
echo "After indexing:"
cat -n $fname

#!/bin/sh
echo "Enter your message:"
read msg
wall "$msg"
echo "Message sent successfully"

#!/bin/sh
mkdir temporary
find $HOME/lab -type f -mtime -2 -ok cp {} ./temporary \;
echo "Written files:"
ls ./temporary

#!/bin/sh
if [ $# -ne 2 ] ;then
echo "ERROR!!! Rerun the script by passing the pattern and the filename as the command line
arguments"
exit
fi
grep "$1" $2 || echo "Pattern not found"
echo "Job done"

#!/bin/sh
if [ $# -ne 2 ] ;then
echo "ERROR!!! Rerun the scritpt by passing the two filenames as command line arguments"
exit
fi
ls -l $1 | cut -c 2-10 > perm1
ls -l $2 | cut -c 2-10 > perm2
cmp perm1 perm2
if [ $? -eq 0 ] ;then
echo "The permissions are identical and they are: `cat perm1`"
else
echo "The permissions are unique"
echo "$1: `cat perm1`"
echo "$2: `cat perm2`"
fi

#!/bin/sh
echo -e "Enter the fiename: \c"
read fname
echo "Character size: `wc -c < $fname`"
echo "Size on disk: `du -h $fname`"

#!/bin/sh
if [ $# -ne 1 ] ;then
echo "ERROR!!! Rerun the script by passing the filename as command line argument"
exit
fi
if [ -f $1 ] ;then
echo "Regular file"
elif [ -d $1 ] ;then
echo "Directory file"
fi

#!/bin/sh
echo "The number of current users are: `who | wc -l`"

#!/bin/sh
if [ $# -ne 1 ] ;then
echo "ERROR!!! Please pass the single user name as a command line argument and rerun the
script"
exit
fi
who | grep "$1" | grep -v "pts" > time
if [ $? -ne 0 ] ;then
echo "User $1 has not yet logged in"
exit
fi
hh=`cat time | cut -c 34-35`
mm=`cat time | cut -c 37-38`
date > dat
h=`cat dat | cut -c 12-13`
m=`cat dat | cut -c 15-16`
th=`expr $h - $hh`
tm=`expr $m - $mm`
if [ $tm -lt 0 ] ;then
tm=`expr 60 + $tm`
th=`expr $th - 1`
fi
echo "The user $1 has been logged since the last $th hours and $tm minutes"

#!/bin/sh
echo -e "Enter the filename: \c"
read fname
echo "The number of vowels in the file are: `tr -cd [aeiouAEIOU] < $fname | wc -c`"

You might also like