2

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
yael
  • 2,433
  • 5
  • 31
  • 43

3 Answers3

5

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
user9517
  • 115,471
  • 20
  • 215
  • 297
2

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).

sch
  • 560
  • 4
  • 13
  • 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
  • No. it's not what I said. POSIX now defines "sh". What it also says is that the POSIX "sh" might not be in "/bin" and that the behavior when a script starts with "#!" is _unspecified_. `case...in (..)` is perfectly fine in a `sh` script, simply not necessarily in a `/bin/sh` script. Just don't write `/bin/sh` scripts, just scripts without a `#!` line (and avoid Solaris). – sch Oct 14 '12 at 19:24
  • Before being a POSIX shell, `/bin/sh` used to be a Bourne shell (and still is in Solaris which is the only notable exception), but before that it was even something completely different. There's not point trying to be portable as far back as the Thomson shell, and IMO there's no point being portable as far back as the Bourne shell either. – sch Oct 14 '12 at 19:29
1

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.

poige
  • 9,448
  • 2
  • 25
  • 52