Bash script accepting variables in valid number format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script accepting variables in valid number format
# 1  
Old 05-24-2018
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

# 2  
Old 05-24-2018
Welcome to the forum.


Very similar problems have been solved umpteen times in these fora. Did you use the search function, or have a look into the links at the bottom left under "More UNIX and Linux Forum Topics You Might Find Helpful"?


Date / time arithmetics is one of the worst tasks in IT, as many subtleties have to be covered / taken care of. It's not just checking we have two two-digit numbers separated by a dash. Certain months allow for certain days only. Do you need leap year dates considered?
You can use numerical comparisons, or string matching, for validations. What be your favorite?
# 3  
Old 05-24-2018
No leap year tests here - would be impossible without knowing the year for each of the dates.

Code:
#!/bin/bash
MDAYS=(0 31 29 31 30 31 30 31 31 30 31 30 31)

function valid-mm-dd ()
{
     MONTH=${1%-*}
     DAY=${1#*-}
     MONTH=${MONTH#0}
     DAY=${DAY#0}

     [[ $MONTH =~ ^[0-9]{1,2}$ ]] || return
     [[ $DAY =~ ^[0-9]{1,2}$ ]] || return
     (( MONTH > 0 && MONTH < 13 )) || return
     (( DAY > 0 && DAY <= MDAYS[MONTH]))  || return
}
DATE1=$1
DATE2=$2

if valid-mm-dd $DATE1 && valid-mm-dd $DATE2
then
   echo "Correct format"
   echo "DATE1 = $DATE1"
   echo "DATE2 = $DATE2"
else
   echo "Not correct format"
   exit 1
fi

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 05-25-2018
@Chubler_XL - suggestion is the answer hereSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl not accepting spaces in script via variables

Hi All, I'm trying to run a script which issues rest commands via curl to an endpoint. If I put spaces in fields via something like insomnia, it works, but when I try from an input file, it's failing with a json error. while IFS=, read mname oname <------ my input file... (10 Replies)
Discussion started by: say170
10 Replies

2. Shell Programming and Scripting

Valid separator in time and date format

Hello. I can use any particular (stupid or not) format when using bash date command. Example : ~> date --date "now" '+%Y-%m-%d %H!%M!%S' 2019-06-03 12!55!33or ~> date --date "now" '+%Y£%m£%d %H¤%M¤%S' 2019£06£03 12¤57¤36 or ~> date --date "now" '+%Y-%m-%d %H-%M-%S' 2019-06-03 12-58-51 ... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid. echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }' ex: ./script.ksh 12:34:21 = okay ./script.ksh... (10 Replies)
Discussion started by: erin00
10 Replies

4. Shell Programming and Scripting

PL/SQL: Specified Number Is Not Valid

Hi I have Unix shell script that invokes PL/SQL procedure. The batch job when executed terminated with the error message:-unlimited: The specified number is not valid for this command.Please let me know what is the root cause of the issue and how to fix the issue. Thanks (1 Reply)
Discussion started by: moonkhan1
1 Replies

5. UNIX for Dummies Questions & Answers

Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you. #!/bin/bash var1=100.25 var2=100.25 if (( $var1 == $var2 )); then echo "Matching" else echo "Not Matching" fi Error: ./number.sh: line... (6 Replies)
Discussion started by: Ariean
6 Replies

6. UNIX for Dummies Questions & Answers

Accepting command line arguments in bash

I have this tcsh code that I want to convert to a bash script. Basically it accepts command line arguments supplied by the user and stores them, so they can be used to run a C++ program. set arg_browseDir_inFileLst = "" set allArgsUpCase = `echo "$*" | tr '' ''` set opt_browseDir_flag =... (17 Replies)
Discussion started by: kristinu
17 Replies

7. Shell Programming and Scripting

how to format a number in bash

Say I have a number x=123, but I want to it be x=000123, because I need to use it in as a file name. thanks! (2 Replies)
Discussion started by: aerosols
2 Replies

8. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

9. Shell Programming and Scripting

How create function valid birthday format dd-mm-yyyy

I write a small shell script create Payroll System my trouble is valid format birthday dd-mm-yyyy when you enter birthday if your value enter not match with format dd-mm-yyyy system will display "Fail Format Please re-enter Birthday with format dd-mm-yyyy" but i don't know how create function... (3 Replies)
Discussion started by: kency
3 Replies

10. Shell Programming and Scripting

Number/string format in bash

I would like to change the format of an integer type number adding zeros to the left of it in a script in bash. For example number=1 echo $number 00001 Thanks (3 Replies)
Discussion started by: josegr
3 Replies
Login or Register to Ask a Question