Friday, October 22, 2010

shell pattern matching

The problem is that the asterisks can match anything,
 even the commas used as delimiters. However,
 if you add the delimiters to the pattern, you can
 no longer match the ends of the list:

list=orange, apple, banana
case $list in
*, orange, *)        echo "The only fruit for which there is no Cockney slang. "; ;
esac

[no output]

To resolve this, wrap the list in an extra set of delimiters when expanding it:
list=orange, apple, banana
case , $list, in
*, orange, *)        echo "The only fruit for which there is no Cockney slang. "; ;
esac
The only fruit for which there is no Cockney slang.

No comments:

Blog Archive