-
Notifications
You must be signed in to change notification settings - Fork 23
ImplOfProgram
TODO
Program.shell
returns the full path of the default system shell
under Linux / Mac OS X it will be /bin/bash
same as the env var SHELL
under Windows it will be C:\Windows\system32\cmd.exe
same as the env var %COMSPEC%
TODO
see
- C++ reference - system()
- C Programming FAQ - Run a program from within a program
- rosettacode - Execute a system command
We require to have Cygwin installed to run command-line programs under Windows but there are still subtleties to understand.
The default command prompt (or console) under Windows is cmd.exe
when running Cygwin our console shell will become /bin/bash
Here some special cases
-
system( "cmd" )
fromC.stdlib
block the current process
call the default system shell as child of the current process
execute thecmd
and wait for it to return -
popen()
fromC.stdio
call the default system shell as child of the current process
and give you control over the input or output file stream
returns the input or outputFILE
-
Program.open()
call the default system shell as child of the current process
works likepopen()
but only control the output stream
and returns the output stream as aString
-
exec()
from
replace the current process with a new process -
fork()
TODO -
wait()
TODO -
spawn()
TODO -
pipe()
TODO
TODO
With cmd.exe
you can execute the ver
command
C:\> ver
output Microsoft Windows [Version 6.3.9600]
Cygwin shell can not execute such commands
$ ver
output -bash: ver: command not found
You need to call cmd.exe
from the Bash shell
$ cmd /c ver
output Microsoft Windows [Version 6.3.9600]
But if you use system()
, popen()
or Program.open()
you can directly use the ver
command even if you are under Cygwin
#!/usr/bin/as3shebang --
import shell.*;
var str:String = Program.open( "ver" );
trace( str );
$ ./test_version
output Microsoft Windows [Version 6.3.9600]
But the inverse is not true,
Program.open( "ls -la" )
will work from a Bash shell
but it will not work from cmd.exe
If you need to run the Bash shell from cmd.exe
you will need to add its location to the %PATH%
test.as
import shell.*;
var str:String = Program.open( "ls -la" );
trace( str );
from cmd.exe
C:\> redshell.exe test.as
you will obtain
'ls' is not recognized as an internal or external command,
operable program or batch file.
to make it work you need to modify the PATH
env var
C:\> set PATH=%PATH%;c:\cygwin\bin
and then C:\> redshell.exe test.as
will work
also you can run bash in different ways
Program.open( "ls -la" )
Program.open( "bash -c 'ls -la'" )
Program.open( "bash.exe -c 'ls -la'" )
Another way to do it is to directly modify the env vars from the script
import shell.*;
import C.stdlib.*;
var PATH:String = getenv( "PATH" );
putenv( "PATH=" + PATH + ";C:\\cygwin\\bin" );
var str:String = Program.open( "ls -la" );
trace( str );
C:\> redshell.exe test.as
will work directly
with the advantage that the %PATH%
was modified only
in the redshell process and is not changed for cmd.exe
itself
Another thing to know is that cmd.exe
will run only what he knows
.exe
, .bat
, etc. and so it will not run your AS3 shell scripts
but there is a way ;)
with Program.open( "bash -c '/path/to/shellscript'" )
you can
if for example you have test_sysinfo
in the current directory
test_sysinfo
#!/usr/bin/as3shebang --
import shell.*;
var p:Array = [ "api", "apiVersion" , "architecture",
"description", "endian", "platform",
"redtamarin", "swfVersion", "version" ];
for( var i:uint = 0; i < p.length; i++ )
{
var m:String = p[i];
trace( "Runtime." + m + " = " + Runtime[m] );
}
to run that shell script from cmd.exe
use var str:String = Program.open( "bash -c './test_sysinfo'" );
and you will get the output of the script in your cmd.exe
prompt
see
- MSDN Process and Environment Control
- MSDN system, _wsystem functions
- MSDN _exec, _wexec functions
- MSDN _spawn, _wspawn functions
- Replacing cmd.exe
- Set PowerShell as Your Default Shell in Windows 2012 Core
- Different Shells for Different Users
- Changing the default Windows Shell in Windows 7
- Changing the default command-line shell in Windows
- MSDN ShellExecute function
- MSDN Windows Shell
- The CYGWIN environment variable