【问题标题】:csh/tcsh How to find if a particular string is in a variable/filename?csh/tcsh 如何查找特定字符串是否在变量/文件名中?
【发布时间】:2021-04-20 12:27:39
【问题描述】:

我想检查一个特定的字符串是否在一个文件名(它是一个变量)中。 [在 csh/tcsh 中]

#!/bin/tcsh
set file = 'KZ.KKAR..BHZ.2008-01-18T19-16-51.SAC'
set string = 'BHZ'
echo $file
if [[$file == *$string*]];then
    echo 'string contains in this'
else 
    echo "NOOOOOOOOO"
endif

类似的东西。 要么 如果可能的话,在一行代码中

【问题讨论】:

    标签: string shell filenames csh tcsh


    【解决方案1】:

    您可以使用=~ 匹配全局模式:

    if ( $file =~ *$string* ) then
    

    注意[[..]]; then 不是有效的 (t)csh 语法;这仅适用于 Bourne shell。

    这记录在man tcsh:

    '==' '!=' '=~' 和 '!~' 运算符将它们的参数作为字符串进行比较;全部 其他人对数字进行操作。运算符 '=~' 和 '!~' 类似于 '!=' 和 '==' 除了右侧是全局模式(请参阅文件名替换) 左手操作数与之匹配。这减少了使用需求 当真正需要所有这些时,shell 脚本中的 switch 内置命令 是模式匹配。

    正如文档所述,switch 也可以使用 glob 模式,类似于 伯恩贝壳。

    【讨论】:

      猜你喜欢
      • 2015-01-22
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多