How to find lines that match exact input and count?

I am writing a package manager in BASH and I would like a small snippet of code that finds lines that match exact input and count them. For example, my file contains:

xyz
xyz-lib2.0+
xyz-lib2.0
xyz-lib1.5

and "grep -c xyz" returns 4.

The current function is:

# $1 is the package name.
function check_pkg_install {
	foo=$(grep -c $1 /etc/spm/packages.list)
	if [ $foo -eq 1 ] ; then
		pkg_installed=1
		return 1
	else
		pkg_installed=0
		return 0
	fi
}

This is a summer project while I have no school, so you can take your time (but it shouldn't take 2 months...) Project is attached.

You can make a array of packages matched.

i=0
for pkg in `grep $1 packages.list`
do
     packages[$i]="$pkg"
     i=$(expr $i + 1)
done

Now, the array $packages contains all grep results. Returns the count of matches:

> echo ${#packages[@]}
4

And you can check_version of every items of the array.

I hope this help. :slight_smile:

Try:

foo=$(grep -Fxc "$1" /etc/spm/packages.list)
1 Like

@Scrutinizer: Thank you. It worked when I copied and pasted it.