Samah
April 12, 2017, 9:45am
1
Hi,
I wrote a function for the first time and not able to get the desired result.
I have requirement to execute 10 queries. For this i wrote a function like below.
function Command {
typeset var SOL;
if [ ${1:-0} -ne 0 ]; then CONNECTION="${CONNECTION} -e -time";
else SOL="`nzsql ${CONNECTION} -c "${1}"`";
fi;
echo "${SOL}";
}
CHECK1="$(Command "SELECT COUNT(*) FROM ${DATABASE}..EMPLOYEE WHERE NVL(EMPLOYEE_ID, 0) <= 0;")"
if [ ${?} -ne 0 ] ; then
echo "CHECK1 : ${CHECK1}";
fi;
Above code is throwing an error.
.[16]: Command[10]: [: COUNT(*): unknown operator
I need to put the check 1 result in a file and need to attach the file to email and send once all the 10 checks performed.
Could any one please tell me what went wrong in this function!?
Aia
April 12, 2017, 10:49am
2
Your double quotes are not doing what's intended.
Samah
April 13, 2017, 4:15am
3
Thanks for the response Aia.
You mean to say, there is no problem with defined function and only the double quotes are causing issue?
Even i tried without quotes, it is not working.
The $() allows quite normal shell code.
But this one is problematic:
if [ ${1:-0} -ne 0 ]
$1 must be in quotes, and comparing it with a number looks odd.
This one looks more appropriate:
if [ -z "$1" ]
This is not correct, they are in fact doing what is intended.
The input characters within the quoted string that are also enclosed between "$(" and the matching ')' shall not be affected by the double-quotes, but rather shall define that command whose output replaces the "$(...)" when the word is expanded. The tokenizing rules in Token Recognition , not including the alias substitutions in Alias Substitution , shall be applied recursively to find the matching ')'.
Shell Command Language: Double-Quotes
In fact, the command is equivalent to:
CHECK1=$(Command "SELECT COUNT(*) FROM ${DATABASE}..EMPLOYEE WHERE NVL(EMPLOYEE_ID, 0) <= 0;")
Samah
April 14, 2017, 2:24am
6
Many Thanks MadeInGermany,
if [ -z "$1" ]
Above one is working.
Thanks Scrutinizer, for a detailed Double- Quotes explanation.
Thanks again both of you!
Dear Samah,
You are trying to program with many levels of language at a time.
I suggest:
A) use set -x to debug
B) Try one level at a time
C) Try this command line:
eval $(Command "SELECT SYSDATE FROM DUAL")
D) If it works, try:
CHECK1=$(eval $(Command "SELECT SYSDATE FROM DUAL") )
E) The error parameter is $?, or ${?}; $(?) does not work.
Regards.
@AbellLuis , what is the sense of introducing "eval" here. I do not understand your statement that $(?) does not work, when no-one was suggesting that. Besides , the OP has already suggested that he found an answer in the thread..
@Scrutinizer
<<what is the sense of introducing "eval" here.>>
The function Command build the name of a call to SQL, but don't execute it; so it need to be parsed and executed. "eval" does it.
<< I do not understand your statement that $(?) does not work, when no-one was suggesting that.>>
The code quoted by the initiator, had this, may be a typegraphical mistake, or a visual one of mine.
<< Besides , the OP has already suggested that he found an answer in the thread.>>
It is my alternative answer, that's all.
Regards.
1 Like