Practical Os
Practical Os
Aim: To write a Shell script that displays list of all the files in the current
directory to which the user has read, write and execute permissions.
Source Code:
#!/usr/bin/sh
echo "enter the directory name"
read dir
if [ -d $dir ]
then
cd $dir
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$File has all permissions"
else
echo "Files does not have all permissions"
fi
fi
done
fi
Result: The result is obtained to display list of all the files in the current directory
to which the user has read, write and execute permissions.
4
Output:
5
Ex.No :02 Count the number of lines and words in the input file
Date : 10/10/2023
wc command:
The syntax of the wc command: # wc [options] filenames
The following are the options and usage provided by the wc command.
wc -l – Prints the number of lines in a file.
wc -w – prints the number of words in a file.
wc -c – Displays the count of bytes in a file.
wc -m – prints the count of characters from a file.
wc -L – prints only the length of the longest line in a file.
Result: cat’ command and wc command to count the number of lines and
number of words in the given input file is simulated using Shell Script.
6
Output:
7
Ex.No :03 grep command
Date : 14/10/2023
Aim: To write a Shell program using grep command that count the number of blank
lines in the file1 and Select the lines from the file1 that have the string, "UNIX".
Source Code: grep command that count the number of blank lines in the file1
cat command
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
Source Code: Select the lines from the file1 that have the string, "UNIX".
$ grep -i "UNIX" demo_file
THIS LINE IS THE UNIX 1ST UPPER CASE LINE IN THIS FILE.
this line is the unix 1st lower case line in this file.
This unix Line Has All Its First Character Of The Word With Upper Case.
And this is the last line in unix.
Result: The result is obtained using Shell program grep command to count the
number of blank lines in the file1 and Select the lines from the file1 that have the
string, "UNIX".
8
Output:
9
Ex.No :04 sed command
Date : 19/10/2023
Aim: To write a Shell program using sed command that print lines, numbers of
lines beginning with “O” and swap the first and second word in each line in the
file.
Source Code: sed command : Print lines numbers of lines beginning with “O”
sed '=' exm.txt // The '=' sign is used to print the line number
sed -n '/O/=' exm.txt // The above command will display the line number that
contains the word 'O'
Source Code:
sed command : swap the first and second word in each line in the file.
sed -e "s/\([^ ]*\) *\([^ ]*\)/\2 \1 /g" file
Result:The result is obtained using Shell program sed command to print lines,
numbers of lines beginning with “O” and swap the first and second word in each
line in the file.
10
Output:
11
Ex.No :05 awk command
Date : 31/10/2023
Aim: To write a Shell program awk script to Count the number of lines in a file
that do not contain vowels and find the number of characters, words and lines in a
file.
Source Code: To Count the number of characters, words and lines in a file
#!/usr/bin/sh
# path to the file
file_path="/home/jo/test.txt"
Source Code: awk script to Count the number of lines in a file that do not contain
vowels
#!/bin/bash
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels are: ",count}' $file
Result: The result is obtained using awk script to Count the number of lines in a
file that do not contain vowels and find the number of characters, words and lines
in a file.
12
Output:
13
Ex. No :07 Factorial Program
Date : 09/11/2023
Aim: To write a Shell program to find out factorial of the given number.
Source Code:
#!/bin/bash
echo "Enter a number"
read num
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i)) #fact = fact * i
}
echo $fact
16
Output:
17