Unix Script for getting date and validating just Hour


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Script for getting date and validating just Hour
# 1  
Old 05-15-2009
Unix Script for getting date and validating just Hour

Hi,

Can someone guide me to write a unix script for getting a hour out of a date command and validating hour to see if its > 7 and < 16. if hours is >7 and <16 then assign a variable value of 0730 and if hour is >16 then assign a variable value of 1630?

Help appreciated.

Thanks in advance.

Zulfiqar
# 2  
Old 05-15-2009
Quote:
Originally Posted by zulfikarmd
Can someone guide me to write a unix script
sure, how about this for a start? if you read it, it will show you how to use if/else, how to cut strings...etc
# 3  
Old 05-15-2009
Thanks. To start with I wrote this and executed (Not sure though its correct or not):

#!/bin/bash
$HR=date +%H;
if [$HR -gt 7];
if [$HR -lt 16]; then
sendevent -E SET_GLOBAL -G GOMAN_HR=0730
else if [$HR -gt 16];
sendevent -E SET_GLOBAL -G GOMAN_HR=1630
fi
fi
fi

I am getting following error:
./GOMAN_HR.sh: line 2: =date: command not found
./GOMAN_HR.sh: line 8: syntax error near unexpected token `fi'
./GOMAN_HR.sh: line 8: ` fi'

Any help or guidance?

Thanks.
Zulfiqar
# 4  
Old 05-15-2009
try this:
Code:
HR=$(date +%H);

and delete the last "fi".
Regards
# 5  
Old 05-15-2009
That didn't work. Then I tried following:

:/export/home/zdholkawala $ sh -v GOMAN_HR.sh
#!/bin/bash
HR=`date +%H`;
if [$HR -gt 7];
if [$HR -lt 16]; then
sendevent -E SET_GLOBAL -G GOMAN_HR=0730
else if [$HR -gt 16]; then
sendevent -E SET_GLOBAL -G GOMAN_HR=1630
fi
fi
fi
GOMAN_HR.sh: syntax error at line 11: `fi' unexpected

Even removing last fi doesn't work here. Any help?
# 6  
Old 05-15-2009
Quote:
Originally Posted by zulfikarmd
Thanks. To start with I wrote this and executed (Not sure though its correct or not):

#!/bin/bash
$HR=date +%H;
well, if you really read it, you can see this example (4-3) how to assign to variable the output of a command such as ls. it applies to date command as well...pls read it again, from start to finish.
# 7  
Old 05-15-2009
Thanks. Now new error: Smilie

Code:
#!/bin/bash
HR=`date +%H`;
if ["$HR" -gt 6] && ["$HR" -lt 16]; then
  sendevent -E SET_GLOBAL -G GOMAN_HR=`0730`
  exit 0
else if ["$HR" -gt 15]; then
  sendevent -E SET_GLOBAL -G GOMAN_HR=`1630`
  exit 0
  fi
fi

GOMAN_HR.sh: [07: not found
GOMAN_HR.sh: [07: not found

Not sure where its coming from?

Thanks.

Last edited by Franklin52; 05-15-2009 at 08:47 AM.. Reason: adding code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. Shell Programming and Scripting

Day/Hour diff between two date

Dear All I need to find out day diff between two dates. date -d or date -- day is not working in mine system. Currently i am using below code but it gives me wrong value while month change. IP: Date 1: 20150802 11:30:45 Date 2: 20150728 16:30:45 code used: awk '{t1=$2; t2=$4;... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

3. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

4. Shell Programming and Scripting

Generate last hour date in YYYYmmddHH

can some one show me how to generate last hour of date for example: 2012010100 -> 2011123123 // check first date of a new year 2008030100 -> 2008022923 // check leap year as well And also no perl code is restricted. This is what i am facing issue now, can someone provides help to me. (4 Replies)
Discussion started by: alvin0618
4 Replies

5. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

6. Shell Programming and Scripting

Can any one share a unix script for validating the export of a mapping which is in XM

Hi, Can any one share a unix script for validating the export of a mapping which is in XML format. like my requirment is that when ever we are migrating the mappings or wokflows from development to QA we will take the export of that mapping into an XML file.Insted of reviewing the mapping... (1 Reply)
Discussion started by: perlamohan
1 Replies

7. UNIX for Advanced & Expert Users

Can any one share a unix script for validating the export of a mapping which is in XM

Hi, Can any one share a unix script for validating the export of a mapping which is in XML format. like my requirment is that when ever we are migrating the mappings or wokflows from development to QA we will take the export of that mapping into an XML file.Insted of reviewing the mapping... (1 Reply)
Discussion started by: perlamohan
1 Replies

8. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies

9. Shell Programming and Scripting

Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy). How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid. and the todate is optional.so if the todate is not present in the input then i need to assign the... (6 Replies)
Discussion started by: Sharmila_P
6 Replies

10. Shell Programming and Scripting

Validating date in sas

I need some help on sas on unix.... Is there any way to validate date in sas (1 Reply)
Discussion started by: radhika03
1 Replies
Login or Register to Ask a Question