使用 shell 腳本從陣列中排除類似的檔名

使用 shell 腳本從陣列中排除類似的檔名

我有一系列文件,例如file=[a.yml, a.json,b.yml,b.json]。我正在使用for循環進行迭代。當數組中同時存在or和 時,我需要排除.json檔案的執行。但如果我只在數組中(例如,它需要通過循環。這可以用 shell 腳本實現嗎?.yml.yaml.json.json[a.json,b.json]

基本上我試圖比較數組中的字串並動態排除重複項。

這可以用 shell 實作嗎?


filename=$(git show --pretty="format:" --name-only $CODEBUILD_RESOLVED_SOURCE_VERSION)
echo "$filename"
mkdir report || echo "dir report exists"
for file in ${filename}; do
    echo ${file}
    ext=${file##*.}
    if [ $ext == "yaml" ] || [ $ext == "yml" ] || [ $ext == "json" ]; then
        if [ ${file} != "buildspec.yml" ] && [ ${file} != "stackupdatebuildspec.yml" ] && [ ${file} != "specs.json" ]; then
            stack=$(echo ${file} | cut -d "." -f 1)
            stackName="${stack//[\/]/-}"
            echo ${stackName}
            howmany() { echo $#; }
            numOfFilesValidated=$(howmany $listOfFilesToScan)
            echo "=========================================== Syntax validation started =============================================================="
            cfSyntaxLogFile="cf-syntax-validation-output"
            numOfFailures=0
            numOfValidatedFiles=0
            for file_to_scan in $listOfFilesToScan; do
                if [[ $(cfn-lint -t "$file_to_scan" --parameter-values-path "${stack}.json" --append-rules ./append_rules --override-spec ./over_ride_spec/spec.json |& tee -a $cfSyntaxLogFile) == "" ]]; then
                    echo "INFO: Syntax validation of template $file: SUCCESS"
                    ((numOfValidatedFiles++))
                else
                    echo "ERROR: Syntax validation of template $file: FAILURE"
                    ((numOfFailures++))
                fi
            done'''

答案1

您可以在繼續之前查看是否有其他值:檢查 bash 數組是否包含值

您也可以僅將.json檔案保留在陣列中,並稍後在循環中檢查.yml檔案是否存在。

bash有一個很好的參數替換:

${parameter%word}

Remove matching suffix pattern.

在你的情況下,它會是這樣的(刪除.json並添加.yml

if [ ! -f "${filename%.json}.yml" ]
then
    # process
fi

答案2

試試這樣的事情:

declare -A fhash

# Load $files array with 'git show -z' - NUL-separated filenames in case
# of spaces, newlines, etc.
mapfile -d '' -t files < <(git show -z --pretty="format:" --name-only "$CODEBUILD_RESOLVED_SOURCE_VERSION")

# build an associative array (hash) from the files array, so we can easily
# check if a matching .yml filename exists for .json and .yaml files.
for f in "${files[@]}"; do
  fhash["$f"]=1
done

# now process each of the filenames in the $files array.
for f in "${files[@]}"; do
  # ignore these filenames
  [[ $f =~ ^(buildspec.yml|stackupdatebuildspec.yml|specs.json)$ ]] && continue

  base=${f%.*}       # get base filename
  ext=".${f##*.}"    # get file's "extension"

  # ignore '.json' and .'yaml' files if there is a matching .yml filename.
  # also ignore .json files if there is a matching .yaml filename
  [[ $ext =~ \.(json|yaml)$ ]] && [ "${fhash[$base.yml]}"  -eq 1 ] && continue
  [[ $ext =~ \.json$        ]] && [ "${fhash[$base.yaml]}" -eq 1 ] && continue

  # The code so far has skipped/ignored all of the files we don't want to
  # process, so you can do whatever you need with "$f".

  # ... your code here ...
done

相關內容