How to Pass Parameters to LC Shell Script?

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

How to Pass Parameters to LC Shell Script?

Post by icouto » Sat Apr 26, 2014 9:18 am

How can I pass a parameter to a .lc script I'm running from Terminal? How do I get this parameter in my script?

If I place any of the functions param, params or paramCount it seem to freeze the server - ie., the execution of the script stops at that line, and I have to force-quit it.

Any help would be greatly appreciated.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8428
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to Pass Parameters to LC Shell Script?

Post by FourthWorld » Sat Apr 26, 2014 2:46 pm

Param and paramCount are used only for functions - the hang you're seeing is likely an unhandled execution error from attempting to use those in a different context. When a script execution error is encountered, LC will output the error message to stdErr, which under Apache will be appended to Apache's error log, and when run from the command line should appear in the Terminal output from the app. In most cases what LC outputs to stdErr will contain the exact line number where the execution error occurred for quick repair.

Command line arguments are passed to the app in global variables named $1, $2, etc. You can obtain the number of these arguments by checking the value of the global variable $#.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

Re: How to Pass Parameters to LC Shell Script?

Post by icouto » Sun Apr 27, 2014 2:20 am

Thank you, Richard - that was a very useful reply.

The server is not erring when asked to solve 'param', 'params' or the 'paramCount' - there is apparently no output being sent to stdErr - it just seems to be caught in an infinite loop. I need to 'ctrl-Z' to get it to stop. I get a feeling this shouldn't be happening, so it should probably be reported as a bug.

The '$' variables are also not behaving as explained in the Dictionary. The Dictionary states:
If you start up the application from the command line (on OS X, Unix or Windows systems), the command name is stored in the global variable $0 and any arguments passed on the command line are stored in numbered variables starting with the $ character.
In my script, however, running the script with no parameters, shows an empty $0, and $# is '0' (zero). Starting the script with one parameter, puts that parameter in $0, and $# is now '1'.

Is that expected behaviour - and the documentation just needs updating - or is this a bug? Should I be writing my scripts expecting parameter 1 to be in $0 or in $1?

By the way, this is all being done using LC Server 6.6.1.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8428
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to Pass Parameters to LC Shell Script?

Post by FourthWorld » Sun Apr 27, 2014 3:39 am

I don't understand: of you're starting the script worth one param, wouldn't you expect the param count to be 1?

With most things Unix, counting begins at 0.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 92
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

Re: How to Pass Parameters to LC Shell Script?

Post by icouto » Sun Apr 27, 2014 8:57 am

FourthWorld wrote:I don't understand: of you're starting the script worth one param, wouldn't you expect the param count to be 1?
Indeed. As I described, if I pass a single parameter, $# is "1", but the parameter gets placed in $0, not in $1, as described both by you, and the Dictionary.

According to the Dictionary, $0 should contain the name of the app/command/script being run, but it doesn't. When the code is run as a shell script, $0 seems to actually hold the first parameter passed, not the name of the script - and if no parameter is passed, it holds nothing.

Is this expected, but undocumented behaviour, or is this a bug? What do you think?

Post Reply

Return to “CGIs and the Server”