you can do this:
dpkg -s <packagename> &> /dev/null
then check exit status.only if the exit status of the above command was equal to 0
then the package installed.
so:
#!/bin/bash
echo "enter your package name"
read name
dpkg -s $name &> /dev/null
if [ $? -ne 0 ]
then
echo "not installed"
sudo apt-get update
sudo apt-get install $name
else
echo "installed"
fi