Prototype: execresult_as_data(command, shell, output)

Return type: data

The return value is cached.

Description: Execute command and return a data container including command output and exit code.

Functions in the same way as execresult(), and takes the same parameters. Unlike execresult(), and returnszero(), this function allows you to test, store, or inspect both exit code and output from the same command execution.

Arguments:

  • command: string, in the range: .+
  • shell: one of
    • noshell
    • useshell
    • powershell
  • output: one of
    • both
    • stdout
    • stderr

Example:

Policy:

body common control
{
      bundlesequence  => { "example" };
}

bundle agent example
{
  vars:
      "my_data"
        data => execresult_as_data("echo 'hello'", "useshell", "both");
      "my_json_string"
        string => storejson(my_data);

  reports:
      "echo 'hello' returned '$(my_json_string)'";

}

Output:

R: echo 'hello' returned '{
  "exit_code": 0,
  "output": "hello"
}'

Notes: you should never use this function to execute commands that make changes to the system, or perform lengthy computations. Consider using commands promises instead, which have locking and are not evaluated by cf-promises.

See also: execresult().

History:

  • Introduced in 3.17.0