From 4a2ae94f1dfe091b33c4fcc1e03e64862cc943bc Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Tue, 17 Apr 2007 14:44:46 +0000 Subject: wrapper for shell commands --- it.class | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'it.class') diff --git a/it.class b/it.class index 459691e..c3841d3 100644 --- a/it.class +++ b/it.class @@ -273,5 +273,33 @@ function filter_keys($array, $keys) return $result; } + +/** + * Construct shell command, log it, execute it and return output as string. + * @param varargs: scalars for cmd and arguments, assoc arrays to specify opts and opt values + * @return output of command. shell errors not detectable, see error_log in /www/server/logs + */ +function exec(/* ... */) +{ + $args = func_get_args(); + $parts = array(); + + foreach ($args as $arg) + { + foreach ((array)$arg as $key => $val) + { + if (it::match('^-', $val)) + it::fatal("leading - in value"); + else + $parts[] = (is_int($key) ? "" : escapeshellarg($key) . " ") . escapeshellarg($val); + } + } + + $cmd = join(" ", $parts); + it::log('exec', $cmd); + + return (string)shell_exec($cmd); +} + } ?> -- cgit v1.2.3