Scripting issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting issue
# 8  
Old 05-09-2016
Hi RavinderSingh, but my result wants start date to be less then end date

---------- Post updated at 03:06 PM ---------- Previous update was at 02:49 PM ----------

My apologies for not making the question clear.

Here it goes>>
I am having a problem. which I have described below>>

I have to run a script with the format : <File_name><Start_date><End_date>

example:
Code:
abcd.sh <01-MAY-2014> <01-JAN-2016>

but if I intentionally give an argument like >>
example:
Code:
abcd.sh <01-MAY-2014> <01-JAN-2012>

,
it should throw an error like "start date is more than end date"
I want a code which actually does so



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 05-09-2016 at 06:44 AM.. Reason: Added code tags (again!)
# 9  
Old 05-09-2016
Not clear. You have been given two examples of how to achieve the result you desire. Where now exactly are you stuck?

BTW, are those < and > part of the function parameters or not?
# 10  
Old 05-09-2016
Hi RudiC,
I would definately look into it from the next time
# 11  
Old 05-09-2016
Hello Chandan_Bose,

Still it is not clear but did some changes in script in case you need exact reason in output, then it may help you in same.
Code:
cat script.ksh
START_DATE=$1
END_DATE=$2;
if [[ -n $1 && -n $2 ]]
then
        awk -vstart_date="$START_DATE" -vend_date="$END_DATE" 'BEGIN{
                                                                        num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
                                                                        for(i=1;i<=num;i++){
                                                                                                Q[array[i]]=i
                                                                                           };
                                                                        split(start_date, A,"-");
                                                                        split(end_date, B,"-");
        if(A[3] < B[3]){VAL_YEAR="START date year is less than END date year."}
        if(A[3] > B[3]){VAL_YEAR="START date year is greater than END date year."}
        if(A[3] == B[3]){VAL_YEAR="START date year is same as END date year."}
        if(Q[toupper(A[2])] < Q[toupper(B[2])]){VAL_MONTH="START date month is before than END date month."}
        if(Q[toupper(A[2])] > Q[toupper(B[2])]){VAL_MONTH="START date month is after than END date month."}
        if(Q[toupper(A[2])] == Q[toupper(B[2])]){VAL_MONTH="START date month is same as END date month."}
        if(A[1] < B[1]){VAL_DATE="START date is before than END date."}
        if(A[1] > B[1]){VAL_DATE="START date is after than END date."}
        if(A[1] == B[1]){VAL_DATE="START date is same as END date."}
        if(A[3] <= B[3] && Q[toupper(A[2])] <= Q[toupper(B[2])]){
        if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
                                print "start_date " start_date " seems to be less than " end_date " end_date."
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }                                  '
else
        echo "One of the argument is seems to be missing while running the script."
fi

So here is an example when we run script with a scenario as follows.
Code:
./script.ksh 29-MAY-2015 21-May-2015
Date is NOT in proper format because of following values:
START date year is same as END date year.
START date month is same as END date month.
START date is after than END date.

If above code doesn't meet your requirements then please port sample Input_file with expected output and with all the conditions you wanted in your requirement so that we could understand it more clearly.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-09-2016 at 08:51 AM.. Reason: Correct spelling here.
This User Gave Thanks to RavinderSingh13 For This Post:
# 12  
Old 05-09-2016
Hi RavinderSingh,

Certainly your code served my purpose

---------- Post updated at 05:39 PM ---------- Previous update was at 03:33 PM ----------

Code:
#!/usr/bin/bash
clear
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8



if [ $# -lt 2 ]  ; then
        echo " Incorrect Number of Arguments";
        echo " Usage : Main_Script <FROM_DATE> <TO_DATE>";
        echo " Example : Main_Script 21-JUL-2015 30-JUL-2015";
        exit;
		
fi;

if [ $# == 2 ]; then
      if [[ $1 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9] ]]; then
	      if  [[ $2 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9] ]]; then 
		  START_DATE=$1
          END_DATE=$2;
          if [[ -n $1 && -n $2 ]]
          then
                 awk -vstart_date="$START_DATE" -vend_date="$END_DATE" 'BEGIN{
                                                                        num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
                                                                        for(i=1;i<=num;i++){
                                                                                                Q[array[i]]=i
                                                                                           };
                                                                        split(start_date, A,"-");
                                                                        split(end_date, B,"-");
        if(A[3] > B[3]){VAL_YEAR="START date year is greater than END date year."}
        if(Q[toupper(A[2])] > Q[toupper(B[2])]){VAL_MONTH="START date month is after than END date month."}
        if(A[1] > B[1]){VAL_DATE="START date is after than END date."}
        if(A[3] <= B[3] && Q[toupper(A[2])] <= Q[toupper(B[2])]){
        if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
                                echo "REPORT FROM " $1 "TO "$2;
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }                                  '
else
        echo "One of the argument is seems to be missing while running the script."
                            fi;
						 echo "Incorrect date format";
		                 echo  $1;
                         echo "Usage : DD-MMM-YYYY";
		                 echo " Example :  Main_Script 21-JUL-2015 30-JUL-2015";
                        exit;
		              fi 
		   else
		   echo "Incorrect date format";
		   echo  $1;
          echo "Usage : DD-MMM-YYYY";
		  echo " Example :  Main_Script 21-JUL-2015 30-JUL-2015";
		  exit;
       fi; 
	   else
	    echo "Incorrect date format";
		echo  $2;
        echo "Usage : DD-MMM-YYYY";
		echo " Example :  Main_Script 21-JUL-2015 30-JUL-2015";
		exit;
	
		 
    fi;

Whenever I'm running the code , with the following inputs ./testingscript.sh 21-JUL-2015 21-JUN-2016

The message which it is throwing is >>
"Date is NOT in proper format because of following values:"

---------- Post updated at 05:51 PM ---------- Previous update was at 05:39 PM ----------

And then it is running into an infinite loop, instead of giving perfect input examples. Can I have a justification as to why is this happening
# 13  
Old 05-09-2016
Hello Chandan_Bose,

Would like to request you if you are merging our code(provided by anyone in here forum members) into a large code(which we do not know your complete requirement and you haven't mentioned into post too), we could not give explanation for it like why it is not working, until/unless we go through your code completely(I had tested my script provided in pervious posts and is working fine for me). Though I have gone through your script and tried my best to correct it, please try it and let me know how it goes(Though I haven't tested it).
Code:
 #!/usr/bin/bash
clear
 export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
if [ $# -lt 2 ]  ; then
        echo " Incorrect Number of Arguments";
        echo " Usage : Main_Script <FROM_DATE> <TO_DATE>";
        echo " Example : Main_Script 21-JUL-2015 30-JUL-2015";
        exit;
 fi;
 if [ $# == 2 ];
then
      if [[ $1 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9]  && $2 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9] ]]
      then
                  START_DATE=$1
                  END_DATE=$2;
                                                                        awk -vstart_date="$START_DATE" -vend_date="$END_DATE" 'BEGIN{
                                                                        num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
                                                                        for(i=1;i<=num;i++){
                                                                                                Q[array[i]]=i
                                                                                           };
                                                                        split(start_date, A,"-");
                                                                        split(end_date, B,"-");
        if(A[3] > B[3]){VAL_YEAR="START date year is greater than END date year."}
        if(Q[toupper(A[2])] > Q[toupper(B[2])]){VAL_MONTH="START date month is after than END date month."}
        if(A[1] > B[1]){VAL_DATE="START date is after than END date."}
        if(A[3] <= B[3] && Q[toupper(A[2])] <= Q[toupper(B[2])]){
        if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
                                echo "REPORT FROM " $1 "TO "$2;
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }
        else               {
                                print "Date is NOT in proper format because of following values:"
                                print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
                           }
                           }                                  '
                  else
                                 echo "Incorrect date format";
                                 echo  $1;
                                 echo "Usage : DD-MMM-YYYY";
                                 echo " Example :  Main_Script 21-JUL-2015 30-JUL-2015";
                                 exit;
                  fi
        else
                                 echo "One of the argument is seems to be missing while running the script."
        fi

EDIT: Also adding a code with less if-else conditions by creating a function. This is only enhancement of my code in previous post.
Code:
START_DATE=$1
END_DATE=$2;
if [[ -n $1 && -n $2 ]]
then
        awk -vstart_date="$START_DATE" -vend_date="$END_DATE" '
                                          function check(Q,W,T, VAL)
                             {
                                if(Q < W)VAL="START date " T " is less than END date " T
                                if(Q > W)VAL="START date " T " is greater than END date " T
                                if(Q == W)VAL="START date " T " is equal to END date " T
                                return VAL
                             }
                                          function print1(A1,B1,C1)
                             {
                                print A1 ORS B1 ORS C1
                             }
                                                                BEGIN{
                                                                        num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
                                                                        for(i=1;i<=num;i++){
                                                                                                Q[array[i]]=i
                                                                                           };
                                                                        split(start_date, A,"-");
                                                                        split(end_date, B,"-");
        VAL_YEAR=check(A[3], B[3],"YEAR");
        VAL_MONTH=check(Q[toupper(A[2])], Q[toupper(B[2])],"MONTH");
        VAL_DATE=check(A[1], B[1],"DATE");
        if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
                                print "start_date " start_date " seems to be less than " end_date " end_date."
                           }
        else               {
                                print1(VAL_YEAR,VAL_MONTH,VAL_DATE);
                           }
                           }                                  '
else
        echo "One of the argument is seems to be missing while running the script."
fi

After running the script as above following are the different results.
Code:
 ./script.ksh 22-MAY-2015 21-May-2015
Date is NOT in proper format because of following values:
START date YEAR is equal to END date YEAR
START date MONTH is equal to END date MONTH
START date DATE is greater than END date DATE

 ./script.ksh 21-MAY-2015 21-May-2015
Date is NOT in proper format because of following values:
START date YEAR is equal to END date YEAR
START date MONTH is equal to END date MONTH
START date DATE is equal to END date DATE
  
 ./script.ksh 20-MAY-2015 21-May-2015
start_date 20-MAY-2015 seems to be less than 21-May-2015 end_date.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-09-2016 at 11:40 AM.. Reason: Adding a enhanced form of my solution here for user.
# 14  
Old 05-09-2016
Pure bash approach:
Code:
IFS=- read D1 M1 Y1 D2 M2 Y2 <<< $1-$2
MTMP=$(locale abmon)
MTMP=${MTMP^^}
L1=${MTMP%%$M1*}
L2=${MTMP%%$M2*}
(( $(printf "%04d%02d%02d < %04d%02d%02d\n" $Y2 $((${#L2}/4+1)) $D2 $Y1 $((${#L1}/4+1)) $D1) ))  && echo "start later than end"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting issue

Hi Experts, I'm stuck with a single liner bash script. Need your help to fix it. Let me brief you that what is my expectations from this script. I need the interfaces configured for a list of servers, their respective IP address and FQDN in a output file. The output should be in the same... (3 Replies)
Discussion started by: arun_adm
3 Replies

2. Shell Programming and Scripting

Scripting Issue

Hi All, I am facing a problem when i am trying to run shell script more than 5 times. I have shell script(.sh file) which ran perfectly fine in the early attempts(1,2,3,4 runs). But if i try to run the script more number of times, i am facing the below error message. Too many ('s I do... (3 Replies)
Discussion started by: am24
3 Replies

3. Shell Programming and Scripting

Scripting Issue

needing this script to shut down 1 IceS and start up another, close the 2nd one after 12 seconds and then reboot. here is what i have so far #!/bin/bash ShutDown() { echo "Shutdown in progress." wall <<ENDOFWALL CI Shutdown has been intiated!!!! Shutdown will occur in 30 seconds...... (1 Reply)
Discussion started by: Zaine
1 Replies

4. Shell Programming and Scripting

Scripting Issue

I am having an issue with a script that I created today, my first attempt at this, and was wondering if anyone can give me insight to what changes need to be made. Below is a copy of the script that I have written. WEe are trying to monitor whether or not a services is running. I do have a cron... (1 Reply)
Discussion started by: lsudubee
1 Replies

5. Shell Programming and Scripting

Scripting issue

Hello, I have an ASCII file (many files of the same format, but different dates and numbers) in the format like below: 2008.01.02,08:00,1.46520,1.46520,1.46410,1.46440,70 2008.01.02,08:05,1.46450,1.46560,1.46440,1.46540,79 2008.01.02,08:10,1.46530,1.46540,1.46490,1.46500,46... (8 Replies)
Discussion started by: chief2000
8 Replies

6. Shell Programming and Scripting

scripting issue

Hello, Guys I am having a sql script file which contains some sql statements including inserting values, One column is of the data type date. Now i am having a KSH script for inserting values via this script into the database. The problem I am facing that when I am inserting value in the... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

7. Shell Programming and Scripting

IP-Scripting Issue

Hi Guys, I am quite new to Shell Scripting... I need ur help.. This is very urgent. The thing is like, I need to match a IP address (ex 192.168.200.56) i.e, xxx.xxx.xxx.xx inside a KSH script,but if we enter in different format other than the specified format (ex jjj.ksj., 1.0...), it should... (3 Replies)
Discussion started by: mraghunandanan
3 Replies

8. Shell Programming and Scripting

Scripting issue..

Hi guys... I am newbie to Shell Scripting... I am querying the Oracle database.I want a Shell script for fetching some data from datasets in Oracle database and displaying in an excel sheet.This is my requirement.. Can u plz help me guys.. Regards, Mahesh... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

9. Shell Programming and Scripting

Scripting issue..

Hi guys... I am a newbie to scripting... I have a small requirement... I dont whether u r clear with my requirement... But plz try to help me... Like, tell me some kind of scripts that can help me in retreiving the data from the datasets. Regards, Mahesh... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

10. Shell Programming and Scripting

scripting issue

folks; i'm trying to write a shell script to do the following: 1. i have a file with 39 fields, i'm trying to add 10 more fields with (!) as a field separator. with the following requirement: if field number 20 has a value, then field number 40 will show as (!M!), and if the field number 20 does... (2 Replies)
Discussion started by: moe2266
2 Replies
Login or Register to Ask a Question