代码之家  ›  专栏  ›  技术社区  ›  Guy Sadoun

如何匹配bash中的文件扩展名?[副本]

  •  0
  • Guy Sadoun  · 技术社区  · 8 年前

    我只想在文件名以 .request 但不知何故它调用函数 request 子字符串 .

    marked with red and green 进口零件,右边是输出

    for file in ${filesOrDir[*]}; do    
        if [[ -f "$file" ]]; then
            if [[ "$file"=*[".request"] ]]; then
                # Enters here when .request is a substring.
            fi
        fi
        if [[ -d "$file" ]]; then
                # ... some logics       
        fi
    done
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Dennis Vash    8 年前

    尝试使用: [[ $file == *.request ]]

    handle-request.sh 是用于检查文件名的帮助脚本。

    handle-request.sh文件:

    while IFS='' read -r line || [[ -n "$line" ]]; do
        if [[ $line == *.request ]]; then
            echo $line
        fi
    done < "$1"
    

    说明: reference

    IFS='' (或IFS=)防止删除前导/尾随空白。

    -r

    || [[ -n $line ]] 如果最后一行不以 \n EOF ).

    文件:

    hello.request
    .request.hello
    file name with space.request
    

    hello.request
    file name with space.request