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 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 [[ ! $ETH_NAME =~ ^eth[0-9]$ ]] [[ rather than [ which is what the OPs problem was. 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).
/bin/sh-compatible /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. (. Every manual from BSD's till Solaris have mentions of case…esac but uses pattern) list syntax. $(...). 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. /bin/bash and be more portable is of a higher importance (as to me). ) 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.