KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Executing Shell Tasks with LAUNCH EXTERNAL PROCESS
PRODUCT: 4D | VERSION: 13.1 | PLATFORM: Mac & Win
Published On: November 20, 2012

To some extent this Tech Tip is about replacing the obsolete 4D Pack command AP ShellExecute. The point of AP ShellExecute (and this Tech Tip) is to launch external tasks from 4D but have those tasks execute within the context of the Operating System "shell". On Windows the shell is "cmd.exe". OS X includes several shells, but the default is "bash".

To be clear the only command that can be used to do this in 4D v13 is LAUNCH EXTERNAL PROCESS (LEP), so this Tech Tip focuses on how to use LEP to start up a shell.

What kinds of tasks might require the use of a shell? Any command line task requires a shell in which to execute. The "copy" command on Windows and "cp" on OS X are examples of shell commands. The execution of batch (Windows) or script (OS X) files is probably the most common example from a 4D perspective.

Here are some examples:

The "rmdir" command can be used to delete a folder on Windows (including all contents), but it requires a shell. The code in 4D to delete the folder C:\My Folder could look like this:

$command_t:="cmd.exe /C rmdir /Q /S \"C:\\My Folder\""
LAUNCH EXTERNAL PROCESS($command_t)


To delete the folder /Users/me/My Folder on OS X, using the "rm" command, it would look like:

$command_t:="/bin/bash -c rm -rf \"/Users/me/My Folder\""
LAUNCH EXTERNAL PROCESS($command_t)


Note: In both of the preceding examples the paths are surrounded by double-quotes because they contain spaces. Also note that the OS X path must be in POSIX format; HFS paths are not supported from the shell. Since 4D v12, 4D contains commands to automatically convert between the two (Convert path system to POSIX and Convert path POSIX to system)

Tips

Here are some tips for working with LEP in this context...

Use Relative Paths

Set the current directory prior to calling LEP. This allows for the use of relative paths instead of absolute paths. Absolute paths can fail with certain commands that do not accept quoted paths. It also makes it easier to write generic code that works in all environments.

SET ENVIRONMENT VARIABLE("_4D_OPTION_CURRENT_DIRECTORY";$currentDirectory_t)


Hide the Console Window

Unless you want to see the console window, hide it like this:

SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")


Passing Commands to CMD.exe

The syntax to use cmd to execute a command is "cmd.exe /C string" or "cmd.exe /K string". The content of "string" should be the command you want to execute. Use the "/C" switch to close the console window for "cmd.exe"; use the "/K" switch if you want the window to remain open.

$LEPCommand_t:="cmd.exe /C"+$shellCommand_t


LEP Streams

Use BLOB's for the streams to avoid any truncation (text variables may be truncated).

Asynchronous LEP

Use asynchronous execution so as not to block other 4D tasks:

SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")


If the output of the LEP call is important, it needs to be directed elsewhere (it will not be returned via the streams). Output to a text file works well.

Environment Control

Use "start" in conjunction with /c to have full control over the environment. For example /B will start an application without creating a console window. The switch /I resets the environment to the default of cmd rather than the current environment ("environment" means things like Environment Variables).

Performance Considerations

For better performance, rather than make several LEP calls for each shall command, dump the shell commands into a batch/script file and execute that.

OS X Script Exectution

On OS X the path to the script file must be, at minimum, a full relative path unless the script can be located on the environment path. For example if the script is in the current directory, simply precede it with "./":

$pathToscript_t:="./script.sh"


Research!

Perhaps the most important tip: if you need help, search! Remember that once LEP launches a shell, 4D no longer has any control over it. Expertise when using the OS shell will not and should not come primarily from 4D resources. The shells themselves have built-in documentation:

Windows - open a Command Prompt and type:

cmd /?


OS X - open a Terminal window and type:

man bash


There is a wealth of information available on sites like Stack Overflow to help with complex shell commands as well.