| Syntax Elements | Source | Preprocessor | Types | Expressions | Backtick Expressions | Statements | Class | Logging |
A very powerful feature of scripting language - like unix shell or perl -
is the ability of inline evaluation of strings.
GroupOperator
: '`' StringLiteral '`'
;
The backtick enables inline expression evaluation in string literals, similar to Perl or sh
evaulation.
Single identifier, wrapped with
${}
will be resolved in current scope:
int i = 42;
String s = `"i is ${i}"`;
out.println(s);
|
This code prints:
i is 42
.
If the string between
${}
is not a single identifier it will be
evaluated as expression:
int i = 42;
String s = `"i is ${i + 1}"`;
out.println(s);
|
This code prints:
i is 43
.
The string between
!{}!
will be evaluated as normal
script. This part will be replaced with the content written to
out.
int i = 42;
String s = `"i is !{ out.print(i + 1); }!"`;
out.println(s);
|
This code prints:
i is 43
.
Note: writing to
err
is also possible, but this goes to
the standard err object.
The string between
@{}
has to be a identifier to
a StringArray.
String args = [ "a", "b" ];
String s = `"args is @{args}"`;
out.println(s):
|
i is a b
.
If you want to quote parts of the string, to avoid unwanted evaluation you can quote parts with
#{}#.
String hello = "hallo";
out.println(`"#{${hello}}# is ${hello}"`);
|
The first
${hello}
is quoted, so the output is:
${hello} is hallo
With
`{ }`
the enclosing command will be executed by the
operation system.
The command can contain itself contain string replacements.
Here an example:
String cdir = System.getAcdkHome();
String lsOutput = "";
String dirCmd = "";
if (System.getPlatformFlags() & acdk.lang.PfWin32)
dirCmd = "dir";
else
dirCmd = "ls";
lsOutput = `"`{${dirCmd} ${cdir}}`"`;
|
You can control more options for execution the external command with
variables declared in the current scope.
-
int SHELLEXECUTE_TIMEOUT
: only wait given milliseconds for finish
process execution. If the process was not finished after given milliseconds
the process will be killed.
The default is -1
, which means calling script waits for ever.
-
bool SHELLEXECUTE_USEFILEREDIRECT
: Use temporary files to capture
output of process.
Default is false
.
-
bool SHELLEXECUTE_USESHELL
: Don't execute the given executable directly
by underlying operation system, but use the system shell.
Default is false
.
-
String SHELLEXECUTE_SHELL
: If SHELLEXECUTE_USESHELL is true
define the shell executable, which should execute the command.
Default is "cmd"
on Windows and "sh"
on unix platforms.
-
String SHELLEXECUTE_EXECUTE_OPTION
: Option given to the shell
(SHELLEXECUTE_SHELL
).
Default is "/C"
on Windows and "-c"
on unix platforms.
-
bool SHELLEXECUTE_ISSHELLSCRIPT
: The command passed to shell
(SHELLEXECUTE_SHELL
) is not a native command, but itself a script.
Here a sample to use the shell:
SHELLEXECUTE_USESHELL = true;
String path;
if (System.getPlatformFlags() & acdk.lang.PfWin32)
path = `"`{echo #{%PATH%}#}`"`;
else
path = `"`{echo #{${PATH}}#}`"`;
out.println("PATH from shell is: " + path);
|