Unixシェルスクリプトで一連のワイルドカード文字をエスケープする方法は?

Unixシェルスクリプトで一連のワイルドカード文字をエスケープする方法は?

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:] \'\'\`\~\!\@\#\$\%\^\&\*\-\_\{\}\;\"\:\/\.\,\<\>\?\(\)\]\'\)\;
                                              ^

シェルはこのバックティックを閉じるバックティックとして解釈COUNT=し、残りの行をコマンドとして実行しようとします~!@#$%^&*-_{};":/.,<>?()]');

この試み:

COUNT=$(sqlplus -s $USER/$PASS@proddb <<EOF
<snip>
EOF
)

この$()構造は一般的により安全で読みやすい。

おすすめ記事