bash + 如何匹配包含字符和数字的字符串

yae*_*ael 2 bash shell-scripting

请建议 - 我的语法有什么问题?(不应该打印“坏界面”)

备注 - 我使用 bash shell

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

eth0 



[ $ETH_NAME  != eth[0-9] ] && echo bad interface
Run Code Online (Sandbox Code Playgroud)

use*_*517 5

使用[[ ... ]]可以进行模式匹配的复合命令!=

$ ETH_NAME=eth0
$ [[ $ETH_NAME != eth[0-9] ]] && echo bad interface
$ ETH_NAME=eth01
$ [[ $ETH_NAME != eth[0-9] ]] && echo bad interface
bad interface
Run Code Online (Sandbox Code Playgroud)

  • @AnsgarWiechers:是的,但您仍然需要使用 `[[` 而不是 `[`,这是 OP 的问题所在。 (2认同)