2017-07-28 60 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 
相關問題