Question

The following code is a snippet from a larger bash script I'm writing to parse through a sequence of amino acids and carry out calculations based on specific pairs. For simplicity, I have shortened it and made it self contained - but the problem I'm coming up against is that if I run the example test calculation "calc ....." line - (which is commented out below,) I get a "... is undefined" error - which refers back to the variable fed into the while-read loop. Perhaps this is to do with subshells and/or variable accessibility but I'm stumped as to the solution, especially as I have similar scripts which work.

Any help would be appreciated. - Also, I'm running this in a mac OSX terminal.

#!/bin/bash

#Set input
Seq=HTCHMAREQEMNMHG
SeqMAX=${#Seq}

#set start position for read
Pos=1

#Analyse sequence
echo -e "\nReading: $Seq "

#Read sequence, one character at a time 
while  read -r -n1 Res ; do

    #If Res variable has hit a "blank", then move on
    if [[ $Res = "" ]] ; then

        break

    #Else, if Res variable is the first     
    elif [[ $Pos = "1" ]] ; then

        #Calculate rate

        #UNCOMMENTING THIS LINE CAUSES ERROR
        #calc 9 + 10

            echo "$Left $Right"
            Pos=$((Pos+1))
            Left=$Res

        #Else, if Res variable is the last  
        elif [[ $Pos = $SeqMAX ]] ; then

            Left=$Res
            Right=xC
            ###CALC............
            Pos=$((Pos+1))
            echo -e "$Left $Right"  

        #Finally, if charcter is internal       
        else 

            Right=$Res
            echo "$Left $Right"
            Left=$Res
            Pos=$((Pos+1))

        fi

done < <(echo $Seq)

exit
Était-ce utile?

La solution

It appears that calc reads from standard input, so you need to prevent it from consuming data from the stream provided to the while loop intended for the read command:

calc 9 + 10 < /dev/null

Autres conseils

You are missing: '

Try:

calc '9 + 10'

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top