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
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
[[ ... ]] 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
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
My favorite way of doing that matching is
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.