I've started learning bash today, but I knew basics of javascript.
It would be very nice if anyone can point out at least a few pieces of advice on the code.
Because there are so many details in bash, I hope you won't be too harsh.
Expected behavior
The code is supposed:
- to set the value of a variable named 'charge' from the input files
- to create a few directories and subdirectories based on the input files
Concrete Example
Let's say there is a mol2 file named glucose-minus1.mol2.
- I expect the code to set up a variable charge with value -1, so charge="-1". So you can see there is a translation from minus1 --> -1 If the filename is 'wrong' the user should be notified "we can't find the charge value in the filename" or something like that.
- And to create a directory named
glucose-minus1(without the extension) plus the 3 sub-directories "solv" "sin-solv" and "thermo". All this if the directories are do no exist already.
#!/bin/bash
touch "mol-minus1.mol2" "mol-minus2.mol2"
molecules=(*.mol2)
subdirs=("solv" "sin-solv" "thermo")
setCharge () {
#input molfile
#if it finds any chargeLabel in molfile, sets the charge value (and we use it later).
chargeLabels=( "minus3" "minus2" "minus1" "cero" "plus1" "plus2" )
charges=("-3" "-2" "-1" "0" "+1" "+2")
echo "${chargeLabels[*]}"
for index in "${!chargeLabels[@]}"
do
if [[ "$1" == *"${chargeLabels[index]}"* ]]
then
charge="${charges[index]}"
echo "$charge"
else
echo "Im not setting charges..."
continue
fi
done
}
createDir(){
#pass dir as an argument.
if [ ! -d "$1" ]
then
mkdir "$1"
else
printf " %s is already there " "$1"
fi
}
for mol in "${molecules[@]}"
do
[ -f "$mol" ] || exit 0
#sets the carge variable to a value included in molfile.
echo "$mol"
setCharge "$mol"
#acreate molecule directory if not there.
IFS="." read -ra splitted <<< "$mol"
if [[ ! -d "${splitted[0]}" ]]
then
mkdir "${splitted[0]}"
printf "directory %s has been created. Molecule charge is %s" "${splitted[0]}" "$charge"
else
printf "directory %s already exists. Molecule charge is %s" "${splitted[0]}" "$charge"
continue
fi
#create subdirectories if not there.
cd "${splitted[0]}" || printf "No directory with name %s" "${splitted[0]}" && exit 0
for dir in "${subdirs[@]}" #create subdirs if not there.
do
createDir "$dir"
done
cd '../' #single quotes for string literals.
cp "$mol" "${splitted[0]}/sin-solv/"
#python -c'import flexo_api.py; flexo_api.flexo("$)'
done