I'm writing a bash script to perform some commands on files. In order to do that, I need to load the modules which contain these commands. Now I can do these from the command line just fine, but I wanted to implement a check in a script to see if those modules are loaded, and if they are not, to load them. Here is what I have done so far (I am just 3 days into learning bash, so excuse any newbie errors):
#A list with all the necessary modules
declare -a modules=("CDO/1.9.7-gompi-2019a" "ncview/2.1.7-gompi-2019a")
echo "Running script"
echo ""
#Check if modules are loaded. If not, load them
for mod in "${modules[@]}"
do
module is-loaded $mod
if [ $? = 1 ]
then
echo "Loading ${mod}"
module load $mod
else
echo "${mod} is already loaded"
fi
done
echo ""
echo "Finished running script"
It enters the if statement, meaning the module is not loaded, and then says "Loading CDO...", but when I try to use commands from this module, it does not work because the module just doesn't seem to be executed. But the "module is-loaded" executes just fine. What am I doing wrong?
[diyon@login2 scripts]$ ./script.sh
Running script
Loading CDO/1.9.7-gompi-2019a
Loading ncview/2.1.7-gompi-2019a
Finished running script
[diyon@login2 scripts]$ cdo
-bash: cdo: command not found