How can I call Cygwin command pipeline with the MATLAB 'system' function?
12 views (last 30 days)
Show older comments
MathWorks Support Team
on 5 Jul 2016
Answered: MathWorks Support Team
on 6 Jul 2016
I installed Cygwin on my Windows machine and tried to use the MATLAB 'system' function to execute a pipeline of Linux commands provided by Cygwin. The execution failed, although the same command pipeline worked fine directly in the Cygwin shell. Below is the MATLAB code and the fourth line constructs the command pipeline to be passed to the 'system' function.
>> inputfname = 'input.gz'; % the sample input is attached
>> nlines = 5;
>> outprefix = 'out';
>> pipeline = sprintf('date; gzip -dc %s | sed -n ''2p;4~2p'' | split -a 3 -d -l %d --numeric-suffix=1 --additional-suffix=%s --filter=''gzip > $FILE.gz'' - %s ; date', inputfname, nlines,'_part.txt', outprefix);
>> system(pipeline);
The MATLAB console output said:
'sed' is not recognized as an internal or external command, operable program or batch file.
How can I make this work?
Accepted Answer
MathWorks Support Team
on 5 Jul 2016
Disclaimer: Cygwin is a Linux emulator in Windows. Cygwin is not in the supported platform list. This article is meant as a self-help resource for customers with the similar issues.
In this issue, there are several things to understand to find the right solution:
1. The 'system' function of MATLAB only passes the command to the operating system (OS) and has no control over how the passed command is executed.
2. Installing Cygwin does not grant the OS the knowledge to the counterpart executables of the Linux commands. These executables reside under the 'bin' sub-directory under the Cygwin installation directory.
3. Forward slash ('\') works for Windows shell but not for the Cygwin shell.
4. Two single quotes ('''') do not act as a double quote ('"') in the Windows shell.
5. The semicolon (';') works as a pipeline in Cygwin (or real Linux) but not in a Windows shell.
With the above understanding, the code in the question should be revised as below. The primary change is to include the full paths to each invoked Cygwin commands.
>> clear;
>> cygwinPath = 'C:\cygwin\bin'; % the value depends on the actual installation of Cygwin
>> cw_gzip = fullfile(cygwinPath, 'gzip');
>> cw_sed = fullfile(cygwinPath, 'sed');
>> cw_split = fullfile(cygwinPath, 'split');
>> cw_date = fullfile(cygwinPath, 'date');
>> inputfname = 'input.gz';
>> nlines = 5;
>> outprefix = ''
>> system(cw_date); % separate call to 'date' without using ';' for pipeline separation, which is not supported in Windows
>> pipeline = sprintf(...
>> ['%s -dc %s | '...
>> '%s -n \"2p;4~2p\" | ' ...
>> '%s -a 3 -d -l %d --numeric-suffix=1 --additional-suffix=%s '...
>> '--filter=\"%s > $FILE.gz\" - %s/file'], ...
>> cw_gzip, name2, ...
>> cw_sed, ...
>> cw_split, nlines, '_r2_callqual.txt', ...
>> strrep(cw_gzip,'\','/'),...
>> outdir);
>> system(pipeline);
>> system(cw_date);
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!