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


[ $ETH_NAME  != eth[0-9] ] && echo bad interface

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
  • 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. – user9517 Oct 14 '12 at 13:48
  • I didn't claim anything different. – Ansgar Wiechers Oct 14 '12 at 13:56

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

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

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

  • 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

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

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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