I am working with the ensemble of the mol2 filles located in the same directory.
structure36S.mol2 structure30S.mol2 structure21.mol2
structure36R.mol2 structure30R.mol2 Structure20R.mol2
structure35S.mol2 structure29R.mol2 Structure19R.mol2
structure35R.mol2 structure28R.mol2 Structure13R.mol2
structure34S.mol2 structure27R.mol2
structure34R.mol2 structure26.mol2 jacks18.mol2
structure33S.mol2 structure25.mol2 5p9.mol2
structure33R.mol2 structure24.mol2 Y6J.mol2
structure32R.mol2 structure23.mol2 06I.mol2
structure31R.mol2 structure22.mol2
From this data I need to make an associative array with the names of the filles (without extension (mol2)) as well as some value (7LMF) shared between all elements:
dataset=( [structure36S]=7LMF [structure36R]=7LMF [structure35S]=7LMF ...[06I]=7LMF [Y6J]=7LMF )
We may start from the following script:
for file in ./*.mol2; do
file_name=$(basename "$file" .mol2)
#some command to add the file into the array
done
How this script could be completed for the creating of the array?
CodePudding user response:
Try this Shellcheck-clean code:
#! /bin/bash -p
declare -A dataset
for file in *.mol2; do
file_name=${file%.mol2}
dataset[$file_name]=7LMF
done
CodePudding user response:
I would recommend turning on nullglob
otherwise the pattern will evaluate as a string when there is no match.
Use parameter expansion to remove the file extension.
If the leading './' is included, it will need to be stripped with another expansion.
#!/usr/bin/env bash
shopt -s nullglob
declare -A dataset
for file in *.mol2; do
dataset =([${file%.*}]=7LMF)
done
for key in "${!dataset[@]}"; do echo "dataset[$key]: ${dataset[$key]}"; done