2017-07-28 54 views
1

我有一个带有ipa文件的文件夹。我需要通过在文件名中使用appstoreenterprise来识别它们。正则表达式与文件路径中的名称不匹配

mles:drive-ios-swift mles$ ls build 
com.project.drive-appstore.ipa         
com.project.test.swift.dev-enterprise.ipa 
com.project.drive_v2.6.0._20170728_1156.ipa      

我已经试过:

#!/bin/bash -veE 

fileNameRegex="**appstore**" 

for appFile in build-test/*{.ipa,.apk}; do 
if [[ $appFile =~ $fileNameRegex ]]; then 
    echo "$appFile Matches" 
else 
    echo "$appFile Does not match" 
fi 
done 

但是没有匹配项:

mles:drive-ios-swift mles$ ./test.sh 
build-test/com.project.drive-appstore.ipa Does not match 
build-test/com.project.drive_v2.6.0._20170728_1156.ipa Does not match 
build-test/com.project.test.swift.dev-enterprise.ipa Does not match 
build-test/*.apk Does not match 

如何将正确的脚本修改成相匹配build-test/com.project.drive-appstore.ipa

+0

尝试' fileNameRegex = “* AppStore的。*”'。 – Phylogenesis

回答

1

你在混淆glob字符串匹配与正则表达式匹配。对于贪婪的水珠比赛就像*你可以使用测试运营商,==

#!/usr/bin/env bash 

fileNameGlob='*appstore*' 
#   ^^^^^^^^^^^^ Single quote the regex string 

for appFile in build-test/*{.ipa,.apk}; do   
    # To skip non-existent files 
    [[ -e $appFile ]] || continue 

    if [[ $appFile == *${fileNameGlob}* ]]; then 
     echo "$appFile Matches" 
    else 
     echo "$appFile Does not match" 
    fi 
done 

产生的结果

build-test/com.project.drive_v2.6.0._20170728_1156.ipa Does not match 
build-test/com.project.drive-appstore.ipa Matches 
build-test/com.project.test.swift.dev-enterprise.ipa Does not match 

(或)使用正则表达式使用贪婪匹配.*作为

fileNameRegex='.*appstore.*' 
if [[ $appFile =~ ${fileNameRegex} ]]; then 
    # rest of the code 

这就是说你的原始要求符合ma TCH enterpriseappstore字符串中的文件名使用扩展水珠比赛中bash

使用水珠:

shopt -s nullglob 
shopt -s extglob 
fileExtGlob='*+(enterprise|appstore)*' 

if [[ $appFile == ${fileExtGlob} ]]; then 
    # rest of the code 

,并用正则表达式,

fileNameRegex2='enterprise|appstore' 
if [[ $appFile =~ ${fileNameRegex2} ]]; then 
    # rest of the code 
+1

感谢您如何跳过不存在的文件。非常有帮助! – mles

1

您可以使用下面的正则表达式匹配的AppStore和企业在一个文件名:

for i in build-test/*; do if [[ $i =~ appstore|enterprise ]]; then echo $i; fi; done 
相关问题