如何在Bash case语句中测试一个空字符串?

我有一个Bash脚本,根据变量的值执行操作。 case语句的一般语法是:
case ${command} in
   start)  do_start ;;
   stop)   do_stop ;;
   config) do_config ;;
   *)      do_help ;;
esac

如果没有提供命令,我想执行认例程,如果命令无法识别,则执行do_help。我试图省略case值这样:

case ${command} in
   )       do_default ;;
   ...
   *)      do_help ;;
esac

结果是可预测的,我想:

Syntax error near unexpected token `)'

然后我试着用正规表达式使用我最好的镜头:

case ${command} in
   ^$)     do_default ;;
   ...
   *)      do_help ;;
esac

有了这个,一个空的$ {command}落到了* case。

我想做不可能的事吗?

case语句使用globs,而不是regexes,并坚持使用完全匹配。

所以空字符串像往常一样被写为“”或“’:

case "$command" in
  "")        do_empty ;;
  something) do_something ;;
  prefix*)   do_prefix ;;
  *)         do_other ;;
esac

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...