1

I've written the following script:

CODE=`cut -c 7-21 Data.txt`

for i in ${CODE};
do
    WIN=2
    PER=50
    if [ -f ../FASTA_SEC/${i}.fa ]
    then
        while [[ ${WIN} -lt 20 ]]
        do 
            echo "The value of WIN is" $WIN "now begins nested while"
            while [[ ${PER} -lt 100 ]]
            do      
                echo "value of PER is" $PER             
                ((PER=$PER+2))
            done
            ((WIN=$WIN+2))
        done

    else
        echo "the file does not exist" ${i}
    fi
done

I expected it to work like this:

$WIN $PER
2    50
2    52
2    54
2    56
[.....]
4    50 
4    52
4    54
4    56

And so on.. I though i could do it with a nested while but i get the following result:

The value of WIN is 2 now begins nested while
value of PER is 50
value of PER is 52
value of PER is 54
value of PER is 56
value of PER is 58
value of PER is 60
value of PER is 62
value of PER is 64
value of PER is 66
value of PER is 68
value of PER is 70
value of PER is 72
value of PER is 74
value of PER is 76
value of PER is 78
value of PER is 80
value of PER is 82
value of PER is 84
value of PER is 86
value of PER is 88
value of PER is 90
value of PER is 92
value of PER is 94
value of PER is 96
value of PER is 98
The value of WIN is 4 now begins nested while
The value of WIN is 6 now begins nested while
The value of WIN is 8 now begins nested while
The value of WIN is 10 now begins nested while
The value of WIN is 12 now begins nested while
The value of WIN is 14 now begins nested while
The value of WIN is 16 now begins nested while
The value of WIN is 18 now begins nested while

The inner loop just runs once when WIN value is 2, then win value interments. Why isn't it worked as desired?

How could I define a variable as a range with increments of 2. For instance, $PER=(Range 50:100, 2) or so for integrating this range in a for i in $PER loop

What do you suggest?

Thank you

  • 2
    Put PER=50 inside the first while loop. – Kusalananda Jan 12 '17 at 20:37
  • It worls truw, i forgotthat! – Neuls Jan 12 '17 at 21:18
0

You're expecting the value $PER to be reset to 50 for each iteration of the outer while loop, but you don't reset it there.

I also notice that you read potentially lots of data into a single variable at the top of the script.

This would be a bit more efficient (assuming bash or ksh syntax):

cut -c 7-21 "Data.txt" |
while read -r code; do
    if [[ ! -f "../FASTA_SEC/$code.fa" ]]; then
        printf 'No such file: %s\n' "../FASTA_SEC/$code.fa"
        continue
    fi

    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"
        done
    done
done

This avoids reading in the full output of cut into a variable and instead reads it line by line.

It also avoids the cumbersome while loops.

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.