Avatar of Swaminathan_K
Swaminathan_KFlag for India

asked on 

help on unix script

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;

Linux OS Dev

Avatar of undefined
Last Comment
Swaminathan_K
Avatar of farzanj
farzanj
Flag of Canada image

Sorry, could you point out the area in script where you are having trouble.  Please clarify
Avatar of Swaminathan_K
Swaminathan_K
Flag of India image

ASKER

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;

Avatar of Swaminathan_K
Swaminathan_K
Flag of India image

ASKER

The aboce code does not work when I do not pass any value from the unix script. Any work around for the same.
Avatar of farzanj
farzanj
Flag of Canada image

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.
Avatar of Swaminathan_K
Swaminathan_K
Flag of India image

ASKER

hi farzanj,

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


Avatar of farzanj
farzanj
Flag of Canada image

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.
Avatar of Swaminathan_K
Swaminathan_K
Flag of India image

ASKER

the format of the date is : mmddyy eg :170511 for 17th of May 2011
Avatar of farzanj
farzanj
Flag of Canada image

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

Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

The first line of code should be

   for date in "$fromdate" "$todate"
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Swaminathan_K
Swaminathan_K
Flag of India image

ASKER

Made some changes to my code , as mentioned . It worked.  Awesome..
Linux OS Dev
Linux OS Dev

Kernel programming for Linux operating systems can be done with many different languages; C, C++, Python, Perl and Java, which are some of the most common languages used.There are also many different varieties of Linux, such as Ubuntu, Fedora and OpenSUSE.

10K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo