Movatterモバイル変換


[0]ホーム

URL:


CFEngine documentation homepage

execresult

Suggest changes
Table of contents

Prototype:execresult(command, shell, output)

Return type:string

The return value iscached.

Description: Executecommand and return output (bothstdout andstderr) asstring.

If the command is not found, the result will be the empty string.

Theshell argument decides whether a shell will be used to encapsulate thecommand. This is necessary in order to combine commands with pipes etc, butremember that each command requires a new process that reads in files beyondCFEngine's control. Thus using a shell is both a performance hog and apotential security issue.

The optionaloutput argument allows you to select which output will beincluded, betweeenstdout,stderr, orboth. The default isboth.

Arguments:

  • command:string - Fully qualified command path - in the range:.+
  • shell: - Shell encapsulation option - one of
    • noshell
    • useshell
    • powershell
  • output: - Which output to return; stdout or stderr - one of
    • both
    • stdout
    • stderr

Example:

Prepare:

code
rm -rf /tmp/testheremkdir -p /tmp/testheretouch /tmp/testhere/atouch /tmp/testhere/btouch /tmp/testhere/ctouch /tmp/testhere/dtouch /tmp/testhere/eecho "#!/usr/bin/env sh" >/tmp/testhere/echo-stdout-and-stderrecho "echo stderr >&2" >>/tmp/testhere/echo-stdout-and-stderrecho "echo stdout" >>/tmp/testhere/echo-stdout-and-stderrchmod +x /tmp/testhere/echo-stdout-and-stderr

Policy:

code
bodycommoncontrol{bundlesequence=>{"example"};}bundleagentexample{vars:"my_result"string=>execresult("/bin/ls /tmp/testhere",noshell);"my_result_with_stdout_and_stderr"string=>execresult("/tmp/testhere/echo-stdout-and-stderr",noshell);"my_result_with_stdout"string=>execresult("/tmp/testhere/echo-stdout-and-stderr 2>/dev/null",useshell);"my_result_with_stderr"string=>execresult("/tmp/testhere/echo-stdout-and-stderr 1>/dev/null",useshell);reports:"/bin/ls /tmp/testhere returned '$(my_result)'";"my_result_with_stdout_and_stderr == '$(my_result_with_stdout_and_stderr)'";"my_result_with_stdout == '$(my_result_with_stdout)'";"my_result_with_stderr == '$(my_result_with_stderr)'";}

Output:

code
R: /bin/ls /tmp/testhere returned 'abcdeecho-stdout-and-stderr'R: my_result_with_stdout_and_stderr == 'stderrstdout'R: my_result_with_stdout == 'stdout'R: my_result_with_stderr == 'stderr'

Notes: you should never use this function to execute commands thatmake changes to the system, or perform lengthy computations. Such anoperation is beyond CFEngine's ability to guarantee convergence, andon multiple passes and during syntax verification these function callsare executed, resulting in system changes that arecovert. Callstoexecresult should be for discovery and information extractiononly. Effectively calls to this function will be also repeatedlyexecuted bycf-promises when it does syntax checking, which ishighly undesirable if the command is expensive. Consider usingcommands promises instead, which have locking and are not evaluatedbycf-promises.

See also:returnszero(),execresult_as_data().

History:

  • 3.0.5 Newlines no longer replaced with spaces in stored output.
  • 3.17.0 Introduced optional parameteroutput added allowing selection of stderr, stdout or both.

Still need help?

Chat Ask a question on Github Mailing list
Version 
master3.24 (LTS)3.21 (LTS)view all versions

[8]ページ先頭

©2009-2025 Movatter.jp