Question

I want to store verbatim text in Bash variables. I have an approach described here and I would like criticism and suggestions on improving this approach. Currently, the specific application is to have functions within a shell script library that are, to some extent, self documenting. Here is the sort of function I have in mind:

templateFunction(){
################################################################################
interrogationInformation=$(cat << 2012-09-26T1909
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
2012-09-26T1909
)
################################################################################
if [ "${1}" != "-interrogate" ]; then #interrogation
    echo "function template"
fi #interrogation
}

An interrogation function can query the template function and the template function then returns some information about itself by way of the 'verbatim' variable interrogationInformation. The interrogation function parses this information.

In due course, it is likely that special characters such as quotation marks could be stored in these 'verbatim' variables (for example, in natural language). These variables could be used also for building code files, such as for web pages. The difficulty in using this approach is that cat can vary between distributions and the resulting behaviour can be unpredictable. Essentially, I would like criticism of my approach and suggestions for improvement. Perhaps one improvement would be a better (preferably standard) program than cat as used in this case.

I thank you very much for any suggestions.

Était-ce utile?

La solution

You can use read instead of cat as follows:

IFS= read -d '' interrogationInformation << "EOF"
    <class>
        setup
    </class>
    <description>
        This is a natural language description of this function.
    </description>
    <prerequisiteFunctions>
        myFunction1
        myFunction2
    </prerequisiteFunctions>
    <prerequisitePrograms>
        myProgram1
        myProgram2
    </prerequisitePrograms>
EOF

Note that quoting EOF prevents any parameter expansion in the text.

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