LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-24-2018, 07:54 AM   #1
ersan-poguita
LQ Newbie
 
Registered: May 2018
Posts: 6

Rep: Reputation: Disabled
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
 
Old 05-24-2018, 08:19 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
We're not here to do your homework for you, what have you tried so far and where are you having problems.
 
1 members found this post helpful.
Old 05-24-2018, 08:28 AM   #3
ersan-poguita
LQ Newbie
 
Registered: May 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
yes I did, but didn't worked well using regex, I actually tried this

Code:
#!/bin/bash
date1=$1
date2=$2

if [[ $date1 =~ ^[0-9]{1,2}-[0-9]{1,2}$ ]] && [[ $date2 =~ ^[0-9]{1,2}-[0-9]{1,2}$ ]]
 then
  echo "VALID DATES"
  echo "date1=$date1"
  echo "date2=$date2"
 else
  echo "INVALID DATES!"
  exit1
fi
PS: I was actually expecting a friendly reply from my 1st inquiry here but was not, specially from a senior member... so unprofessional

Last edited by ersan-poguita; 05-24-2018 at 10:51 AM.
 
Old 05-24-2018, 08:41 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@TenTenths,

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.
 
2 members found this post helpful.
Old 05-24-2018, 08:58 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
meh! Looks like homework to me.....
Quote:
Originally Posted by rtmistler View Post
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.
 
2 members found this post helpful.
Old 05-24-2018, 09:07 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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
 
Old 05-24-2018, 09:14 AM   #7
ersan-poguita
LQ Newbie
 
Registered: May 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
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
 
Old 05-24-2018, 09:22 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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.
 
1 members found this post helpful.
Old 05-24-2018, 09:28 AM   #9
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
And the regex posted by TenTenths is a little better. It will reject clearly invalid dates like 99-99, but it will still accept dates like 02-30
 
1 members found this post helpful.
Old 05-24-2018, 09:37 AM   #10
ersan-poguita
LQ Newbie
 
Registered: May 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
thanks all, I would say at this point TenTenths suggestion worked on my test.
 
Old 05-24-2018, 10:03 AM   #11
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Ok. If you want a better test that will fail dates like 04-31 or 02-30, you could do something like this:

Code:
if $(date -d "2016-$date1" &>/dev/null) ; then ...
And if you don't know the year, you could simply leave it out so it's "this year":

Code:
if $(date -d "$date1" &>/dev/null) ; then
Then 02-29 will be valid if this year is a leap year.
 
1 members found this post helpful.
Old 05-24-2018, 10:50 AM   #12
ersan-poguita
LQ Newbie
 
Registered: May 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Ok. If you want a better test that will fail dates like 04-31 or 02-30, you could do something like this:

Code:
if $(date -d "2016-$date1" &>/dev/null) ; then ...
And if you don't know the year, you could simply leave it out so it's "this year":

Code:
if $(date -d "$date1" &>/dev/null) ; then
Then 02-29 will be valid if this year is a leap year.
thanks Guttorm, I'll try this out
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash - variables and adding another number to the variable Springs Linux - Newbie 3 02-10-2017 04:16 PM
[SOLVED] Is this valid in bash script new_user3085 Programming 4 09-12-2016 06:35 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
Counting the number of exit variables - Bash. 0bfuscated Programming 3 07-02-2010 11:43 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration