Got a 08003 when DB2 Calls in sourced Code - bash

I hope this post is better to understand:
I use the bash db2 function to create a sessions and after this i want to execute some code. When i do it in a sourced Script i get a 08003.
dbconnect.sh:
#!/bin/bash
{
db2 -x +c < Connectionstring # RC is 0
db2 -x +c "Select * from Table" # RC is 4, erros say 08003
}
main.sh:
#!/bin/bash
. ./dbconnect.sh
change dbconnect to following code is not helping:
dbconnect.sh:
#!/bin/bash
db2 -x +c < Connectionstring # RC is 0
db2 -x +c "Select * from Table" # RC is 4, erros say 08003
but putting all in one file works perfekt:
#!/bin/bash
db2 -x +c < Connectionstring
db2 -x +c "Select * from Table"
Any Ideas?
The Bashversion:
4.3.48(1)-release (x86_64-suse-linux-gnx)
Linux Version:
SUSE LINUX Enterprise Server 12 SP3

Related

Check if a role exists in PostgreSQL using psql

I need in a bash script a IF condition on the existence of a role in a PostgreSQL database. I have found solutions in SQL code [1, 2], but I need something I can use directly in bash, I assume with the help of psql. In [2] there are also psql solutions, but I don't manage to adapt it in a IF statement.
I have tried this unsuccessfully (I am a PostgreSQL and bash newbie):
psql_USER=my
if [ "$( psql -h db -U postgres --no-psqlrc --single-transaction --pset=pager=off --tuples-only --set=ON_ERROR_STOP=1 -tc "SELECT 1 FROM pg_user WHERE usename = $psql_USER" | grep -q 1 )" == '1' ] > /dev/null 2> /dev/null; then
echo "HOURRA !"
fi;
Result is:
Password for user postgres:
ERROR: column « my » does not exist
LINE 1: SELECT 1 FROM pg_user WHERE usename = my
^
I would avoid the quoting problem like this:
if psql -Atq -c "SELECT '#' || usename || '#' FROM pg_user" | grep -q '#'"$psql_USER"'#'
then
echo yes
fi
The psql invocation selects a list of all usernames, prefixed and suffixed with #. The grep has return code 0 if psql_USER contains one of these user names, else 1. The then branch of if is only taken if the return code of the pipeline is 0, that is, if the user exists in the database.

How to hide export variable details shell script

I have 2 script
script 1 : demo_details.txt
script 2 : demo.sh
Script 1 : Path : /demo/d/demo_details.txt and contain below details
export CON_DB_TECY=Username/Password#host:port/Servicename -> `abc/abc#local:123/orabc`
Script 2 : Path : /demo/d/demo.sh and contain below code
. /demo/d/demo_details.txt
sqlplus -s -S << EOF
$CON_DB_TECY
select * from dual;
exit;
EOF
When i run above script 2 using -> sh x demo.sh
It prints details of demo_details.txt => CON_DB_TECY=abc/abc#local:123/orabc
which is connection details that i want to secure and
should not be displayed when i run script using sh -x demo.sh
you can put all connection and sql in demo_details.txt and redirect it
demo_details.txt
Username/Password.....
select * from dual;
exit;
then
demo.sh
sqlplus -s -S <demo_details.txt

Find if a PostgreSQL database exists with bash

I have a bash function where I check if a PostgreSQL database already exists.
I capture the output. If database exist PostgreSQL returns the database name as response.
function is_database() {
local database=$1
local output=$(sudo -u postgres psql -c "SELECT datname FROM pg_catalog.pg_database WHERE datname=\"$database\";")
if [[ $output = *"${1}"* ]]
then
return 0
else
return 1
fi
}
is_database test
I get the following error:
column "test" does not exist
I am not searching for a table, but a database.
Use single quotes for string literals:
sudo -u postgres psql \
-c "SELECT datname FROM pg_catalog.pg_database WHERE datname='$database'"
Your code as it is won't work for database names like has spaces or has'quotes.

From a shell script, how can I check whether a table in db2 database exists or not?

I am trying to write a script which allows to check if the db2 table exists or not. If it exists I will continue to touch a file if not exists then it has to wait for 30 min and try to check the same after 30 min. How might I achieve this?
#!/bin/sh
db2 "connect to <database> user <username> using <password>"
Variable=`db2 -x "SELECT COUNT(1) FROM SCHEMA.TABLEA WHERE 1=2"`
while read Variable ;
do
if $Variable=0
then touch triggerfile.txt
else
sleep 30
fi
done
You want to continually poll (without limitation on time) for a table to exist? Might be more readable to use bash or korn syntax, and avoid backticks but that's your choice.
Usual caveats apply, don't hardcode the password.
Apart from the looping logic, you might try this inside the loop (bash or ksh syntax shown below), initialising the variables to suit yourself:
db2 "connect to $dbname user $username using $passwd"
(( $? > 0 )) && print "Failed to connect to database " && exit 1
db2 -o- "select 1 from syscat.tables where tabschema=$schema and tabname=$tabname with ur"
rc=$?
# rc = 0 : the table exists in that schema
# rc= 1 : the table does not exist
(( rc == 1 )) && touch triggerfile.txt
# rc >= 2 : some warning or error, need to investigate and correct
(( rc >= 2)) && print "problems querying syscat.tables" && exit 1
db2 -o- connect reset

Removing leading \n while assaigning to a variable

I have a DB2 query in a shell script which return an integer value, but I am unable to store it in a variable.
temp1= echo db2 -x "select max(id) from work.work_tb"
I am getting this output when I run it, sh -x test.sh
db2 -x select max(id) from work.work_tb
echo 50
temp1=
50
So for some reason $temp1 is unable to get the value, I think its because the db2 query is returning value prefixed with \n. How do I get rid of the newline char and get the value to temp1?
No, that's not why.
temp1=`db2 -x "select max(id) from work.work_tb"`
emp1=$(echo db2 -x "select max(id) from work.work_tb")
or using backticks
emp1=`echo db2 -x "select max(id) from work.work_tb"`
In general, to remove newlines, you can pass it to tools like tr/sed etc
... | tr -d "\n"

Resources