加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:27:02 所属栏目:安全 来源:网络整理
导读:我的剧本: #!/bin/bashfor file in *.ats;do if [[ ("${file}" = THx) || ("${file}" = THy)]] then cp $file /home/milenko/procmt fidone 目录中的文件 262_V01_C00_R000_TEx_BL_128H.ats262_V01_C01_R000_TEy_BL_128H.ats262_V01_C02_R000_THx_BL_128H.a
我的剧本:
#!/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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读