Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

please advice - what's wrong with my syntax ? ( should not print the "bad interface " )

remark - I work with bash shell

ETH_NAME=eth0         ( or any other as eth1 or eth2 .... )
echo $ETH_NAME

eth0 



[ $ETH_NAME  != eth[0-9] ] && echo bad interface
share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

Use the [[ ... ]] compound command which can do pattern matching with !=

$ ETH_NAME=eth0
$ [[ $ETH_NAME != eth[0-9] ]] && echo bad interface
$ ETH_NAME=eth01
$ [[ $ETH_NAME != eth[0-9] ]] && echo bad interface
bad interface
share|improve this answer
1  
Another option would be a regular expression: [[ ! $ETH_NAME =~ ^eth[0-9]$ ]] –  Ansgar Wiechers Oct 14 '12 at 13:20
2  
@AnsgarWiechers: Yes, but you still need to use [[ rather than [ which is what the OPs problem was. –  Iain Oct 14 '12 at 13:48
    
I didn't claim anything different. –  Ansgar Wiechers Oct 14 '12 at 13:56
add comment

Or use the standard (POSIX sh) case statement instead of ksh's [[...]] (also found in bash and zsh).

case $ETH_NAME in
  (eth[0-9]) ;;
  (*) echo >&2 bad interface
esac

Note that it will say that eth10 is a bad interface.

You could instead do

case $ETH_NAME in
  (eth|eth*[!0-9]*) echo >&2 bad interface;;
  (eth*) ;;
  (*) echo >&2 bad interface
esac

(note that network interfaces names including ethernet ones are not limited to ethx).

share|improve this answer
    
This one is my favorite, since it's fast and /bin/sh-compatible –  poige Oct 14 '12 at 14:57
    
Note that it's not /bin/sh compatible (as some /bin/sh like the Bourne shell still found on some systems are not POSIX and don't accept that syntax) but it is POSIX sh compatible so would work in any POSIX conformant shell. –  sch Oct 14 '12 at 15:49
    
Actually it is, but I have no idea why you use opening (. Every manual from BSD's till Solaris have mentions of case…esac but uses pattern) list syntax. –  poige Oct 14 '12 at 16:37
    
Yes, the leading "(" is POSIX but not Bourne. It is more legible, helps with bracket matching in vi, avoid parsing issues in some shells inside $(...). It is supported by all POSIX shells and IIRC the Almquist shell (even the original one before it was made POSIX), so basically all Bourne-like shells but the Bourne shell. It breaks Bourne compatibility. –  sch Oct 14 '12 at 19:13
    
Ok, bracket matching is cool thing, but ability to not require /bin/bash and be more portable is of a higher importance (as to me). ) –  poige Oct 14 '12 at 19:18
show 2 more comments

My favorite way of doing that matching is case…esac:

case "$ETH_NAME" in
    eth[0-9]) ;;
    *) echo 'bad interface' ;;
esac

since it's fast and rather portable (doesn't require using bash). I can't say that subset of regexps available for case is a rich one, but often it's sufficient.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.