Hi I am pretty new to bash scripting. I am trying to make a pipeline for Nanopore assembly. I want a pipeline, where I can set several input paths and output ID. I finnally found a way to set these variables to a state where they are working and where the script is stating if these variables are not set = just what I want.
The problem is that I am now getting some sort off error mesages, eventhough it actually finds the paths and files.
Here is some of the script part that is troubling, where I set the four variables:
if [$1 = ]; then echo "Mangler directory til Nanopore filer(er)" exit 0
fi
if [$2 = ]; then echo "Ingen output filnavn angivet" exit 0
fi
if [$3 = ]; then echo "mangler Illumina forward fil" exit 0
fi
if [$4 = ]; then echo "mangler Illumina reverse fil" exit 0
fi
PATH=${1}
PREFIX=${2}
il1=${3}
il2=${4}
d=$(printf "%(%d-%m-%Y)T")
#Porechop
echo PORECHOP TRIMMER NANOPORE READS
$'/home/kma/miniconda3/envs/pomoxis/bin/porechop' -i $PATH --threads 4 --check_reads 100 --discard_middle -o /media/kma/new/porechop/"$PREFIX"_"$d"_trimmed.fastq
wait $pid
echo Porechop $pid finished. Here is the output I am getting, before it goes through with the task:
/home/kma/Nanopore_pipeline.sh: line 10: [/media/kma/new/Nanopore: No such file or directory
/home/kma/Nanopore_pipeline.sh: line 15: [Bfrag_THS: command not found
/home/kma/Nanopore_pipeline.sh: line 20: [/media/kma/new/Illumina/SRR8549466_1.fastq: No such file or directory
/home/kma/Nanopore_pipeline.sh: line 25: [/media/kma/new/Illumina/SRR8549466_2.fastq: No such file or directoryI hope someone can help me whit this problem :-) - Sabine
1 Answer
You made a common mistake: There should be a blank space after [ (also before ]). Those are keywords [ is a command and ] its mandatory argument (as pointed out by Kamil Maciorowski), and just as you cannot write echo1, you cannot write [$1. Besides, = is a binary operator, so you need one expression on each side of it. Apparently you want to check whether each argument from $1 to $4 is empty. Then, write it thus
if [ "$1" = "" ]; thenfor each of them. I have also double quoted your variable, that's very important generally (you may check out why).
Now, if you tried if [ $1 = ] and supplied your script no arguments, $1 would be empty and the execution would be terminated with "Mangler directory til Nanopore filer(er)". So you could think that the script is OK, when in fact it is not. What really happens is that the expression becomes if [ = ], and since there is a non-empty string = in the test, it succeeds.
Some recommendations:
You can verify the number of arguments provided with $# instead of checking if each of them is empty.
shellcheck is a useful tool to debug your script, give it a try!
2