Results 1 to 4 of 4

Thread: String Length

  1. #1
    Join Date
    Feb 2004
    Location
    Los Angeles, CA
    Posts
    28

    Unanswered: String Length

    Is there a simple way to determine the length of a Variable in a shell?

    I am using Korn in the following example. The code can be a 1 or 2 digit A/N code but must be less than 3 digits A/N:

    #!/bin/ksh

    echo "Enter Code for Product: \c"
    read PC

    if [ -z "$PC" ]
    then
    echo "No code entered..."
    exit 1
    elif [ Here is where I want to check that $PC is less than 3 digits A/N ]
    then
    Do something based on whether it is 1 or 2 digits.
    else # It's 3 digits or more
    exit 2
    fi

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Lightbulb

    You could try this:
    Code:
       len=$(echo $PC|awk '{print length;}')
       if [ len -lt 3 ]
       then
         ...etc...
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Feb 2004
    Location
    Los Angeles, CA
    Posts
    28
    Works great, Thank you =)

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    if (( ${#len} < 3 ))
    then

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •