Page 1 of 1

sending concatenated string through shell

Posted: Wed Sep 03, 2014 10:17 am
by glynypig
I'm really struggling with this. Can somebody please explain what I am doing wrong - I'm new to LiveCode. I'm trying to send a concatenated string with a "cd" command through shell on a mac.

For example, this works fine...

Code: Select all

    put shell("cd" && quote & defaultFolder & quote) into field 2
Whereas trying to append a folder name "playground" (folder created elsewhere in the script on openCard) does not...

Code: Select all

    put shell("cd" && quote & defaultFolder & slash & "playground" & quote) into field 2
And neither does this...

Code: Select all

    put shell("cd" && quote & defaultFolder & "/playground" & quote) into field 2
I'm lost - either is creates a "folder does not exist" (usually because it's missing the forward slash and so creating a folder name that does not exist) or else it goes to the PWD regardless of the indicated subfolder.

Re: sending concatenated string through shell

Posted: Wed Sep 03, 2014 11:43 am
by Mark
Hi,

The defaultFolder exists by definition. If you set the defaultfolder to a non-existent defaultfolder, LiveCode throws an error. However, the defaultFolder & "/Playground" may not exist. Maybe you need to check the value of the defaultFolder and make sure that there is a folder Playground in that location. If it doesn't exist, you can't cd to it.

Also note that the cd command doesn't return a value if it is executed successfully AND that this command doesn't have any effect on LiveCode. The cd command doesn't change LiveCode's defaultFolder after the handler finishes AND it doesn't change the shell's directory, since the shell always runs in LiveCode's default directory. If you want to run the shell in a different directory, then you have to set the defaultFolder first. For example this works:

Code: Select all

set the defaultFolder to "/"
put shell("ls")
This returns a list of files and folders in the root of the hard disk.

Re: sending concatenated string through shell

Posted: Wed Sep 03, 2014 12:47 pm
by glynypig
Thanks for the reply, I should explain more. I am trying to get Livecode to turn on PHP through terminal and set the root of localhost to the folder playground. defaultFolder it seems is always the location of the livecode created app - I am simply trying to change this by concatenating "/playground" to the end of the shell("cd...") command

First my stack creates the playground folder as a subdirectory of my app folder and creates a standard index php file there - that all works fine and I can navigate to it and check it works.

Then I have Livecode execute shell to change directory through terminal and then get the default folder and append the playground folder to it like this...

Code: Select all

 put shell("cd" && quote & defaultFolder & slash & playground & quote) into mySomething
      put shell("php -S localhost:8000") into field 2
And later I kill any current php process running on OS X and start a new one - this all works fine, it is only the concatenating the folder name to defaultFolder issue and the folder definitely exists.

No matter how I attempt to concatenate "/playground" onto the back of the shell("cd defaultFolder") it seems that the root for my localhost is always my app directory and not the playground subfolder.

Re: sending concatenated string through shell

Posted: Wed Sep 03, 2014 1:00 pm
by glynypig
On second reading your response the penny dropped what you were trying to say and it now works. Thanks.

Instead of trying to concatenate "playground" onto the end of my change directory command I instead changed default folder

Code: Select all

 put the defaultFolder into newdefaultFolder
   set the defaultFolder to newdefaultFolder & "/playground"
      // initiate and launch php on mac osx
   put shell("cd" && quote & defaultFolder & quote) into mySomething
 put shell("php -S localhost:8000") into field 2
Is it right though that I have to basically "put" shell into some object in order for it to work? (in my example it's dead variables such as "mySomething") Can I not just execute it somehow? I am brand new to Livecode and still figuring out some of the basics - my background is in PHP

Re: sending concatenated string through shell

Posted: Wed Sep 03, 2014 1:15 pm
by Mark
Hi,

As I explained in my previous reply, you can't use the shell to change LiveCode's defaultfolder and changing the current directory in the shell won't change the active directory the next time the shell function is used. This example demonstrates it:

Code: Select all

on mouseUp
   put "pwd" into myShell
   put shell(myshell) into myOutput --> LC's defaultfolder
   put "cd /" into myShell --> move to root
   put shell(myShell) & cr after myOutput --> empty & cr
   put "pwd" into myShell
   put shell(myShell) & cr after myOutput --> still LC's default folder!
   put myOutput
end mouseUp
Your new attempt should work, but the line that contains the cd command is useless. You don't need to put the return value of the shell function into a variable or control. Most people use the it variable for this.

Code: Select all

put the defaultFolder & "/playground" into myNewDefaultFolder
set the defaultFolder to myNewDefaultFolder
get shell("php -S localhost:8000")
Edit: I wrote this while you posted another reply.

Kind regards,

Mark

Re: sending concatenated string through shell

Posted: Thu Sep 04, 2014 9:15 am
by [-hh]
..........

Re: sending concatenated string through shell

Posted: Thu Sep 04, 2014 4:49 pm
by glynypig
Hi, thanks for your input both of you, this is all really helping and I have a working demo - I will try your shortened version -hh next. However, right now, with my working demo the stack simply freezes once the shell command is run. I have tried not putting the shell into a variable or field and trying get instead but the same problem perssists. I also tried having a substack popup and self-close in the hopes it wouldnt freeze my main stack but it still freezes the main stack - it is like it is constantly monitoring the terminal (I notice this because when I do a killall php on the process I see the result from terminal if I have the shell put into a field (if that makes sense).

My aim here is to start-up php, set a localhost address and then elsewhere in my stack load in localhost through revbrowser - the purpose of which is to enable my users to use PHP within my app (maybe adventurous for my first app). The "grander/meta" purpose of which is to create a php educational ebook which allows the student to practice the exercises within the ebook and avoid having to use mamp etc - keeps things central.

Heres what I am doing in my "popup" substack which is hanging my app...

Code: Select all

on openCard
   get the defaultFolder
   put it into newdefaultFolder
   set the defaultFolder to newdefaultFolder & "/playground"
   // initiate and launch php on mac osx
   get shell("cd" && quote & defaultFolder & quote) 
   get shell("php -S localhost:8000") 
end openCard
I will experiment with your combined shell command -hh and see if it is any better

Re: sending concatenated string through shell

Posted: Thu Sep 04, 2014 5:34 pm
by [-hh]
..........

Re: sending concatenated string through shell

Posted: Thu Sep 04, 2014 11:50 pm
by glynypig
Thanks, the cd command it seems does have an effect for my purposes because performing a cd to a specific folder sets that folder as the root directory for the a php -S localhost:8000 server - the benefit to me is that by cd-ing to defaultFolder/playground and having Livecode create that folder means that php will use that folder as the root for the server wherever the app is installed on the users system. That is at least how it is done through terminal - maybe not the most ideal way to do it through Livecode but as I said I am brand new to it and truth be told no expert in macs terminal either. But I have tested doing it without the CD command and the localhost is finding its root elsewhere - god knows where as I am not aware of any terminal command to locate it.

I will try your method out - presumably I can also add my cd command into iShell as well.

I assumed the shell command was the cause of my stack hanging because the stack seems to be constantly listening to terminal - is there a way to terminate the shell/terminal session without killing the php process?

Re: sending concatenated string through shell

Posted: Fri Sep 05, 2014 12:23 am
by [-hh]
..........

Re: sending concatenated string through shell

Posted: Fri Sep 05, 2014 10:20 am
by glynypig
Wow -hh you just simplified that down a huge amount for me thank you - it goes to show how experience triumphs over reason any day.

I am trying to create an ebook where you can enter PHP into a text field next to an example and then see the results in another browser object on the same page - your first example does this wonderfully - although the html doesnt render but I am guessing this is because I am outputting to a text field and not an browser object. This has simplified things enormously, huge thanks for your help. Now I just need to get a browser object up and running but I am sure I can work that out.