bash – 我的shell脚本中的逻辑OR

我的剧本:
#!/bin/bash

for file in *.ats;
do
    if [[ ("${file}" = THx) || ("${file}" = THy)]]
    then cp $file /home/milenko/procmt
    fi
done

目录中的文件

262_V01_C00_R000_TEx_BL_128H.ats
262_V01_C01_R000_TEy_BL_128H.ats
262_V01_C02_R000_THx_BL_128H.ats
262_V01_C03_R000_THy_BL_128H.ats

我想要的是复制包含THx或THy的文件,但不复制文件.
为什么?

使用 extglob进行扩展通配怎么样?这样您就可以使用for本身来获取所需的扩展:
shopt -s extglob
for file in *TH?(x|y)*.ats; do
   # do things with "$file" ...
done

* TH?(x | y)*.ats扩展为包含< something>的文件TH x或y< something> .ats

您的脚本失败,因为您有一个拼写错误:

if [[ ("${file}" = THx) || ("${file}" = THy)]]
#                                          ^
#                              missing space

这可以:

$d="hi"
$[[ ($d == hi) || ($d == ha) ]] && echo "yes"
yes

虽然括号是多余的:

$[[ $d == hi || $d == ha ]] && echo "yes"
yes

相关文章

用的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补全...