1

I need to execute the following Oracle SQL in unix crontab. The query is as follows:

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

I have set escape characters before each wildcard, but still I am getting error. So I have written the crontab where I select the count first. But I am getting error again and again. Following are the relevant contents from my 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 

The error I am getting is:

checkcount[13]: ~!@#$%^&*-_{};":/.,<>?()]');: not found. Nothing to Worry 

    1 Answer 1

    1

    The error indicates the problem is here:

    REGEXP_LIKE(tran_particular,'[^[:alnum:] \'\'\`\~\!\@\#\$\%\^\&\*\-\_\{\}\;\"\:\/\.\,\<\>\?\(\)\]\'\)\; ^ 

    The shell interprets this backquote as closing the one in COUNT=, and tries to run the remaining of the line as a command, ~!@#$%^&*-_{};":/.,<>?()]');.

    Try this:

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

    The $() construct is usually safer (and easier to read).

    5
    • Hi I am still getting the same error.
      – Mistu4u
      CommentedFeb 24, 2016 at 10:17
    • Getting error as [YOU HAVE NEW MAIL] In Checkcount checkcount[13]: ~!@#$%^: not found. checkcount[13]: *-_{}: not found. checkcount[13]: ":/.,: not found. Nothing to Worry You have mail in /usr/spool/mail/rbladmin
      – Mistu4u
      CommentedFeb 24, 2016 at 10:17
    • Relevant code: function checkcount { echo "In Checkcount" COUNT=$(sqlplus -s $USER/$PASS@finratna <<EOF set pagesize 0 set escape on 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 }`
      – Mistu4u
      CommentedFeb 24, 2016 at 10:18
    • You still have to escape the special chars! (They look like they're not escaped in your comment: is this true?) The error message shows the line has been broken on &, ;, < which all have special meaning for the shell.
      – L. Levrel
      CommentedFeb 25, 2016 at 11:59
    • Hi, I avoided the error by keeping the SQL query separate from the .sh file . Rather I called the SQL file @. So now it is working fine. Thanks for your comments although.
      – Mistu4u
      CommentedFeb 25, 2016 at 15:45

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.