1

I work on a script running Cheetah ; at some point there are some bash commands and I got a syntax error ("Error in the Python code which Cheetah generated for this template").

The line involved is :

&& name=$(echo '\$another_variable'".phy" | cut -d _ -f 1)

The syntax error being on the very first '$' sign. It worked yesterday so I'm kind of confused ...

The whole code is wrraped in a xml tag :

<command><![CDATA[
    ln -s '$input' '$input.element_identifier' &&

    BlastAlign -i '$input.element_identifier'
    -m $advanced_option.m
    #if $advanced_option.r != ""
        -r $advanced_option.r
    #end if
    #if $advanced_option.x != ""
        -x $advanced_option.x
    #end if
    -n $advanced_option.n
    #if $advanced_option.s != 0
        -s $advanced_option.s
    #end if

    && mkdir outputs

    && name=$(echo '\$input.element_identifier'".phy" | cut -d _ -f 1)

    && number=$(grep '/' '\$input.element_identifier'".phy" | wc -l)

    && new_file=$name"_sp"$number".phy"
    && mv '\$input.element_identifier'".phy" '$new_file'
    && new_file2=$name"_sp"$number".nxs"
    && mv '\$input.element_identifier'".nxs" '$new_file2'
    && cp '$new_file' outputs/
    && cp '$new_file2' outputs/

    #if $fasta_out.value == True
        && python $__tool_directory__/scripts/S01_phylip2fasta.py /outputs/'$new_file' outputs/$name"_sp"$number".fasta"
    #end if

]]></command>
Micawber
  • 563
  • 1
  • 4
  • 12
  • I'm not sure what you're asking. But it looks like you're trying to do this: `name=${another_variable%%_*}`, no need for `echo` and `cut`. – janos Oct 27 '17 at 09:15
  • It seems it works indeed ! I don't have the error anymore. But a second command has exactly the same error on the first '$' : `&& number=$(grep '/' '\$input.element_identifier'".phy" | wc -l)`. It's a problem with Cheetah, I don't know why it's happening because it's works fine as a simple bahs command – Micawber Oct 27 '17 at 09:43
  • Is the error on the `$` of the `$(...)`? Then it seems it's not able to execute a sub-shell. You could try to use `\`...\`` instead of `$(...)`. – janos Oct 27 '17 at 09:46
  • The code you show is `bash` code but not `Cheetah` so how can we help? Please show us more code, at least few lines of `Cheetah` code. – phd Oct 27 '17 at 10:35
  • sorry, it's edited now. – Micawber Oct 27 '17 at 12:52

1 Answers1

1

You have to screen (escape with a backslash) every $ sign that you want to pass to bash, else Cheetah tries to interpret them:

&& name=\$(echo '\$input.element_identifier'".phy" | cut -d _ -f 1)

&& number=\$(grep '/' '\$input.element_identifier'".phy" | wc -l)

etc.

phd
  • 57,284
  • 10
  • 68
  • 103