0

I am assigning the count from a sql query to a shell variable. But there seems to be some other blank spaces coming up.

output=$(
  $ORACLEHOME/bin/sqlplus -s $DBUSER/$DBPASSWORD <<EOF
   set pages 0 feed off echo off

   SELECT COUNT(1) 
            FROM TABLE_NAME
            exit;
EOF
)

echo "output is $output"

echo "length of output is ${#output}"

the length should be coming as 1. But it is coming as 3. Any help is appreciated.

2
  • Post the output of your db query alone in the question please. Also post the output. Commented Jul 1, 2017 at 11:05
  • This is the output. output is 0 length of output is 3 The table has no records. So it should return '0'. Commented Jul 1, 2017 at 11:09

1 Answer 1

1

I don't think this is a neat way to do the query and assign the results to a variables. Actually there is no question here.. The sql output may be formatted and I assume that to have spaces in the beginning. You can either modify your sql query to print raw results- which I think is the best option- or do

output=${output##*[[:space:]]}

before calculating its length. If the variable has spaces in its beginning and end, then you may do

output=${output//[[:space:]]/}
Sign up to request clarification or add additional context in comments.

1 Comment

@GourabSengupta : You're welcome, but try modifying the query to print the count in the raw format. That would be the best way to do it I guess :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.