help on unix script

Swaminathan_K
Swaminathan_K used Ask the Experts™
on
Hi ,

I need help on how to check the value of the input parameters passed from the unix script to the procedure.

The requirement is that , we run the unix script call account_details which accepts two dates as inputs as command line arguments.

eg :./ account_details.sh 01-APR-2011 03-APR-2011

Here we may pass the dates or no dates as inputs to the script
./account_details.sh

In the script we call a stored procedure which acceptstwo dates as parameters. I pass the dates through the unix script to the procedure. But the problem is when I pass the date , Iam able edit that value in the procedure , but when I do not pass the value , Iam unable to check whether it is null or space etc.

Can anyone help me in this regard?



Unix Script:
#!/bin/sh
MON=`/bin/date +%b`
user=sam
pass=sam1
Today=`date +%h-%y`
db=emp

#if [ $# -ne 2 ]; then
#echo "Error : No of Parameters mismatch : scriptname fromdate todate eg: 01-APR-2011"
#exit 1
#fi

fromdate=$2
todate=$3

var=`sqlplus -s $user/$pass@$db <<EOF
set head off
set pagesize 0
set linesize 3000
set feedback off
set echo off
set feed off
set verify off
set trimspool on
#set termout off
set colsep ','
set sqlprompt ''
set echo off
set sqlnumber off
SET SERVEROUTPUT ON;
declare
v_status number(12);
v_errmsg varchar2(4000 byte);
begin
pull_charges (to_date('$fromdate','DD-MON-YYYY'),to_date('$todate','DD-MON-YYYY'),v_status, v_errmsg);
if v_status=1 then
  DBMS_OUTPUT.PUT_LINE('Status=1');
Else
  DBMS_OUTPUT.PUT_LINE ('Status=0'||v_errmsg);
END IF;
END;
/
exit
EOF`

# $var is the returned value as a Unix variable
status=`echo "$var" | cut -c8`

if [ "$status" -eq 0 ]; then  
echo "Success"
exit 0
else
echo $var  
exit 1
fi


Procedure Code:
create oe replace procedure pull_charges (v_fromdate date , v_todate date,v_status out number , v_errmsg out varchar2)
is

begin

---- here I need to add teh code to test whether is null or contains any other value like white spac etc other then dates. If I add the below code i get an errror while compiling
If v_fromdate is null or v_from_date=" ") then
----business logic

end if;
exception
when others then
v_status:=0;
v_errmsg:=SQLCODE || sqlerrm;
end;

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sorry, could you point out the area in script where you are having trouble.  Please clarify

Author

Commented:
In the oracle procedure , how do i check whether the  date is passed is NULL or equal to space?
I tried the below code also,
Procedure Code:
create oe replace procedure pull_charges (v_fromdate date  DEFAULT TRUNC(SYSDATE), v_todate date TRUNC(SYSDATE+1),v_status out number , v_errmsg out varchar2)
is
begin
DBMS_OUTPUT.PUT_LINE(v_fromdate ||'-'||v_todate );

exception
when others then
v_status:=0;
v_errmsg:=SQLCODE || sqlerrm;
end;

Author

Commented:
The aboce code does not work when I do not pass any value from the unix script. Any work around for the same.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Work around is that you should check your values BEFORE making call to the procedure.  If you values are blank or not valid, do NOT pass them to the procedure.  You should check that in the shell commands.  It is not worth calling the procedure with wrong values anyway.  Why not make sure that the values are correct before passing them on?

Try something like
[ "x$var" == "x" ]  && echo "VAR is blank

Are you using Borne shell or Korn?  What is the version?  Actually /bin/sh can be a symlink.  It could be even bash.  You can check by doing ls -l /bin/sh

Depending upon what it is, I can tell you exact command to test your date value.  I also need to know the version.  If it is Korn 87, test syntax will be different.  Korn 93 is easier.  You can use sed or awk to check exact format of your date value and don't make the call to the procedure if the value is bad.  Just end the script gracefully.

Author

Commented:
hi farzanj,

You right  , it is symlink /bin/sh.  The version of ksh we are using is ksh88.


You should be able to do this test.

[ "x$var" = "x" ] &&  echo "Var is null"

If the value of var is blank, this test will be true

You can also write it as
if [ "x$var" = "x" ]
then
    echo "Var is null"
fi

If you could tell me exact format of your date, I can write a test for you that would check if it is exact syntax or not.  If it is not, just terminate the script or do any other corrective actions.

Author

Commented:
the format of the date is : mmddyy eg :170511 for 17th of May 2011
For mmddyy you can use [01][0-9][0-3][0-9][0-9][0-9]
For ddmmyy you can use [0-3][0-9][01][0-9][0-9][0-9]

I am confused because your example and format don't match.  This is a general check.  Please forgive the syntax, ksh88 doesn't accept whole lot of internal regular expressions and I don't want you to waste too much time on it.  The code below check both dates I quickly saw in your code.  If there is a problem, please let me know.


for date in "$fromdate $todate"
do
     if [ $(echo $date |  grep -c "^[0-9][0-9][0-9][0-9][0-9][0-9]$" ) = 0 ]
     then
            echo "Date format not correct"
            exit 1
     fi 
done

Open in new window

The first line of code should be

   for date in "$fromdate" "$todate"
You may also try this one.  I don't have ksh88 right now but it might work, just try this one


if [[ "$fromdate" = "^[0-9][0-9][0-9][0-9][0-9][0-9]$" ]]
then
       echo "Date format incorrect"
       exit 1
fi
if [[ "$todate" = "^[0-9][0-9][0-9][0-9][0-9][0-9]$" ]]
then
       echo "Date format incorrect"
       exit 1
fi

Author

Commented:
Made some changes to my code , as mentioned . It worked.  Awesome..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial