Home > database >  bash: assign variable on the fly according to the pattern in file name
bash: assign variable on the fly according to the pattern in file name

Time:01-24

My bash script operates with different files located within the same directory and then execute some program for them. Each files match the following pattern where the ${receptor} keyword is always located after the second "_" of the file name. This keyword defines the group of the file.

something_something_${receptor}_repX.pdb

According to the ${receptor}, all files are devided in three groups. So I create a special function which set the name of the prefix and execute all operations in flow only for the files contained this index:

set_receptor () {
#receptor='5r82dim'
#receptor='2zu2dim'
receptor='7dr8dim'
}


set_receptor

for pdb in "${docking_pdb}"/*_${receptor}_*.${lig_ext}; do
pdb_name=$(basename "$pdb" .${lig_ext})
pdb_name="${pdb_name/_rep1}"
echo "Converting ${output}: the system ${pdb_name}
done

This means that each time when I need to switch between different groups I need to uncomment $receptor manually in the set_receptor function. How could I determine $receptor automatically in the workflow in order that my script could be executed only 1 time for any number of groups and determine each group automatically in the begining of FOR loop execution?

CodePudding user response:

If the keyword is always after the second underscore, you can use parameter expansion to extract it:

#!/bin/bash
for pdb in "$docking_pdb"/*_*_*."$lig_ext" ; do
    receptor=${pdb##*/}       # Remove the path.
    receptor=${receptor#*_}   # Up to the first underscore.
    receptor=${receptor#*_}   # Up to the second underscore.
    receptor=${receptor%%_*}  # Remove everything after the 1st underscore.
    ...

done
  •  Tags:  
  • bash
  • Related