我需要在 unix crontab 中执行以下 Oracle SQL。查询如下:
select count(*)
from tbaadm.htd
where cust_id is not null and
pstd_flg = 'Y' and
del_flg = 'N' and
tran_date = (select db_stat_date-1 from tbaadm.gct) and
REGEXP_LIKE(tran_particular,'[^[:alnum:] ''`~!@#$%^&*-_{};":/.,<>?()]');
我在每个通配符之前设置了转义字符,但仍然收到错误。所以我编写了首先选择计数的 crontab。但我一次又一次地收到错误。以下是我的 crontab 中的相关内容:
. $HOME/.profile
function dbconstants
{
USER="user"
PASS="password"
MAIL_BODY_PATH="/home/admin/CRONTAB_SHELL/"
MAIL_BODY=$MAIL_BODY_PATH"mail.txt"
}
function checkcount
{
COUNT=`sqlplus -s $USER/$PASS@proddb <<EOF
#connect $USER/$PASS@proddb;
set pagesize 0
SET ESCAPE '\'
select count(*)
from tbaadm.htd
where cust_id is not null and
pstd_flg = 'Y' and
del_flg = 'N' and
tran_date = (select db_stat_date-1 from tbaadm.gct) and
REGEXP_LIKE(tran_particular,'[^[:alnum:] \'\'\`\~\!\@\#\$\%\^\&\*\-\_\{\}\;\"\:\/\.\,\<\>\?\(\)\]\'\)\;
EOF`
echo $COUNT
echo $COUNT | sed 's/^[ \t]*//;s/[ \t]*$//' |& read -p COUNT1
}
function fetchdetails
{
`sqlplus -s $USER/$PASS@finratna <<EOF >$MAIL_BODY
set feed off pages 0 trimspool on
set pagesize 60
set linesize 9999
set trim on
set head off
SET ESCAPE '\'
alter session set nls_date_format='DD-MM-YYYY';
select tran_date|| '|,' ||tran_id|| '|,' ||part_tran_srl_num|| '|,' ||tran_particular|| '|,' ||REGEXP_REPLACE
(tran_particular,'[^[:alnum:] ''\`~!@#$%^&*-_{};":/.,<>?()]','') reg_par
from tbaadm.htd
where cust_id is not null and
pstd_flg = 'Y' and
del_flg = 'N' and
tran_date = (select db_stat_date-1 from tbaadm.gct) and
REGEXP_LIKE(tran_particular,'[^[:alnum:] ''\`~!@#$%^&*-_{};":/.,<>?()]');
EOF`
}
function deletefile
{
rm -f $MAIL_BODY
}
dbconstants
checkcount
if [ "$COUNT" -gt 0 ]
then
fetchdetails
else
echo "Nothing to Worry"
fi
deletefile
我收到的错误是:
checkcount[13]: ~!@#$%^&*-_{};":/.,<>?()]');: not found.
Nothing to Worry
答案1
该错误表明问题出在这里:
REGEXP_LIKE(tran_particular,'[^[:alnum:] \'\'\`\~\!\@\#\$\%\^\&\*\-\_\{\}\;\"\:\/\.\,\<\>\?\(\)\]\'\)\;
^
shell 将此反引号解释为关闭 中的反引号COUNT=
,并尝试将该行的其余部分作为命令 运行~!@#$%^&*-_{};":/.,<>?()]');
。
尝试这个:
COUNT=$(sqlplus -s $USER/$PASS@proddb <<EOF
<snip>
EOF
)
该$()
结构通常更安全(并且更易于阅读)。