I'M trying to write a small program that will validate weather or not a string is valid. Assuming that the date will always be 8 digits (12/21/2012) I need a function that will count the number of digits in a string. What I've got below is only returning "'is not 8 digits' even when I am inputing 8 digits.

! /bin/bash
This is the calender array for the DateValidation program

calender=(31 28 31 30 31 30 31 31 30 31 30 31)

read -p "Enter a date for validation: " Date

set the variable "LEN" to hold the lengh of the input number

LEN=$(echo ${Date})

if [ $LEN -lt 8 ]; then

echo "$Date' is 8 characters'"

else "$Date' is not 8 characters'"

fi

END

hi,

no need of a function, bash parameter substitution can do it:

read -p' enter date: ' Date
test ${#Date} -eq 8 || s=" not"
echo "$Date is$s 8 chars"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.