0

I have the following code in unix

 #!/bin/bash
 user=$1
 username=$user'%'
 DB_name=$2
 userpwd=$4
 export PGPASSWORD=$3

 psql -h localhost -U postgres -d $DB_name -At -c "SELECT COUNT(*) AS USER_COUNT FROM pg_roles WHERE rolname = '${user}' " \
| while read -a Record ; 
do
USER_COUNT=${Record[0]} 
echo 'user USER_COUNT ' $USER_COUNT
done

echo 'count' $count
if   [[ $(($USER_COUNT)) -eq 0 ]]
then
echo 'New User ' $user
else
echo 'User Exists ' $user

fi

It basically accepts a given username and checks if it exists in Postgresql. After we pass the arguments, it checks its count, which will be 1, if the username exists, otherwise 0, and stores it in the variable USER_COUNT. It then compares it with 0. But the comparison is fetching me wrong results. Regardless of whether a username exists or not, it only runs the 'True' part of if.

Edit-1 The reason I used if [[ $(($USER_COUNT)) -eq 0 ]] instead of if [$USER_COUNT -eq 0 ], as suggested by Phill, is because I was getting an error message otherwise, saying unary operator expected. I read somewhere that it is because $USER_COUNT is an unset variable.

Edit-2 Thanks for your suggestion Lennart. As suggested by him, I added -x to my first line and found that even though the value of $USER_COUNT is 1 when the user exists, and 0 otherwise, but while comparison, i.e in the step if [[ $(($USER_COUNT)) -eq 0 ]], $USER_COUNT takes the value 0, even if earlier it was 1. Hence, it always evaluates to true. But I am not sure what to do to resolve the above situation.

Edit-3 Thanks a lot Lennart. Your code solved my problem.

  • Should it not be if [[ $(($USER_COUNT)) -ne 0 ]] ? – Lennart Oct 29 at 11:11
  • @Lennart Thanks, I have edited my code. But anyways, the problem still persists. It never goes to the else part regardless of whether the user exists or not. – suvrat Oct 29 at 11:14
  • Adding -x to shebang, i.e. #!/bin/bash -x may reveal what is going on – Lennart Oct 29 at 11:24
1

You may consider something like:

#!/bin/bash
...
cnt=$(psql -h localhost -U postgres -d $DB_name -At -c "SELECT COUNT(*) AS USER_COUNT FROM pg_roles WHERE rolname = '${user}' ")

if [[ ${cnt} -eq 0 ]]
then
    echo 'New User ' $user
else
    echo 'User Exists ' $user
fi

The error you got saying unary operator expected is indeed because the variable is unset. There are various ways to handle that, but in this case, I think it should be fine

0

if [[ $(($USER_COUNT)) -eq 0 ]]

Given that this is bash and you're only performing a simple, numeric comparison, shouldn't that "if" condition look more like this?

if [ $USER_COUNT -eq 0 ] 

Doubled square braces do something "more", allowing the use of regular expressions and other weirdness which you don't need here.

Personally, I wouldn't rely on a count to detect anything. Retrieve something definite from the record (like the name) and make sure you get something back - you could get a zero count back for any number of reasons.

Also, read up on using the Postgres Password File to get passwords into psql. https://www.postgresql.org/docs/9.1/libpq-pgpass.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.