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

Lua does not work on the commandline

Discussion in 'Support' started by djspits, Oct 14, 2016.

  1. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    From the help:
    Code:
    lua -e'a=1' -e 'print(a)' script.lua
    "... does not compute."
    from an old sf movie.:smile:

    But seriously, I can't get the simplest of snippets to work from the command line or @Lua[]

    Running a file does seem to work, but anything quoted is ... unwieldy.

    >lua -e print("Hello world")

    surprisingly also works, although I would think the quoting is wrong.

    So, would you be so kind to explain how I'm supposed to quote a Lua line like;

    Code:
    print(string.gsub("hello world", "(%w+)", "%1 %1"))
    Regards,
    Dirk Jan Spits.
     
  2. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    Comparison of internal Lua and two external versions I compiled myself.
    Code:
    PROMPT> ver
    
    TCC  20.00.22 x64   Windows 7 [Version 6.1.7601]
    
    PROMPT> which lua & lua -v
    lua is an internal command
    Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
    
    PROMPT> which lua52 & lua52 -v
    lua52 is an external : C:\BIN\lua52.exe
    Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
    
    PROMPT> which lua53 & lua53 -v
    lua53 is an external : C:\BIN\lua53.exe
    Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
    
    PROMPT> lua -e "print(math.sin(12))"
    Lua: (command line):1: unexpected symbol near '"print(math.sin(12))"'
    
    PROMPT> lua52 -e "print(math.sin(12))"
    -0.53657291800043
    
    PROMPT> lua53 -e "print(math.sin(12))"
    -0.53657291800043
    
    PROMPT> lua -e "print('this is a test')"
    Lua: (command line):1: unexpected symbol near '"print('this is a test')"'
    
    PROMPT> lua52 -e "print('this is a test')"
    this is a test
    
    PROMPT> lua53 -e "print('this is a test')"
    this is a test
    
    PROMPT>
    Edit: Lua function seems to work...
    Code:
    PROMPT> %@lua[print(math.sin(12))]
    -0.53657291800043
    
    PROMPT> %@lua[print('this is a test')]
    this is a test
     
    #2 JohnQSmith, Oct 14, 2016
    Last edited: Oct 14, 2016
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,972
    Likes Received:
    30
    Apparently "-e" must not be followed by a space.
    Code:
    v:\> lua -e "print(math.sin(12))"
    Lua: (command line):1: unexpected symbol near '"print(math.sin(12))"'
    
    v:\> lua -e"print(math.sin(12))"
    -0.53657291800043
     
  4. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    Thanks guys. You inspired me to struggle on. Here's what I came up with:
    Code:
    C:\...\prj :-) >lua -e"print(string.gsub('hello world', '(%%w+)', '%%1 %%1'))"
    hello hello world world 2
    ... which is the correct answer.

    So,
    1) No space between -e and quotation mark
    2) The outer character must be a double quote
    3) Any quotes in the Lua string must then be a single quote
    4) You must double any percent chars in the Lua strings

    Point #3 means the TC user should be warned that most examples from the Lua manuals and tutorials will not run as-is and need to be adapted.

    Or so I thought ... I decided to check my findings by changing the example in the TC help according to the rules above.

    Code:
    C:\...\prj :-( >lua -e'a=1' -e 'print(a)'
    Lua: (command line):1: unfinished string near <eof>
    
    C:\...\prj :-( >lua -e"a=1" -e"print(a)"
    Lua: (command line):1: unexpected symbol near '"print(a)"'
    I think I will wait until Rex relieves us from our suffering.
     
    #4 djspits, Oct 14, 2016
    Last edited: Oct 14, 2016
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,972
    Likes Received:
    30
    Just to make it more interesting. :-)
    Code:
    v:\> lua -e"a=1" -eprint(a)
    1
    Another problem is (I think, maybe already realized) that TCC's all-purpose parser considers '=' as an argument separator. I think that, because of that, the unfinished string in djspits's recent example (the one that failed) is
    Code:
    'a
    ... that TCC skipped the '=' and started a new argument.[/code]
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,870
    Likes Received:
    83
    First, you shouldn't be using the LUA command if you just want to execute a LUA argument; that's for @LUA[...].

    The LUA command expects a script name, so it's looking for quotes and then removing them before passing the command line on to the LUA parser.

    But if you really want to use LUA instead of @LUA, the = is a command delimiter in TCC, so you need to prevent that from being parsed. You need to set CMDBatchDelimiters=NO in your TCMD.INI.
     

Share This Page