2

i have a file that has a list of routes to .sh files, but to execute the scripts a date is needed, it the file the date is YYYYMMD. When I run the script that executes the list of scripts, i prompt the user for the date with the readcommand.

My problem is that i need to be 100% sure that the date that the user inputs is at least all numbers, how can i do this? i have already made sure that the maximum chars inputed is 8, so all i need is to only allow the user to input numbers. This is all i have so far.

echo 'Input date (YYYYMMDD)'
read -n 8 date

Any ideas?

Thankyou.

RyPeck
  • 7,830
  • 3
  • 38
  • 58
Juanpe
  • 446
  • 5
  • 16

3 Answers3

1

You should use a while loop :

number=""
while [[ ! $number =~ ^[0-9]{8} ]]; do
    echo Please enter your age
    read number
done

Where ^[0-9]{8} means "8 numbers from 0 to 9"

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Ummm, it doesn't work, gives me a syntax error, i was trying this already, i had tried `while [[ $number != [0-9]{8} ]]` – Juanpe Sep 23 '14 at 09:53
  • `line 50: syntax error at line 52: ~(E)^[0-9]{8} ]]` – Juanpe Sep 23 '14 at 10:32
  • @GaryPerry I can't test it now, you should take a look [here](http://stackoverflow.com/questions/18709962/bash-regex-if-statement) – Thomas Ayoub Sep 23 '14 at 10:47
0

After having some help from @Thomas i have solved my problem. The solution is to compare the input varible to a regular expression. But for some reason doing this directly does not work, so i have saved the regular expression in a variable and then compared the variables.

reg='^[0-9]{8}$'
while [[ ! $number =~ $reg ]]
do
 echo 'The format is wrong'
done

^ <-- The start of the line

[0-9]{8} <-- 8 numbers from 0 to 9

$ <-- The end of the line

=~ <-- Compare with regular expression

Hope this answer helped!

Thankyou

PS: If anybody know why the comparison did not work when doing it directly please let me know.

Juanpe
  • 446
  • 5
  • 16
0

I find an until loop can do this job just fine in many instances.

I found the syntax for the comparison on another forum and it works, while some of the syntax mentioned in here was throwing errors at me. Notice the "..+$" ending and inclusion of the [+-]?.. or [+].. in the case of positive numbers only.

To be honest, I'm still learning, and I don't know why this syntax works for me versus what others have posted here.

Note: this does not work without the -p flag and with -n the interpreter was unhappy.

#!/bin/bash
read -p 'Input date (YYYYMMDD): ' date
until [[ $date =~ ^[+]?[0-9]{8}+$ ]]
do
    echo "Oops! User input was not 8 characters and/or not a positive integer!"
    echo
    read -p 'Input date (YYYYMMDD): ' date
done

echo "User gave a valid input: $date"

Console Output:

Input date (YYYYMMDD): very age
Oops! User input was not 8 characters and/or not a positive integer!

Input date (YYYYMMDD): -19670218
Oops! User input was not 8 characters and/or not a positive integer!

Input date (YYYYMMDD): 670218
Oops! User input was not 8 characters and/or not a positive integer!

Input date (YYYYMMDD): 19670218
User gave a valid input: 19670218
DogeCode
  • 346
  • 2
  • 10