I have the following test script using $() to capture the return values from a
SQLPLUS command. I also want to capture the return code to identify if an
error occurred. Running this on my Mac and Linux box called Dorado, all works
well, but the same script on SunOS box called Blenny it does not (see error
below). But if I change the $() to backticks ` then it works.
I was under the impression backticks and $() syntax were essentially the same.
Can someone help to understand the differences? Personally I like the $()
syntax more then the backticks. The backticks sometimes get lost when looking
through a bunch of code.
hmmm.. I was also looking at my return code, it does not change from 0. If I
put a syntax error in the query, like rename user_tables to xzyz, it still
returns 0. I guess showing that the sqlplus command finished successfully but
did not transfer the exit value to $?. I was hoping to get those two separate
values. For example, in the Blenny test below, I expect to see the text
"Database Error [1]" before printing the return value text. Any ideas?
Thanks
Eric
>cat t3.ksh
#!/usr/bin/env ksh
# ========== ========== ========== ========== ========== ========== ==========
set +o xtrace +o verbose -o nounset
# ---------- ---------- —————
# where am I going and what am I looking for?
# ---------- ---------- ----------
typeset conn="eric/$...@$2"
typeset search="LOG"
integer count=3
# ---------- ---------- —————
# Run the query
# ---------- ---------- —————
typeset rv=$(sqlplus -SILENT -RESTRICT 2 -LOGON /NOLOG << !!eosql
SET ECHO OFF
WHENEVER SQLERROR EXIT 1
WHENEVER OSERROR EXIT 2
CONNECT ${conn}
SET FEEDBACK OFF VERIFY OFF TIME OFF TIMING OFF
COLUMN tbl_nm FORMAT A20 HEADING 'Table|Name'
SELECT
LOWER( table_name ) AS tbl_nm
FROM
user_tables
WHERE
table_name LIKE '%${search}%' AND
ROWNUM <= ${count};
EXIT;
!!eosql
)
# ---------- ---------- —————
# remove any SQLPlus header stuff
# ---------- ---------- ----------
integer rc=$?
rv=$( print "${rv}" | grep -v "onnected" \
| grep -v "Copyright" \
| grep -v "With the Partitioning" \
| grep -v "SQL\*Plus: Release" )
# ---------- ---------- —————
# What did we find?
# ---------- ---------- ----------
(( rc > 0 )) && print -u2 print "\n***> database error : [${rc}]\n"
print -- "===================="
print "Return Value:\n${rv}"
print -- "===================="
exit
# ========== ========== ========== ========== ========== ========== ==========
>uname -a
Linux dorado 2.6.18-92.1.13.el5 #1 SMP Wed Sep 24 19:33:52 EDT 2008 i686 i686
i386 GNU/Linux
>print ${.sh.version}
Version M 93s+ 2008-01-31
>./t3.ksh xxxx zzzz
====================
Return Value:
Table
Name
--------------------
alertlogs
trans_log
trans_log_dd
====================
>uname -a
SunOS blenny 5.9 Generic_122300-07 sun4u sparc SUNW,Sun-Fire-V210 Solaris
>print ${.sh.version}
Version M 1993-12-28 s+
>./t3.ksh xxxx zzzz
====================
Return Value:
table_name LIKE "%${search}%" AND
*
ERROR at line 6:
ORA-00904: "%${search}%": invalid identifier
====================
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users