Schließen Sie ähnliche Dateinamen mithilfe eines Shell-Skripts aus einem Array aus

Schließen Sie ähnliche Dateinamen mithilfe eines Shell-Skripts aus einem Array aus

Ich habe ein Array von Dateien, Beispiel file=[a.yml, a.json,b.yml,b.json]. Ich iteriere mithilfe foreiner Schleife. Ich muss die .jsonDateien von der Ausführung ausschließen, wenn sie sowohl .ymloder .yamlals auch .jsonim Array enthalten sind. Wenn ich aber nur .jsonim Array habe (Beispiel [a.json,b.json]), muss es durch die Schleife laufen. Ist das mit einem Shell-Skript möglich?

Grundsätzlich versuche ich, die Zeichenfolgen in einem Array zu vergleichen und die Duplikate dynamisch auszuschließen.

Ist dies mit Shell möglich?


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'''

Antwort1

Sie können prüfen, ob Sie den anderen Wert haben, bevor Sie fortfahren:Überprüfen, ob ein Bash-Array einen Wert enthält.

Sie können auch nur die Dateien im Array behalten .jsonund später in der Schleife die Existenz der .ymlDatei prüfen.

bashhat dafür einen schönen Parameterersatz:

${parameter%word}

Remove matching suffix pattern.

In Ihrem Fall würde es ungefähr so ​​aussehen (ohne das .json) .yml:

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

Antwort2

Versuchen Sie etwas wie Folgendes:

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

verwandte Informationen