I am using a bash script to run a program on a list of genes, all of which are named in a file called IDlist.txt. After reading IDlist.txt into an array, I am trying to loop through the array and execute the command once for every gene. This requires concatenating the path with the gene name (IDlist[i]) and the file extension (.fas).
Here's what my code looks like:
#!/bin/bash
## This declares IDlist and reads each line in the file into the array
declare -a IDlist
n=0
while read line
do
IDlist[n]=${line}
n=${n}+1
done < "IDlist.txt"
##Then I loop through IDlist
for i in ${IDlist[@]}
do
echo $i
path1="/path/to/geneTrees/63mammnt/trimallcds_wSpalax/newFasta/${i}.fas"
echo $path1
path2="/path/to/geneTrees/masterTrees/master.$i.tre"
##This is the command
(echo 1; echo ${path1}; echo ${path1}; echo ${path2}; echo 2; echo 2) | /path/to/program/hyphy/HYPHYMP /path/to/program/hyphy/build/lib/hyphy/TemplateBatchFiles/SelectionAnalyses/RELAX.bf
done
The output from "echo $i" is correct, but the output from "echo $path1" is not. For $i = CRIPT $path1 is .fas1/to/geneTrees/63mammnt/trimallcds_wSpalax/newFasta/CRIPT
.fas is appending to the beginning of the string (even though I'm adding it to the end), and it's writing over the first folder in the path. This does not happen when I run these commands without a script.
IDlist.txt
is a DOS/Windows file terminated by CR/LF. Convert it to unix withdos2unix
or trim the CR withIDlist[n]=${line%$'\r'}
.n
variable withn=${n}+1
, instead of adding them to it; while they will be evaluated as arithmetic expressions inIDlist[n]=...
, it's still better to just writeIDList[n++]=...
and get rid of then=${n}+1
.