Executes an external program and returns the output as a string.
In the given command line the placeholder `%s` is substituted with the input value.
+The provided command line is split on spaces before it is passed to the shell.
+Using single quotes (`'`) splitting can be avoided (e.g. `'%s'` would prevent splitting on spaces within the input value).
+The surrounding single quotes are removed.
+
The external program must either be in the executable search path of the server process, or an absolute path has to be used.
For security reasons all commands need to be whitelisted.
### General Setup
-**Item**
+#### Item
This will replace the visible label in the UI with the transformation you apply with the command <TransformProgram>.
String yourItem "Some info [EXEC(/absolute/path/to/your/<TransformProgram> %s):%s]"
```
-**Rule**
+#### Rule
```java
rule "Your Rule Name"
When the input argument for `%s` is `fri` the execution returns a string with the last weekday of the month, formated as readable text.
-```
+```shell
Fri 31 Mar 2017 13:58:47 IST`
```
Please note: This profile is a one-way transformation, i.e. only values from a device towards the item are changed, the other direction is left untouched.
-# Further Reading
+## Further Reading
* [Manual](http://man7.org/linux/man-pages/man1/date.1.html) and [tutorial](https://linode.com/docs/tools-reference/tools/use-the-date-command-in-linux/) for date.
* [Manual](http://man7.org/linux/man-pages/man1/numfmt.1.html) and [tutorial](https://www.pixelbeat.org/docs/numfmt.html) for numfmt.
-
-
package org.openhab.transform.exec.internal;
import java.time.Duration;
+import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@NonNullByDefault
@Component(property = { "openhab.transform=EXEC" })
public class ExecTransformationService implements TransformationService {
+ private static final Pattern SPLIT_ON_SPACE = Pattern.compile("(['])((?:\\\\\\1|.)+?)\\1|([^\\s']+)");
private final Logger logger = LoggerFactory.getLogger(ExecTransformationService.class);
private final ExecTransformationWhitelistWatchService execTransformationWhitelistWatchService;
long startTime = System.currentTimeMillis();
String formattedCommandLine = String.format(commandLine, source);
- String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5),
- formattedCommandLine.split(" "));
+ String[] cmdLineParts = SPLIT_ON_SPACE.matcher(formattedCommandLine).results()
+ .map(mr -> mr.group(2) == null ? mr.group() : mr.group(2)).toArray(String[]::new);
+ String result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(5), cmdLineParts);
logger.trace("command line execution elapsed {} ms", System.currentTimeMillis() - startTime);
return result;