I'm trying to write a if-else condition in shell/bash script which will be used for many different files so it won't fit a certain structure.
I have three different files, and up to three different variables selected from each of these files which go into the if-else statement. In my script, I have this written (which probably could be written in a better way) at the beginning as follows:
ANC1=$(sed -n 1p file1 | cut -f 1 -d' ' )
ANC2=$(sed -n 2p file1 | cut -f 1 -d' ' )
ANC3=$(sed -n 3p file1 | cut -f 1 -d' ' )
ANC11=$(sed -n 1p file2 | cut -f 1 -d' ' )
ANC21=$(sed -n 2p file2 | cut -f 1 -d' ' )
ANC31=$(sed -n 3p file2 | cut -f 1 -d' ' )
ANC15=$(sed -n 1p file3 | cut -f 1 -d' ' )
ANC25=$(sed -n 2p file3 | cut -f 1 -d' ' )
ANC35=$(sed -n 3p file3 | cut -f 1 -d' ' )
As example, from these files, the following variables could have resulted:
echo ${ANC1}
FIN
echo ${ANC2}
NFE
echo ${ANC3}
echo ${ANC11}
FIN
echo ${ANC21}
NFE
echo ${ANC31}
echo ${ANC15}
FIN
echo ${ANC25}
NFE
echo ${ANC35}
SAS
From here, I've written the if-else statement (taking into account the possible missing variables in three files as above). To make sense of it, the attempt is to do the following:
first condition: if all variables are not empty; second condition: if the third variable is the only missing variable; third condition: if the third and second variables are empty
if [ "${ANC3}" != "" ] || [ "${ANC31}" != "" ] || [ "${ANC35}" != "" ]; then
echo "***** three variables *****"
bcftools merge -m both \
fileref1.genotypes_${ANC1}.vcf.gz \
fileref1.genotypes_${ANC2}.vcf.gz \
fileref1.genotypes_${ANC3}.vcf.gz \
-Oz \
-o fileref1.new.genotypes_${ANC1}.${ANC2}.${ANC3}.vcf.gz
bcftools merge -m both \
fileref2.genotypes_${ANC11}.vcf.gz \
fileref2.genotypes_${ANC21}.vcf.gz \
fileref2.genotypes_${ANC31}.vcf.gz \
-Oz \
-o fileref2.new.genotypes_${ANC11}.${ANC21}.${ANC31}.vcf.gz
bcftools merge -m both \
fileref3.genotypes_${ANC15}.vcf.gz \
fileref3.genotypes_${ANC25}.vcf.gz \
fileref3.genotypes_${ANC35}.vcf.gz \
-Oz \
-o fileref1.new.genotypes_${ANC15}.${ANC25}.${ANC35}.vcf.gz
elif
[ "${ANC3}" == "" -a "${ANC2}" != "" ] || [ "${ANC31}" == "" -a "${ANC21}" != "" ] || [ "${ANC35}" == "" -a "${ANC25}" != "" ]; then
echo "***** two variables *****"
bcftools merge -m both \
fileref1.genotypes_${ANC1}.vcf.gz \
fileref1.genotypes_${ANC2}.vcf.gz \
-Oz \
-o fileref1.new.genotypes_${ANC1}.${ANC2}.vcf.gz
bcftools merge -m both \
fileref2.genotypes_${ANC11}.vcf.gz \
fileref2.genotypes_${ANC21}.vcf.gz \
-Oz \
-o fileref2.new.genotypes_${ANC11}.${ANC21}.vcf.gz
bcftools merge -m both \
fileref3.genotypes_${ANC15}.vcf.gz \
fileref3.genotypes_${ANC25}.vcf.gz \
-Oz \
-o fileref1.new.genotypes_${ANC15}.${ANC25}.vcf.gz
elif
[ "${ANC3}" == "" -a "${ANC2}" == "" ] || [ "${ANC31}" == "" -a "${ANC21}" == "" ] || [ "${ANC35}" == "" -a "${ANC25}" == "" ]; then
echo "***** one variable ***** "
cp fileref1.genotypes_${ANC1}.vcf.gz fileref1.new.genotypes_${ANC1}.${ANC2}.vcf.gz
cp fileref2.genotypes_${ANC11}.vcf.gz fileref2.new.genotypes_${ANC11}.vcf.gz
cp fileref3.genotypes_${ANC15}.vcf.gz fileref1.new.genotypes_${ANC15}.vcf.gz
fi
Every time I run this script, 3 files are supposed to be produced, but sometimes this is not the case. The first part works (for the files where all variables are not empty) - but the second and third conditions don't seem to. I've also tried [ -z "${ANC3}" ]
and [ -n "${ANC2}" ]
to indicate missing and non missing, respectively but this also did not work. Also tried [[ ]]
compared [ ]
but still the same.
Anything that I'm obviously missing?