Calling two functions one after other is failing in bash/sqlplus - linux

I have two functions- ABC, XYZ. ABC deletes some rows in table1 as user1 and XYZ drops some users as DB User. First I am calling ABC and then XYZ. ABC function is being executed but at XYZ it is failing- "XYZ: command not found"
function ABC
{
sqlplus -s $ur1/$pwd#$SID << EOF
delete from table1 where row_name = 'A1';
delete from table2 where row_name = 'A2';
exit
EOF
}
function XYZ
{
sqlplus eip_dba/eip_dba$result#${input} << eof
set timing off
set serveroutput on size 10000
set feedback off
spool xyz_$input.out
drop user usr1 cascade;
drop user usr2 cascade;
drop user usr3 cascade;
commit;
exit
eof
}
ABC
XYZ
bash-3.2$ ./db_test.sh
2 rows deleted.
2 rows deleted.
./db_test.sh: line 100: XYZ: command not found
Please let me know what went wrong.
Thanks in advance.

Try removing the whitespace before EOF
function ABC
{
sqlplus -s $ur1/$pwd#$SID << EOF
delete from table1 where row_name = 'A1';
delete from table2 where row_name = 'A2';
exit
EOF
}
I tried this in a sample program and it fixed the problem.

The format of here-documents is:
<<WORD
here-document content ...
here-document content ...
here-document content ...
WORD
WORD can be any label you like, but the end marker must be the only thing on the line, no whitespaces before or after.
In your example functions you are using eof and EOF as labels, you should remove the space characters from in front of them so that they are really at the beginning of the line, like this:
function ABC
{
sqlplus -s $ur1/$pwd#$SID << EOF
delete from table1 where row_name = 'A1';
delete from table2 where row_name = 'A2';
exit
EOF
}
function XYZ
{
sqlplus eip_dba/eip_dba$result#${input} << eof
set timing off
# ... and so on ....
eof
}
That is, the ending EOF and eof are right at the beginning of the line, with no trailing spaces either.

Related

Bash script with multiline heredoc doesn't output anything

I'm writing a script to send SQL output in mail, but it is not executing successfully and is not generating the output I want.
The query generates two columns with multiple rows. How can I generate the output in table format as below?
Below is my code:
#!/bin/bash
ORACLE_HOME= **PATH
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH
TNS_ADMIN= ** PATH
export TNS_ADMIN
today=$(date +%d-%m-%Y)
output=$(sqlplus -S user/pass#service <<EOF
set heading off;
SELECT distinct list_name ,max(captured_dttm) as Last_received FROM db.table1
group by list_name having max(captured_dttm) <= trunc(sysdate - interval '2' hour);
EOF)
if [ -z "$output" ];
then
echo"its fine"
exit
else
echo "
Dear All,
Kindly check we've not received the list for last 2 hour : $output
Regards,
Team" | mailx -S smtp=XX.XX.X.XX:XX -s "URGENT! Please check list FOR $today" user#abc.com
fi
When using a here document, the closing string can't be followed by anything but a newline. Move the closing parenthesis to the next line:
output=$(sqlplus -S user/pass#service <<EOF
...
EOF
)

split pipe separated string fetched using SQL

My shell script executes a SQL to fetch the data in the following format:
JOB_ID_001|[PROD] This is a mail subject one ${application_date}|a#example.com,b#example.com
JOB_ID_002|[PROD] This is a mail subject two ${application_date}|c#example.com,b#example.com
I want to split this pipe-separated string, but the output looks very odd as follows:
JOB_ID_001[0]
JOB_ID_001[1]
JOB_ID_001[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
one[0]
one[1]
one[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
example.com,b#example.com[0]
example.com,b#example.com[1]
example.com,b#example.com[2]
JOB_ID_002[0]
JOB_ID_002[1]
JOB_ID_002[2]
This[0]
This[1]
This[2]
is[0]
is[1]
is[2]
a[0]
a[1]
a[2]
mail[0]
mail[1]
mail[2]
subject[0]
subject[1]
subject[2]
two[0]
two[1]
two[2]
${application_date}[0]
${application_date}[1]
${application_date}[2]
ple.com,b#example.com[0]
ple.com,b#example.com[1]
ple.com,b#example.com[2]
My desired output is:
JOB_ID_001
[PROD] This is a mail subject one ${application_date}
a#example.com,b#example.com
JOB_ID_002
[PROD] This is a mail subject two ${application_date}
c#example.com,b#example.com
So that I can continue with those strings.
My shell script is as follows:
email_configs=(`sqlplus -silent $DB_CONN <<-EOF
whenever sqlerror exit 1 oserror exit oscode
set heading off feedback off echo off verify off pagesize 0
$sql_subject_of_mail;
exit;
EOF`)
for i in "${!email_configs[#]}"
do
email_config=${email_configs[i]}
IFS='|' read -r -a email_config_array <<< "$email_config"
job_id=$email_config_array[0]
subject_of_mail=$email_config_array[1]
to_mail_id=$email_config_array[2]
echo $job_id
echo $subject_of_mail
echo $to_mail_id
done
I checked some alternate solutions from this page, but in the output ${application_date} part is missing or there is some other problem.
Can anyone have an idea about my mistake?
The $email_configs array is not being set correctly. It's using spaces as the array delimiters, not newlines.
Rather than set an array, read the output of sqlplus in a loop.
while IFS='|' read -r job_id subject_of_mail to_mail_id
do
echo "$job_id"
echo "$subject_of_mail"
echo "$to_mail_id"
done < <(sqlplus -silent $DB_CONN <<-EOF
whenever sqlerror exit 1 oserror exit oscode
set heading off feedback off echo off verify off pagesize 0
$sql_subject_of_mail;
exit;
EOF
)

Shell script to fetch sql query data in csv file

Need to extract the below query data along with header in csv file using shell script.
Below is the query.
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj<>0
Could please let me know if below approach to fetch data using shell script is correct and script is correct.
#!/bin/bash
file="output.csv"
sqlplus -s username/password#Oracle_SID << EOF
SPOOL $file
select 'SourceIdentifier','SourceFileName','ProfitCentre2','PlantCode',
'tax_retur ReturnPeriod','document_number DocumentNumber','TO_CHAR(invoice_generation_date,'YYYY-MM-DD') Docume','Original','customer_name CustomerName','NVL(sns_pos,new_state_code)POS','PortCode','NEW_HSN_CODE HSNorSAC','(SGSATE+UTGSATE) Stat','(SGS+UT)StateUT','Userde' from dual
Union all
select 'TO_CHAR(SourceIdentifier)','TO_CHAR(SourceFileName)','TO_CHAR(ProfitCentre2)','TO_CHAR(PlantCode)',
'TO_CHAR(tax_retur ReturnPeriod)','TO_CHAR(document_number DocumentNumber)','TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume','TO_CHAR(Original)','TO_CHAR(customer_name CustomerName)','TO_CHAR(NVL(sns_pos,new_state_code)POS)','TO_CHAR(PortCode)','TO_CHAR(NEW_HSN_CODE HSNorSAC)','TO_CHAR((SGSATE+UTGSATE) Stat)','TO_CHAR((SGS+UT)StateUT)','TO_CHAR(Userde)' from
(SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj<>0)
SPOOL OFF
EXIT
EOF
In short: the ; is missing from the end of the select statement.
Some unrequested advice:
I think spool will put extra stuff into your file (at least some new lines), a redirect is better, further the first line is not db-related:
echo "SourceIdentifier;SourceFileName;ProfitCentre2..." > $file
I recommend to generate the csv format right in the select query, later it will be more headache (you can escape there what you want):
$query = "select SourceIdentifier || ';' || SourceFileName || ';' || ProfitCentre2 ... ;"
So querying the DB (I think capital -S is the right one) plus for the formatting of the records (and maybe you want to format your columns too):
sqlplus -S username/password#Oracle_SID >> $file << EOF
set linesize 32767 pagesize 0 heading off
$query
EOF
For me this one is working but one empty line before first query and second query is coming. Empty line remove using awk command
#!/bin/bash
FILE="A.csv"
$ORACLE_HOME/bin/sqlplus -s username/password#Oracle_SID<<EOF
SET PAGESIZE 50000 COLSEP "," LINESIZE 20000 FEEDBACK OFF HEADING off
SPOOL $FILE
select 'TYPE_OF_CALL_V','SWITCH_CALL_TYPE_V','RECORD_TYPE_V','TARF_TYPE_V' from dual;
SELECT TYPE_OF_CALL_V,SWITCH_CALL_TYPE_V,RECORD_TYPE_V,TARF_TYPE_V FROM TABLE;
SPOOL OFF
EXIT
EOF
awk 'NF > 0' $FILE > out.txt
mv out.txt $FILE

write bash function to drop user using sqlplus

I have written a small bash script to delete some rows from a table and drop some users using sqlplus. When I put the code in the function it is giving "syntax error: unexpected end of file" error message. Below is the code. Please let me know how to fix it.
function reset_db
{
sqlplus user1/password1#${input} << eof
set timing off
set serveroutput on size 10000
set feedback off
spool logfile_$input.out
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
commit;
exit
eof
sqlplus dba_usr/dba_password#${input} << eof
set timing off
set serveroutput on size 10000
set feedback off
spool logfile_$input.out
drop user ABC cascade;
drop user DEF cascade;
drop user HIG cascade;
commit;
exit;
}
You're missing the eof at the end of your second sqlplus command. Change this:
exit;
}
to this:
exit;
eof
}
Incidentally, you don't actually need to call sqlplus two separate times; you can use its connect command to drop one connection and open a new one:
function reset_db
{
sqlplus user1/password1#${input} << eof
set timing off
set serveroutput on size 10000
set feedback off
spool logfile_$input.out
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
commit;
connect dba_usr/dba_password#${input}
drop user ABC cascade;
drop user DEF cascade;
drop user HIG cascade;
eof
}

How to do sqlplus in bash if else?

I am trying to write a bash function which should delete some entries from table1 if the user enters 1, otherwise remove different entries. My function looks like this:
function reset_db
{
if [ $usr_input == 1 ]
then
sqlplus -s $USR/$pwd#$SID << EOF
delete from table1 where component = 'ABC';
delete from table2 where component = 'ABC';
exit
EOF
else if [ $usr_input == 2 ]
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
exit
EOF
fi
}
I am getting error: syntax error near unexpected token `fi'
I am sure that it is happening because I am using if-else incorrectly somewhere but not able to figure out a way to fix it.
Also please let me know how can I post code under the same thread if I have any more follow up questions.
Your 'else if' is wrong, the correct syntax is 'elif'.
You need to repeat the command in each clause of the if statement:
function reset_db
{
if [ $usr_input == 1 ]
then
sqlplus -s $USR/$pwd#$SID << EOF
delete from table1 where component = 'ABC';
delete from table2 where component = 'ABC';
exit
EOF
elif [ $usr_input == 2 ]; then
sqlplus -s $USR/$pwd#$SID << EOF
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
exit
EOF
fi
}
As a simplification, you should refactor this:
reset_db () {
if [[ $usr_input = 1 ]]; then
to_delete='ABC'
elif [[ $usr_input = 2 ]]; then
to_delete='XYZ'
else
return
fi
sqlplus -s "$USR/$pwd#$SID" <<EOF
delete from table1 where component = '$to_delete'
delete from table2 where component = '$to_delete'
exit
EOF
fi
}

Resources