【问题标题】:how to check whether a string starts with xx and ends with yy in shellscript?如何在shellscript中检查字符串是否以xx开头并以yy结尾?
【发布时间】:2020-11-22 02:20:52
【问题描述】:

在下面的示例中,我想查找句子是否以“ap”开头并以“e”结尾。

示例:a="apple"

if [[ "$a" == ^"ap"+$ ]]

这没有给出正确的输出。

【问题讨论】:

  • 建议为您的特定外壳添加标签,可能是bash 或ksh
  • less-unix 标签有什么用?

标签: shell unix less-unix


【解决方案1】:

您没有提及您使用的是哪个外壳,但您尝试中的 [[ 表明您使用的是在基础 POSIX sh 语言上扩展的外壳。以下至少适用于bash、zsh 和ksh93:

$ a=apple
$ [[ $a == ap*e ]] && echo matches # Wildcard pattern
matches
$ [[ $a =~ ^ap.*e$ ]] && echo matches # Regular expression - note the =~
matches

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2015-04-15
    • 2017-11-12
    相关资源
    最近更新 更多