Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
system command and user input

Subject: system command and user input

From: Misha Koshelev

Date: 27 Jul, 2009 15:47:01

Message: 1 of 2

Hi, I had a quick question and was wondering if anyone can help. I was debugging what I thought was a bug in my program - turns out it is a "feature" of the system or unix commands. Specifically if one does something like:

 [a,b] = system('find / -name a');

The resulting output b will include any keypresses that the user makes
while the command is executing.

I would like to somehow isolate my output from this - i.e. I would like to execute the command without having any user input creep into the output. Any way to do this?

system('find / -name a < /dev/null');

does not seem to work. Thank you
Misha

Subject: system command and user input

From: Misha Koshelev

Date: 30 Jul, 2009 07:03:02

Message: 2 of 2

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <h4ki5l$of4$1@fred.mathworks.com>...
> Hi, I had a quick question and was wondering if anyone can help. I was debugging what I thought was a bug in my program - turns out it is a "feature" of the system or unix commands. Specifically if one does something like:
>
> [a,b] = system('find / -name a');
>
> The resulting output b will include any keypresses that the user makes
> while the command is executing.
>
> I would like to somehow isolate my output from this - i.e. I would like to execute the command without having any user input creep into the output. Any way to do this?
>
> system('find / -name a < /dev/null');
>
> does not seem to work. Thank you
> Misha

function [status, result] = misha_system(command)
% MISHA_SYSTEM Like system function but user input will _NOT_ enter result.
% [STATUS, RESULT] = MISHA_SYSTEM(COMMAND) Normally, [STATUS, RESULT] =
% SYSTEM(COMMAND) will allow user typing to enter RESULT. MISHA_SYSTEM
% will never allow such a travesty.
%
% Notes: Only works on UNIX
%
% Misha Koshelev
% July 30, 2009
% Montague Laboratory

    if ~exist('command', 'var')
        fprintf(['Error: command parameter must be specified in call to ' ...
                 'misha_system.\n']);
        return;
    end
    
    temp = tempname;
    result = [];
    [status, throw_away_result] = system(...
        [command ...
         sprintf(' > %s 2>&1'), ...
         temp]);
    
    if status || ...
            ~exist(temp, 'file')
        
        return;
        
    end
        
    file = fopen(temp, 'r');
    result = char(...
        fread(file))';
    fclose(file);
    
    delete(temp);
                          
---
Also on file exchange.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us