linux - 我的模式匹配出了什么问题?

标签 linux bash shell

我的目录中的文件

262_V01_C00_R000_TEx_BL_2048H.dat  262_V01_C02_R000_THx_BL_2048H.dat
262_V01_C01_R000_TEy_BL_2048H.dat  262_V01_C03_R000_THy_BL_2048H.dat

我的脚本

#!/bin/bash

for i in *.dat;
do
    if [ "$i" == "$TEx" ];then
        file1= "$i"
    fi
    if [ "$i" == "$TEy" ];then
        file2= "$i"
    fi
done

echo file1
echo file2

我得到了

file1
file2

我的目标是

file1=262_V01_C00_R000_TEx_BL_2048H.dat
file2=262_V01_C00_R000_TEy_BL_2048H.dat

最佳答案

尝试:

  • [[ "$i"=~ TEx ]] 而不是 [ "$i"== "$TEx"]

  • file1=$i= 周围没有空格。另外,双引号不是必需的。

  • echo file1=$file1 不仅仅是 echo file1

关于linux - 我的模式匹配出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38858969/

相关文章:

linux - 加密 config.properties 文件中的密码

shell - 管道 qrencode 到 eog 以避免创建文件

ruby - 如何从 ruby​​ 在远程主机上执行交互式 shell 程序

python - 通过 shell 脚本调用 python 脚本时出现 ImportError

linux - Linux 接入点客户端之间的流量

linux - 使用 ffmpeg 将视频转换为 MP4 (H.264/AAC)

bash - 如何在 macOS 中永久使 ffmpeg 可从 bin/bash 中识别

bash - 从 AWS Lambda 创建 AMI

linux - 在 linux 中替换文件的字符串

android - 尝试在 Linux Ubuntu 上使用 Android systrace.py 时出错