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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17575392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:01:07  来源:igfitidea点击:

How do I test for an empty string in a Bash case statement?

stringbashnullcase

提问by Singlestone

I have a Bash script that performs actions based on the value of a variable. The general syntax of the case statement is:

我有一个 Bash 脚本,它根据变量的值执行操作。case 语句的一般语法是:

case ${command} in
   start)  do_start ;;
   stop)   do_stop ;;
   config) do_config ;;
   *)      do_help ;;
esac

I'd like to execute a default routine if no command is provided, and do_helpif the command is unrecognized. I tried omitting the case value thus:

如果没有提供命令,并且do_help命令无法识别,我想执行默认例程。我尝试省略 case 值,因此:

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

The result was predictable, I suppose:

结果是可以预见的,我想:

syntax error near unexpected token `)'

Then I tried using a regex:

然后我尝试使用正则表达式:

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

With this, an empty ${command}falls through to the *case.

有了这个,一个空洞${command}就落到了这个*案子上。

Am I trying to do the impossible?

我是在尝试做不可能的事吗?

回答by rici

The casestatement uses globs, not regexes, and insists on exact matches.

case语句使用 globs,而不是正则表达式,并坚持完全匹配。

So the empty string is written, as usual, as ""or '':

所以空字符串像往常一样写成""or ''

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

回答by Ernie

I use a simple fall through. no parameters passed ($1="") will be caught by the second case statement, yet the following * will catch any unknown parameter. Flipping the "") and *) will not work as *) will catch everything every time in that case, even blanks.

我使用简单的跌倒。没有传递的参数 ($1="") 将被第二个 case 语句捕获,但以下 * 将捕获任何未知参数。翻转 "") 和 *) 将不起作用,因为 *) 在这种情况下每次都会捕获所有内容,甚至是空白。

#!/usr/local/bin/bash
# testcase.sh
case "" in
  abc)
    echo "this  word was seen."
    ;;
  "") 
    echo "no  word at all was seen."
    ;;
  *)
    echo "any  word was seen."
    ;;
esac

回答by Thomas Altfather Good

Here's how I do it (to each their own):

这是我的做法(对他们自己的):

#!/bin/sh

echo -en "Enter string: "
read string
> finder.txt
echo "--" >> finder.txt

for file in `find . -name '*cgi'`

do

x=`grep -i -e "$string" $file`

case $x in
"" )
     echo "Skipping $file";
;;
*)
     echo "$file: " >> finder.txt
     echo "$x" >> finder.txt
     echo "--" >> finder.txt
;;
esac

done

more finder.txt

If I am searching for a subroutine that exists in one or two files in a filesystem containing dozens of cgi files I enter the search term, e.g. 'ssn_format'. bash gives me back the results in a text file (finder.txt) that looks like this:

如果我要搜索存在于包含数十个 cgi 文件的文件系统中的一个或两个文件中的子例程,我会输入搜索词,例如“ssn_format”。bash 将结果返回到一个文本文件 (finder.txt) 中,如下所示:

-- ./registry/master_person_index.cgi: SQLClinic::Security::ssn_format($user,$script_name,$local,$Local,$ssn) if $ssn ne "";

-- ./registry/master_person_index.cgi: SQLClinic::Security::ssn_format($user,$script_name,$local,$Local,$ssn) if $ssn ne "";