- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to validate a parameter as a date
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 09:16 AM
тАО12-06-2000 09:16 AM
How can I validate the parameter is a valid date. Is there a shell function to compare the input string with a date format.
Or, if there is none, how can I validate if the parameter is a valid number. Just do a test like "if [ $1 -lt 10000000 -o $1 -gt 99999999 ] gave me an error if the parameter contain alphanumerics (2000DEC06).
Thanks for your help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 10:42 AM
тАО12-06-2000 10:42 AM
Re: How to validate a parameter as a date
Given the format of your 'date' parameter, take it and concatenate "0000" to the end of it; 'touch' a file using the resultant string as a timestamp; and examine the return status. If the exit status of the 'touch' is zero, then the 'date' was valid. Thusly:
# ARG=${ARG}0000
# touch -c -m -t $ARG /tmp/$$
# if [ "$?" = 0 ]
# then
# echo "date_OK"
# else
# echo "invalid date: $ARG"
# fi
Note that the '-c' argument to 'touch' prevents creating any file that you have to cleanup.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 10:53 AM
тАО12-06-2000 10:53 AM
Re: How to validate a parameter as a date
Thanks,
Madhu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 11:04 AM
тАО12-06-2000 11:04 AM
Re: How to validate a parameter as a date
Anyway, I'll take care of it. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 11:27 AM
тАО12-06-2000 11:27 AM
Re: How to validate a parameter as a date
Ah, yes. You are correct, the 'touch' only verifies that the day of the month is between 1-and-31 inclusive. Hence a "month/day" of "0431" [also erroneous] would pass without further analysis. My method is clearly a "first approximation" of correctness.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-06-2000 04:46 PM
тАО12-06-2000 04:46 PM
Re: How to validate a parameter as a date
From an L1000 running 11.00...
sandbox:/>echo ":" | at -t 200104310000
warning: commands will be executed using /usr/bin/sh
job 988639200.a at Tue May 1 00:00:00 2001
sandbox:/>
That looks like a bug to me. What do you people think?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-07-2000 12:07 AM
тАО12-07-2000 12:07 AM
Re: How to validate a parameter as a date
I think writting a little C program serving the purpose would be an efficient one than touch or at, and it's not too hard.
Right?
Regargs.
Thinphony!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-07-2000 03:04 AM
тАО12-07-2000 03:04 AM
Re: How to validate a parameter as a date
Here is a small C program, based on the getdate function.
Check 'man getdate' for further personalized input formats.
--- cut here
#include
#include
main(argc,argv)
int argc;
char **argv;
{
struct tm *checked_date;
if(argc != 2) {
fprintf(stderr,"Usage: $0 date_string\n");
exit(1);
}
if((checked_date=getdate(*++argv))==NULL){
fprintf(stderr, "Wrong date format: %s\n", *argv);
fprintf(stderr, "getdate_err = %d\n", getdate_err);
exit(1);
}
else
fprintf(stderr, "Like this date: %s\n", *argv);
}
--- cut here
Personalised format should be in a file pointed to the environment variable DATEMSK which, in your case, should contain the following 2 lines:
--- cut here
%Y%b%d
%Y%m%d
--- cut here
Formats accepted are for example:
2000DEC09
20001209
As getdate() checks for date validity, all input like april 31st or february 29 2001 will be rejected.
Modify the file pointed to by DATEMSK according to your needs if you want more input formats.
The c program is writing error to stderr, it should be quite straightforward to modify as well.
Best regards,
Dan
PS: This is a 'quick and dirty' check only, as I'm just verifying if getdate() is returning NULL or not. ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-07-2000 03:07 AM
тАО12-07-2000 03:07 AM
SolutionMore dirty than quick.... I forgot the exit(0) in case all went well.
Here is the modified source:
#include
#include
main(argc,argv)
int argc;
char **argv;
{
struct tm *checked_date;
if(argc != 2) {
fprintf(stderr,"Usage: $0 date_string\n");
exit(1);
}
if((checked_date=getdate(*++argv))==NULL){
fprintf(stderr, "Wrong date format: %s\n", *argv);
fprintf(stderr, "getdate_err = %d\n", getdate_err);
exit(1);
}
else {
fprintf(stderr, "Like this date: %s\n", *argv);
exit(0);
}
}
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2000 09:35 AM
тАО12-08-2000 09:35 AM
Re: How to validate a parameter as a date
--- Cut here ---
#include
#include
int main(int argc, char **argv)
{
struct tm *checked_date;
if(argc != 2)
{
fprintf(stderr,"\tUsage: check_date [date]\n\n");
return (1);
}
if((checked_date=getdate(*++argv))==NULL)
{
/* error: Invalid date or format */
return (1);
}
return (0);
}
--- End cut ---
Also, I remove unwanted fprintf to be able to use it into a sh script as a boolean result:
--- Cut Here ---
#!/usr/bin/sh
if [ ${#} = 1 ] ; then
if check_date ${1}
then
echo "Continue process"
else
echo "Invalide date. Stop process."
exit
fi
fi
--- End Cut ---