Bash script accepting variables in valid number format
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash script accepting variables in valid number format
Hi Experts
I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have like of a "Month-Day" valid in number format. I would like to have an if-then statement that would validate if the variables passed are in correct, something like these:
Code:
#!/bin/bash
# This script will ONLY accept two parameters in number format
# according to "MM-DD" which is "XX-XX"
# To run this script: ./script.sh xx-xx xx-xx
DATE1=$1
DATE2=$2
if [DATE1 is in correct format] && [DATE2 is in correct format]
then
echo "Correct format"
echo "DATE1 = $DATE1"
echo "DATE2 = $DATE2"
else
echo "Not correct format"
exit 1
fi
ersan-poguita is correct. Members should not assume something is homework unless there is a direct reference one can cite. Meanwhile someone could be performing self study from problems in a guide. This is all fine, the LQ policy on these matters is as follows:
Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
It is helpful to see the added regular expressions you have tried to solve your dilemma. If I were to have greater REGEX experience I would offer some input here. All I can say at this point would be to break down the problem to check one date at a time and suggest doing a simple if-statement using the command line only to validate various test cases, such as 10-01, 01-10, 13-01, and so forth to verify that it detects valid date strings.
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
Which the OP didn't do.
A quick google search turns up a close answer and with a much better regex that can cope with things like the month being in the range 01 to 12 and the day being in the range 01 to 31 which is easily implemented in bash.
Code:
Test Case 1 - One valid date
[tmp]$ ./date.sh 01-01
INVALID DATES!
Test Case 2 - Two valid dates
[tmp]$ ./date.sh 01-01 10-01
Valid Dates
date1=01-01
date2=10-01
Test Case 3 - One date, bad month
[tmp]$ ./date.sh 14-01
INVALID DATES!
Test Case 4 - Two dates, one with bad month
[tmp]$ ./date.sh 14-01 01-01
INVALID DATES!
Test Case 5 - Two dates, one with invalid day
[tmp]$ ./date.sh 01-32 01-01
INVALID DATES!
Not going to write your script for you, but here's the regex I used.
Code:
^(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$
Note that the regex only checks that the month and day are valid numbers in the ranges it doesn't check for invalid dates such as 30th of February.
The regex doesn't look bad. I just tried it, and it works. But there is no command called "exit1" - you probably meant "exit 1".
This doesn't actually check if the dates are valid. If you know the year of the dates, you could add it as prefix, and pass it to "date -d". Then it will even check the 29th of february correctly, when the year is a leap year:
The regex doesn't look bad. I just tried it, and it works. But there is no command called "exit1" - you probably meant "exit 1".
This doesn't actually check if the dates are valid. If you know the year of the dates, you could add it as prefix, and pass it to "date -d". Then it will even check the 29th of february correctly, when the year is a leap year:
For example this is valid:
date -d 2016-02-29
But not this:
date -d 2018-02-29
Hi Guttorm,
you meant to say the script I posted with the regex on the if-then-else worked on you? thanks for the correction on the exit code. I already adjusted it
Yes, I tested it. Maybe not all cases, but I read it as 1 or 2 digits, followed by - then 1 or 2 digits. But as I said, it only checks if it's digits, not really valid dates.
After the digit test, a test with a year prefix and the date command will be a real test.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.