pass a variable into a string ksh - bash

how do you pass a global variable into a string inside a function?
I have the following code that works for the most part:
td_query () { bteq << EOF |grep '^>' |sed -e "s/^>/;/g"
$(cat $HOME/.tdlogon)
DATABASE $schemaName;
.set width 10000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
rqstID="1357"
echo $(td_query "select '>'||'UPDATE schema.SEGN_$rqstID_PRCSS_TBL SET POPN_LVL_EXCLN ='||a.CODE_ID||' WHERE ' || b.SQL_FILE_NM ||' AND POPN_LVL_EXCLN IS NULL'
FROM SE_POPN_EXCLSN a
INNER JOIN SE_CODE_LIB b
ON
a.CODE_ID = b.CODE_ID;")
but the results come back:
UPDATE schema.SEGN_ SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'
missing this:
$rqstID_PRCSS_TBL
it should be:
UPDATE schema.SEGN_1357_PRCSS_TBL SET POPN_LVL_EXCLN = 1002 WHERE MR_IND = 'Y'

_ is a legal character in a shell variable. The shell is trying to find a variable by the name of $rqstID_PRCSS_TBL and getting an empty string. (That's why _PRCSS_TBL is disappearing from your output.)
You need to tell the shell where the variable name ends: schema.SEGN_${rqstID}_PRCSS_TBL

Related

Sqlplus script part showing "Badly placed ()'s." error

I'm writing a script and have a problem when trying to get a date from the table. I'm having problem storing it into a variable. I always get the error:
Badly placed ()'s.
The variable TIMEFRAME is "D" and the shell actually gets into the IF, also the password is also not the problem as I use it to connect manually and check the query (no problems here).
This is the part of the script where I'm having problems.
#!/bin/csh -f
if ( $TIMEFRAME == "D" ) then
set TIMEBEG = `sqlplus -s $PASSWORD << EOF \
set head off; \
set feed off; \
select to_char(trunc(sysdate) - 1,'YYYYMMDD') from dual; \
exit; \
EOF`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif
I also tried to store it on a file, same problem
#!/bin/csh -f
set tmp_file=/tmp/tmp.$$
if ( $TIMEFRAME == "D" ) then
sqlplus -s $PASSWORD > $tmp_file << EOF \
set head off \
set feed off \
select to_char(trunc(sysdate) - 1,'YYYYMMDD') from dual; \
exit; \
EOF
set TIMEBEG=`cat $tmp_file`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif
I tried different solutions but with the same problem, tried without semi-colons, without the SETs, to put it on a single line, I'm out of Ideas.
When trying with -xvf this is what I get when I get into the part:
if ( $TIMEFRAME == "D" ) then
if ( D == D ) then
sqlplus -s $PASSWORD > $tmp_file << EOF set head off set feed off select to_char ( trunc ( sysdate ) - 1,'YYYYMMDD' ) from dual ; exit ; EOF
Badly placed ()'s.
Any ideas? This is running on Amazon Linux
I'm not sure you can use a here-doc inside backticks in C shell.
Write the sqlplus input to a file, rather than writing the whole sqlplus command.
#!/bin/csh -f
set tmp_file=/tmp/tmp.$$
if ( $TIMEFRAME == "D" ) then
cat > $tmp_file <<EOF
set head off
set feed off
select to_char(trunc(sysdate) - 1, 'YYYYMMDD') from dual;
exit;
EOF
set TIMEBEG = `sqlplus -s $PASSWORD < $tmp_file`
set TIMEEND = $TIMEBEG"235959"
set TIMEBEG = $TIMEBEG"000000"
endif

How to make read -d return true

I am using the following construct to set a multiline string in bash shell. But this always returns false which does not work when I set set -e. How can I make this to return success?
#!/bin/bash
set -x
set -e
read -d '' QUERY <<EOF
UPDATE table_name SET
field1 = 'value',
field2 = 'value'
WHERE id = 1;
EOF
mysql table_name -e "$QUERY"
While not an answer to your original question, this does get your problem solved. Consider using substitution and e.g. cat
QUERY=$(cat <<EOM
test
test2
EOM
)
mysql table_name -e "$QUERY"
Please, be aware of safety issues if you're reading those values from unsanitized input.

Passing a variable as an argument to a UNIX function

I intent to pass variable as a parameter to my function .(The purpose of this function is to connect to Terdata,fetch top 10 records from a table and write it to a file).But my function is not called instead the names of the files in the home path are being displayed.
Is there any way to pass argument value as a variable.
Below is my script:
#!/bin/ksh
set -x
my_first_fn()
{
query="SEL TOP 10 * FROM XYZ.TABLENAME"
${BTEQ} << EOF
.logon ${pSERVER}/${pUSERNAME},${pPWD};
.set width 6000;
.set separator ',';
.set titledashes off;
.EXPORT FILE = '${home}/BTEQ.txt';
${stmt};
.EXPORT RESET
.IF ERRORCODE <> 0
THEN .GOTO EXITERR
.QUIT 0;
.LOGOFF;
.LABEL EXITERR
.QUIT 1;
.LOGOFF;
EOF
}
LOG_FILE=${home}/myscript.log
my_first_fn $query | tee ${LOG_FILE}
Thanks in advance...:-)
I didn't define stmt variable and expected the script to execute.This is the part which caused the issue.Rather than this I can directly call the function and mention SEL TOP 10 * FROM XYZ.TABLENAME in the function's body. – UnixNewbie

How to fetch more than one column value from oracle select query to shell variable

I am trying to fetch a row with more than one column value to different shell variables. Infact I found that at a time all the column values can be stored to single shell variable. But how can I put those column values to seperate shell variables. Below is an example I am trying for time being
function sqlQuery {
sqlplus -S shiyas/********* <<'EOF'
set heading OFF termout ON trimout ON feedback OFF
set pagesize 0
SELECT name,open_mode from v$database;
EOF
}
OUTPUT="$( sqlQuery )"
echo $OUTPUT
Here I am getting the output as
ORCL READ WRITE
But my requirement is column values ORCL, READ WRITE should get assigned to different shell variable.
I tried the below of parsing.
echo "$OUTPUT" | while read name open_mode
but it was throwing unexpected end of file error.
-bash-3.2$ sh call_sql_col_val_1.sh
ORCL READ WRITE
call_sql_col_val_1.sh: line 18: syntax error: unexpected end of file
Please let me know what concept I can use to fetch a single row column values to different shell variables.
I do this via eval myself:
oracle#******:/*****> cat test.sh
#!/bin/bash
function sqlQuery {
sqlplus -S / as sysdba <<'EOF'
set heading OFF termout ON trimout ON feedback OFF
set pagesize 0
SELECT name,open_mode from v$database;
EOF
}
eval x=(`sqlQuery`)
NAME=${x[0]}
OPEN_MODE="${x[1]} ${x[2]}"
echo NAME IS $NAME
echo OPEN_MODE IS $OPEN_MODE
So we are running the same function you have above, passing it into x and running it through eval to handle the delimitation. Then you have an array and call call is as such: x[0] for the first item, for example.
Output is:
oracle#******:/******> sh test.sh
NAME IS ******
OPEN_MODE IS READ WRITE

What is wrong with this perl script

Below is the Perl script that I have written
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `sqlplus -s hr/password\#dbname\#sql_script.sql $parameter1`;
SQL SCRIPT:
select count(*) from employees
where job_id <> '&1'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
When I run this query by passing the argument &1 it is giving me a string with an error message. But when I run the same query by hardcoding I'm getting the output properly (the count is 15 which is the correct answer).
select count(*) from employees
where job_id <> 'PU_CLERK'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
I'm not able to understand where I'm going wrong. How do I pass parameters in Perl. We used to do the same way in shell script and it was working absolutely fine.
EDIT:
This is the error message im getting
perl call_sql.pl
value of first variable isold 2: whe
re job_id <> '&1'
new 2: where job_id <> 'PU_CLERK'
15
So its basically not printing the 15 value its printing all those string also when i use '&1' in my sql script
EDIT2:
Hi Guys finally it is working. In my sql code instead of giving '&1' i gave '$1' Now i want to know is $1 of some significance in Perl? Thanks..
I don't know the answer to your current problem, but using the DBI module is the better solution, so I wrote a sample script to get you started. You may need to tweak some things to get it to work.
use strict;
use warnings;
use DBI;
my $dbname = "mydb";
my $user = "foo";
my $passwd = "bar";
my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd)
or die $DBI::errstr;
my $parameter1 = 'PU_CLERK';
my $statement = "select count(*) from employees
where job_id <> ?
and salary > 9000
and commission_pct is not null
order by first_name desc";
my $sth = $dbh->prepare($statement) or die $dbh->errstr;
$sth->execute($parameter1) or die $sth->errstr;
while (my $row = $sth->fetchrow_arrayref) {
print "#$row"; # or whatever you want to do with it
}
$dbh->disconnect or warn $dbh->errstr;
This has nothing with perl.
Proof: make an shell script, say mytest.sh with the next content:
#!/bin/bash
echo "$0: Got $# args" >&2 #to stderr
i=0
for arg
do
let i++
echo "$0: arg($i)=$arg=" >&2 #to stderr
done
echo "15" #result to stdout
make it executable, with chmod 755 mytest.sh
Now modify your perl script as:
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `./mytest.sh -s hr/password\#dbname\#sql_script.sql $parameter1`;
print "script returned: $COUNT_1\n";
run it
$ perl script.pl
result:
./mytest.sh: Got 3 args
./mytest.sh: arg(1)=-s=
./mytest.sh: arg(2)=hr/password#dbname#sql_script.sql=
./mytest.sh: arg(3)=PU_CLERK=
script returned: 15
e.g. the perl
correctly run the external script
correctly passes the arguments to it
so, search for the error in the sqlplus doccumentation...

Resources