1

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.

  • Your IDlist.txt is a DOS/Windows file terminated by CR/LF. Convert it to unix with dos2unix or trim the CR with IDlist[n]=${line%$'\r'}. – mosvy Jul 3 '19 at 19:33
  • 1
    Your script has many other problems, the most obvious being that you keep appending numbers to the n variable with n=${n}+1, instead of adding them to it; while they will be evaluated as arithmetic expressions in IDlist[n]=..., it's still better to just write IDList[n++]=... and get rid of the n=${n}+1. – mosvy Jul 3 '19 at 19:36
1

Some hints:

  • Use dos2unix to convert files to Unix newlines. What looks like overwriting is actually the carriage return character, which when displaying the value has the special meaning of moving the cursor to the start of the line.
  • By far the simplest way to read a file into an array is readarray.
  • Use More Quotes™
  • Try running the result through shellcheck for more hints, and once the script passes you could post it for review.
  • You probably want to group the echo commands using { and } to avoid creating a subshell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.