|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi all
For example: ./test.sh 123456 abcedf so I want there's a test mechanism of parameter. for testing if 123456 match rule of input. if not match ,then echo error and exit. so I think using regexp is an idea. and how? thank you very much your key9 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2006-12-07, Key9 wrote:
> Hi all > > For example: > ./test.sh 123456 abcedf > > so I want there's a test mechanism of parameter. > > for testing if 123456 match rule of input. > > if not match ,then echo error and exit. > > > so I think using regexp is an idea. > and how? It depends on what you want to match. If it's a string: if [ "$1" = "teststring" ]; then If it's a file globbing pattern: case $1 in 1*6) ;; ## If $1 starts with 1 and ends with 6 *) exit 5 ;; If not, exit with error esac If it has to be a regular expression, you can use expr, or in some shells (e.g., bash3): if [[ $1 ~ REGEXP ]]; then -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> > if [[ $1 ~ REGEXP ]]; then > oh man , I don't know I can using this directly:-) In my mind , I have to run some app and test it's return. Thank you very much! |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Key9 wrote:
> > > > if [[ $1 ~ REGEXP ]]; then > > > oh man , I don't know I can using this directly:-) It's as non-portable as the ksh variant... if [[ $1 = pattern ]]; then Use 'case' for portability... case $1 in (pattern) ... ; esac Janis > In my mind , I have to run some app and test it's return. > > Thank you very much! |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2006-12-7, 01:36(-08), Janis:
> Key9 wrote: >> > >> > if [[ $1 ~ REGEXP ]]; then >> > >> oh man , I don't know I can using this directly:-) > > It's as non-portable as the ksh variant... > > if [[ $1 = pattern ]]; then > > Use 'case' for portability... > > case $1 in (pattern) ... ; esac [...] case $1 in pattern) ... ; esac will even be portable to the Bourne shell in case you might need that level of portability. -- Stéphane |
|
![]() |
| Outils de la discussion | |
|
|