1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Syntax error when redirecting eval

Discussion in 'Support' started by David Marcus, Mar 16, 2013.

  1. David Marcus

    Joined:
    Jun 4, 2008
    Messages:
    648
    Likes Received:
    1
    Would someone please explain why I get a syntax error when I try to redirect the output from an alias for "echo %@eval[%$]"?
    Code:
    C:\>alias bc
    echo %@eval[%$]
     
    C:\>bc 2+2
    4
     
    C:\>bc 2+2 > clip:
    TCC: Syntax error "2+2 > clip:"
     
    C:\>alias foo=`echo %$`
     
    C:\>foo bar+2
    bar+2
     
    C:\>foo bar+2 > clip:
     
    C:\>ver
     
    TCC  14.03.59  Windows Vista [Version 6.0.6002]
     
    C:\>
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,300
    Likes Received:
    39
    The > CLIP: is included in the alias arguments %$. Why? Because variable expansion (including aliases) is performed before redirection (so you can use a variable or expression as the filename or device name in a redirection.)

    Your original alias will do what you expect if you call it like this:

    Code:
    ( bc 2 + 2 ) > clip:
    
     
  3. Dan Glynhampton

    Joined:
    Feb 26, 2013
    Messages:
    100
    Likes Received:
    0
    The problem is the way you've defined the alias. Using %$ means all of the parameters to the alias are placed inside the @eval brackets. If you press Ctrl-F to expand the aliases on the command line you'll see what's happening, the command line you end up with is

    echo %@eval[2+2 > clip:]

    and not

    echo %@eval[2+2] > clip:

    which is what you were hoping for. In this case command grouping will help:

    d:\>bc 2+2
    4

    d:\>(bc 2+2) > clip:

    d:\>echo %@clip[0]
    4


    Dan
     
  4. David Marcus

    Joined:
    Jun 4, 2008
    Messages:
    648
    Likes Received:
    1
    So, the only way to avoid using parentheses is to use a btm? (I knew I could use parentheses on the command line.)
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,300
    Likes Received:
    39
    In principle, I suppose a function could parse out the redirection and put it in the desired place. But the thought of actually doing it in practice makes my head hurt.
     
  6. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Indded, AFAIK there is no alternative. Same as my nearly identical alias CALC for the same purpose (but I also use @COMMA to make the result more readable).

    Note that for some commands, e.g., FOR and GLOBAL, you must also do the same if you want the whole output redirected into a single file, else each iteration is redirected either to its own separate file (if using a file in the CWD) , or it overwrites the single output file (assuming NoClobber=No), so only the last one's result remains... which is noted in their respective HELP pages.

    Think of command groups as your friends, not as contortions...
     

Share This Page