I am using psql to get a list of databases already running on my machine and a user-defined variable which has a list of database names. How can I check if the user-defined variables contain names that are already existing as a database? Here Is what I have done so far but the results are not consistent
x=`psql -l | awk '{print $1}' | grep -v "+" | grep -v "Name" | grep -v "List" | grep -v "(" | grep -v "template" | grep -v "postgres" | grep -v "|" | grep -v ":"`
db_name=test1,test2
array=(${x})
for i in "${array[@]}"
do
for db in $(echo ${db_name} | tr ',' ' '); do
if [[ ${i} != ${db} ]] ; then
echo ${db} "Does not exists"
fi
done