#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9

    Bash string length


    I'M trying to get the numer of digits from an input variable for a date validation program. Below is what I currently have but it's only displaying "is not 8 characters"

    Code:
    #! /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
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Try it like this:
    Code:
    # 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 not 8 characters"
    else
      echo "$Date is 8 characters"
    fi
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    58
    Rep Power
    9
    When I ran what you have it's always returning "$Date is 8 characters" even when I feed it more.


    Originally Posted by spacebar208
    Try it like this:
    Code:
    # 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 not 8 characters"
    else
      echo "$Date is 8 characters"
    fi
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    That's because you have '8' hardcoded in the message!

    Change this line from:
    Code:
    echo "$Date is 8 characters"

    to:
    Code:
    echo "$Date is $LEN characters"

IMN logo majestic logo threadwatch logo seochat tools logo