I have this function that works for a co-worker, and I've seen where it works for others as well. I have tried both << EOF, and <
I have added a simple use of the function as well for testing purposes.
td_query() { bteq << EOF |grep '^>' |sed -e "s/^>//"
$(cat $HOME/.tdlogon)
DATABASE DBNAME;
.set width 1000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
td_query "select current_date;"
Unfortunately, when I save this and try to run the ksh file, I get the following error:
-ksh: .: syntax error: `<<' unmatched
Can anyone tell me what would cause this?
You should avoid spaces both at <<EOF and the line starting with EOF.
And the line with EOF shouldn't have trailing spaces either.
First test with a simple script (no idents before EOF !).
td_query() {
wc <<EOF
$(cat $HOME/.tdlogon)
DATABASE DBNAME;
.set width 1000;
.set titledashes off;
$1
.LOGOFF;
.QUIT;
.EXIT
EOF
}
td_query "select current_date;"
And check the bteq program. Is het reading from stdin?
When I replace "wc" by "echo" in the script above, the script stops working!
Related
I have a function that, in a loop, calls another function that executes an sql.
If the sql is a file, it only executes the 1st sql, but, if the sql is embedded, it finishes the loop. Example:
function conciliateFile {
### NOT WORKING
sqlplus -S -L ${BBDD_CHAIN} # ${HOME}/tmp/prueba.sql
### WORKING
#sqlplus -S -L ${BBDD_CHAIN} <<EOF
#set serveroutput on size 1000000
#set linesize 350
#DECLARE
#result_code VARCHAR2(4);
#result_description VARCHAR2(500);
#BEGIN
#DBMS_OUTPUT.PUT_LINE('HELLO');
#END;
#\
#exit;
#EOF
}
funcion mainFunction {
NUM_FILES=$(find ${SQL_PATH} -type f -name "${PATTERN}*sql" |wc -l)
COUNTER=1
find ${SQL_PATH} -type f -name "${PATTERN}*sql" | sort -t"_" -k 2 -n | while read CURRENT_TMP_SQL_FILE; do
CURRENT_TMP_SQL_OUT_FILE=${CURRENT_TMP_SQL_FILE}_out
CURRENT_TMP_SQL_ERR_FILE=${CURRENT_TMP_SQL_FILE}_err
echo "Conciliating file ${COUNTER} out of ${NUM_FILES}"
conciliateFile
COUNTER=$(expr ${COUNTER} + 1)
}
# Main
#split files
mainFunction
And the output would be something like this:
NOT WORKING (executing sql file)
"Conciliating file 1 out of 3"
WORKING OPTION (executing embedded sql)
"Conciliating file 1 out of 3"
"Conciliating file 2 out of 3"
"Conciliating file 3 out of 3"
Any suggestions on this issue???
Your script prueba.sql must end with an exit - otherwise sqlplus will not terminate execution
I have a strange problem - possibly I'm just going blind. I have this short script, which replaces the string #qry# in the here-document with a select statement in a file and then pipes it to mysql:
#!/bin/bash
if [[ "$1" == "-h" ]]
then
echo "sqljob [sqlfile] [procnm] [host] [database] [config file]"
echo " sqlfile: text file containing an SQL statement"
echo " procnm: name that will given to the new, stored procedure"
echo " host: hostname of IP address of the database server"
echo " database: the procedure will be created here"
echo " config file: default configuration file with username and password"
exit
fi
infile=$1
procnm=$2
hn=$3
pn=$4
db=$5
mycfg=$6
{
set -o noglob
sed -e "s/#qry#/$(echo $(cat $infile))/g" <<!
drop procedure if exists $procnm;
delete from jobs where jobname="$procnm";
insert into jobs
set
notes="SQL job $procnm",
jobname="$procnm",
parm_tmpl='int';
delimiter //
create procedure $procnm(vqid int)
begin
call joblogmsg(vqid,0,"$procnm","","Executing #qry#");
drop table if exists ${procnm}_res;
create table ${procnm}_res as
#qry#
end//
delimiter ;
!
} | mysql --defaults-file=$mycfg -h $hn -P $pn $db
However, when the select contains *, it expands to whatever is in the directory even though I use noglob. However, it works from the command line:
$ set -o noglob
$ ls *
What am I doing wrong?
Edit
Block Comments in a Shell Script has been suggested as a duplicate, but as you will notice, I need to expand ${procnm} in the here-doc; I just need to avoid the same happening to select *.
I suspect it is because the construct echo (cat). The echo command gets the * from the cat command and the shell in which it runs expands it. In that shell set noglob is not active.
Try leaving the echo away: /$(cat $infile)/, in the end that is the data you need; then there is no extra glob expansion by a shell.
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
I get a Delimter Error in a Shell Script:
#!/bin/sh
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
;`
EOF
echo $result
#EOF
The result of the Script is fine. But I get two warnings:
./filename.sh: line 13: warning: here-document at line 8 delimited by end-of-file (wanted `EOF')
./filename.sh: line 9: EOF: command not found
What is the problems here? Thank you!
You have the start of your here-doc inside of your command, but the EOF is outside of your command.
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
EOF
`
The ; seems wrong here too (at least it threw an error for me).
I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)