Find the answer to your Linux question:
Results 1 to 3 of 3
#cat test.sh if test $1 = '\[1*\]' then echo "Yes is a Matching Pattern" else echo "Not a Matching Patern" fi but this does not match for 1 , 11 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2004
    Posts
    3

    pattern matching - plz guide


    #cat test.sh
    if test $1 = '\[1*\]'
    then
    echo "Yes is a Matching Pattern"
    else
    echo "Not a Matching Patern"
    fi

    but this does not match for 1 , 11 or 111
    it matches foronly 1*

    also tried using the following
    if [ $1 = [1*] ] - - > did not match for 11
    if [ $1 -eq [1*] ] - - > it complains test.sh: line 2: [: [1*]: integer expression expected

    Can any body tell me y the pattern matching is not hapening?

  2. #2
    Linux Guru
    Join Date
    Mar 2003
    Location
    Wisconsin
    Posts
    1,907
    How about

    Code:
    if test $1 = "1*"
    Double quotes instead of single quotes? I think you can safely remove the [] as well.

    Jeremy
    Registered Linux user #346571
    "All The Dude ever wanted was his rug back" - The Dude

  3. #3
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Quote Originally Posted by jeremy1701
    How about

    Code:
    if test $1 = "1*"
    Double quotes instead of single quotes? I think you can safely remove the [] as well.

    Jeremy
    No...you need the [...]. That is the bash construct for conditional testing.

    Try this:

    Code:
    if [ $1 = [1]* ]; then
         echo "It matches!";
    else
        echo "No match!";
    fi
    Are you trying to match any combination of 1's? This will only match a 1 with anything after it at all. I'm not sure how you would go about matching a pattern of 1's in bash, but I'll research it.

    Edit: That doesn't work, either. I don't know how to go about doing this....
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

Posting Permissions

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