I am trying to create directories whose names are variables defined in an array. When I run my code below I do not understand the errors
#!/bin/bash
#
Output_Base_Dirs=(/home/user/CORDEX/OUTPUT/historical /home/user/CORDEX/OUTPUT/rcp45)
Input_Base_Dirs=(/home/user/CORDEX/INPUT/historical /home/user/CORDEX/INPUT/rcp45)
Input_Data_Dirs=(CLMcom-CCLM4-8-17_v1/CNRM-CERFACS-CNRM-CM5_r1i1p1/day/native)
Var_Dirs=(precip tmin tmax)
Vars=(pr tasmin tasmax)
# Create directories called by the variable name, if not there
###########################################################
for i in "${Output_Base_Dirs[@]}"
do
echo $Var_Dirs[i]
# if [ ! -d ${Var_Dirs[i]} ]; then
echo $Var_Dirs[i]
echo "Directory doesn't exist. Creating now"
mkdir "$Output_Base_Dirs[i]/$Var_Dirs[i]"
echo "File created"
# else
# echo "Directory exists"
# fi
done
exit 0
When I run the above code with the if construct in place I get an error at the line of the if, as follows
precip[i]
./merge_files.sh: line 55: /home/user/CORDEX/OUTPUT/historical: syntax error: operand expected (error token is "/home/user/CORDEX/OUTPUT/historical")
When I run the code with the if construct commented out, I get the following precip[i]
Directory doesn't exist. Creating now
mkdir: cannot create directory ‘/home/user/CORDEX/OUTPUT/historical[i]/precip[i]’: No such file or directory
File created
precip[i]
Directory doesn't exist. Creating now
mkdir: cannot create directory ‘/home/user/CORDEX/OUTPUT/historical[i]/precip[i]’: No such file or directory
File created
Why does
echo $Var_Dirs[i]
print "precip[i]"?
I will appreciate to understand what is the problem and how to do it properly.
Output_Base_Dirsas indexes forVar_Dirs