0

I've got the following code:

#!/bin/bash

while read line
do
echo "line" $line
if [[ -d ../Results/${line}_Forward && -d ../Results/${line}_Reverse ]]
then

        cd ../Results/COMBI
        mkdir ./${line}_COMBI
        cd ..
        for (( win = 2; win < 20; win += 2 )); do
        printf 'The value of win is %d, now entereing inner loop\n' "$win"

        for (( per = 50; per < 100; per += 2 )); do
            printf 'Value of per is %d\n' "$per"
            merger -asequence ./${line}_Forward/${line}_Forward_Trimmed_w${win}_p${per}.fa -bsequence ./${line}_Reverse/REV_COMP/${line}_Reverse_Trimmed_w${win}_p${per}_RevComp.fa -outseq ./COMBI/${line}_COMBI/${line}_w${win}_p${per}_COMBI.fa
        done
        done
else
    echo $line "Forward or Reverse directory does not exist"
fi
done < ./Data_ORD.txt

When i run the script any dictory contained in the if statment exits but they actually do. And i get this kind of output for each line in Data_ORD.txt:

line  ORD0926
 Forward or Reverse directory does not exist

in the last echo $line variable is not printed. I guess something is wrong with this variable but or with the if statement.

Any idea's why $line is not printed in the last echo and directories are not recognized by the if statement?

**.fa: fasta formats which are plain text formats containing DNA sequences. **merger command: contained in the EMBOSS package "The European Molecular Biology Open Software Suite"

5
  • 1
    Do cat -A on your input file, see if it ends with a newline ( so look for $ in the last line ). If it doesn't, it can explain why last echo doesn't print out $line. I can't see what potential issue with if statement could be. Are you checking whether both directories exist or do you need to have just one exit ? Jan 15, 2017 at 13:01
  • Im checking whether if both directories exist. I created a new variable after then which cuts codes contained in the data file: wth=echo ${line} | cut -c 1-7 and it solved the problem! what is used cat -A for?
    – Neuls
    Jan 15, 2017 at 13:28
  • cat -A shows the non-visible characters, for example newlines. Do printf "one\ntwo" | cat -A and you'll see what i mean Jan 15, 2017 at 13:32
  • 1
    (@Serg) cat -A on GNU, or cat -v anywhere, also shows if the lines are terminated by Windows-style CRLF instead of LF; that would explain both the -d tests failing and the 'missing' (actually overwritten) value in the error message for each line. Jan 15, 2017 at 15:37
  • Neuls: FYI in bash (and some other shells) you can just do ${line:0:7} Or you can remove the last char if it's CR with ${line%$'\r'} Jan 15, 2017 at 15:48

1 Answer 1

0

Im checking whether if both directories exist. I created a new variable after then which cuts codes contained in the data file: wth=echo ${line} | cut -c 1-7

#!/bin/bash

while read line
do
echo "line" $line
wth=`echo ${line} |  cut -c 1-7`
echo "wth" ${wth}
if [[ -d ../Results/${wth}_Forward && -d ../Results/${wth}_Reverse ]]
then

        cd ../Results/COMBI
        mkdir ./${wth}_COMBI
        cd ..
        for (( win = 2; win < 20; win += 2 )); do
        printf 'The value of win is %d, now entereing inner loop\n' "$win"

        for (( per = 50; per < 100; per += 2 )); do
            printf 'Value of per is %d\n' "$per"
            merger -asequence ./${wth}_Forward/${wth}_Forward_Trimmed_w${win}_p${per}.fa -bsequence ./${wth}_Reverse/REV_COMP/${wth}_Reverse_w${win}_p${per}_RevComp.fa -outfile none -outseq ./COMBI/${wth}_COMBI/${wth}_w${win}_p${per}_COMBI.fa
        done
        done
else
    echo ${wth} "Forward or Reverse directory does not exist"
fi
done < ./Data_ORD.txt

Now the script works. It seems calling directly $line as a variable isn't a good idea since there can be some unseen spaces.

3
  • 2
    There are various things wrong with this script (first of all, you're not quoting your variables which is why spaces will break it) but it's hard to help since you haven't explained what you're actually trying to do and it is not easy to guess what your directory structure is. Also, it looks like you're aligning forward and reverse strands. What's the point in doing that?
    – terdon
    Jan 15, 2017 at 13:40
  • do you mean quoting a variable like "${line}"? Is it a good idea to do it always? well i got the trouble when checking if the directories existed, they did but the if statement wast recognizing it. I somehow found out that it was due to $line var so i corrected it with cutting it into a new variable (wth). I'm aligning forward and reverse complementary DNA strands in order to get a better quality merged strand
    – Neuls
    Jan 15, 2017 at 16:43
  • Yes, like "${line}" or just "$line". I'm sure we could help more if you gave a more comprehensive example. As for the alignment, I've never used merger` and just glanced at the manual where it mentioned it does a Needlman-Wunsch alignment, but missed that it's designed for overlapping sequences. I get it now.
    – terdon
    Jan 16, 2017 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .