Home > atmlab > handy > exec_system_cmd.m

exec_system_cmd

PURPOSE ^

SYNOPSIS ^

function [out,retvals] = exec_system_cmd(cmd, varargin)

DESCRIPTION ^

 EXEC_SYSTEM_CMD Executes one or more system commands.

 Executes one or more system commands.

 You are strongly recommended to pass each of them through sanitise first,
 as to avoid potentially destroying the universe. The safest alternative
 is usually not to use exec_system_cmd if it can be avoided (e.g. for
 deleting files).

 If any command returns an error code not equal to 0, this function will
 throw an error with the error identifier 'atmlab:exec_system_cmd:shell'.

 IN

   cmd: string or cell array of strings

       Command to pass to the shell. If this is a cell array of strings,
       each string will be apssed to the shell in turn.


   verbose: bool, optional. Defaults to false

       If true, each command with be printed to the screen before it is
       executed.

   continueOnError: bool, optional. Defaults to false

       

 OUT

   out: cell array of strings

       For each command passed as input, the output (stdout+stderr)
       is collected in a string. Those strings are then collected to
       a cell array of strings. If only one input command was given,
       this is a cell array with one string.

    retvals: boolean return value if second optional input 'report' is given
       
 USAGE

   [out,ret] = exec_system_cmd(cmd, varargin)

 See also: sanitise

 updated by: Salomon, Gerrit
 Author: Oliver Lemke

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

exec_system_cmd.m

SOURCE CODE ^

0001 function [out,retvals] = exec_system_cmd(cmd, varargin)
0002 %
0003 % EXEC_SYSTEM_CMD Executes one or more system commands.
0004 %
0005 % Executes one or more system commands.
0006 %
0007 % You are strongly recommended to pass each of them through sanitise first,
0008 % as to avoid potentially destroying the universe. The safest alternative
0009 % is usually not to use exec_system_cmd if it can be avoided (e.g. for
0010 % deleting files).
0011 %
0012 % If any command returns an error code not equal to 0, this function will
0013 % throw an error with the error identifier 'atmlab:exec_system_cmd:shell'.
0014 %
0015 % IN
0016 %
0017 %   cmd: string or cell array of strings
0018 %
0019 %       Command to pass to the shell. If this is a cell array of strings,
0020 %       each string will be apssed to the shell in turn.
0021 %
0022 %
0023 %   verbose: bool, optional. Defaults to false
0024 %
0025 %       If true, each command with be printed to the screen before it is
0026 %       executed.
0027 %
0028 %   continueOnError: bool, optional. Defaults to false
0029 %
0030 %
0031 %
0032 % OUT
0033 %
0034 %   out: cell array of strings
0035 %
0036 %       For each command passed as input, the output (stdout+stderr)
0037 %       is collected in a string. Those strings are then collected to
0038 %       a cell array of strings. If only one input command was given,
0039 %       this is a cell array with one string.
0040 %
0041 %    retvals: boolean return value if second optional input 'report' is given
0042 %
0043 % USAGE
0044 %
0045 %   [out,ret] = exec_system_cmd(cmd, varargin)
0046 %
0047 % See also: sanitise
0048 %
0049 % updated by: Salomon, Gerrit
0050 % Author: Oliver Lemke
0051 
0052 if ~iscell(cmd), cmd = {cmd}; end
0053 
0054 cmd = cmd(:);
0055 
0056 [verbose,continueOnError] = optargs(varargin, {false,false});
0057 
0058 v = atmlab('VERBOSITY');  
0059 atmlab('VERBOSITY',verbose);
0060 vObj=onCleanup(@()(atmlab('VERBOSITY',v)));
0061 
0062 errID = 'atmlab:exec_system_cmd:shell';
0063 out = cell(1,length(cmd)); i = 1; % keep track of output messages
0064 retvals = zeros(1,length(cmd)); % keep track of return messages (if report is input)
0065 for x = cmd'
0066     fullcmd = char(x);
0067     
0068     if verbose>0, logtext(atmlab('OUT'),'%s\n',fullcmd); end
0069     
0070     [ret,out{i}] = system(fullcmd);
0071     
0072     if verbose>0 && ~isempty(out{i})
0073         logtext(atmlab('OUT'),'%s\n',out{i}(1:end-1))
0074     end
0075     
0076     if (ret ~= 0)
0077         if any(~cellfun('isempty',regexp(out{i},...
0078                 {'symbol lookup error',...
0079                 'dyld: Symbol not found',...
0080                 'library version mismatched'},'start')))
0081             error(errID,[ [ out{i} '\n'],...
0082                 'A shared library conflict occured while running the command.\n'...
0083                 'This may be due to the fact that MATLAB comes with its own versions of some system libraries\n',...
0084                 'To use the system libraries do:\n',...
0085                 'If you are on a MAC, try setenv(''DYLD_LIBRARY_PATH'','''')\n',...
0086                 'If you are using LINUX, try setenv(''LD_LIBRARY_PATH'','''')\n',...
0087                 'If this solves the problem, add the corresponding line to your startup file'])
0088         else
0089             if continueOnError, retvals(i) = ret;
0090             else
0091                 error(errID,'Command ''%s'' returned error %d: "%s"', fullcmd, ret, out{i})
0092             end
0093         end
0094     end
0095     i = i+1;
0096 end
0097 
0098 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005