Shell program to search a pattern in a file

A shell program to search a pattern in a file that will take both pattern and file name from the command line arguments. Write a second shell program to read the pattern and the file name from the key board interactively if the pattern and file names are not passed in command line arguments. If they are passed in command line argument, then the variables pattern and file name are set to these values. Finally invoke the first shell file to search a pattern in the file if the file exists.

Create a first file say f1.sh and type the following codes:
#!/bin/sh
if [ $# -eq 0 ]

then
echo "No arguments"
else
grep "$1" $2
fi
Now Create another file say f2.sh and type the following code:
#! /bin/sh
if [ $# -eq 0 ]
then
echo "Enter the pattern"
read pattern
echo "Enter the filename"
read filename
else
pattern= $1
filename= $2
fi
#####################################
OUTPUT

sh f1.sh $pattern $filename
Now execute second file f2.sh.

#####################################

Leave a Reply