Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2005
    Posts
    23

    Unanswered: Compare first characters of a string in UNIX shell

    Hello all!

    I want to verify if the first 2 characters of a string are "+-".
    Can you please help me with that? How can I compare if the varaible (let's say P1 begins with "+-" )


    Thanks in advance,
    Chris

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

    Talking


    Try this:
    Code:
    [[ `echo $P1|cut -c1-2` == '+-' ]] && echo 'Yes.'

    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Aug 2005
    Posts
    23
    Thanks a lot for your help!

    Chris

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    case "$p1" in
      +-*) echo "Start with +-" ;;
        *) echo "Doesn't start with +-" ;;
    esac

  5. #5
    Join Date
    Aug 2005
    Posts
    23
    Hi!

    In fact pdreyer's solution is the one I have chosen already!

    Thank you all!

  6. #6
    Join Date
    Dec 2006
    Posts
    1
    Oh, I'm having a similar problem, I need to compare 2 or more characters (letters) to find out which is the largest or lowest one.

    For example, I enter each letters one by one, like a, b, c, d, e, so the largest one is e and the lowest one is a.

    How can I do that?

  7. #7
    Join Date
    Aug 2005
    Posts
    23
    Hi!

    I think you should parse the letters array and store the lowest.
    For example

    min="ZZZ" //or the first object of the array

    for i in letters_array
    do
    if [[ $i < $min ]]
    then
    min=$i
    fi
    done


    Hope that helps!
    Chris

Posting Permissions

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