[fpc-pascal] Exec(), Linux, and /dev/null redirection

cobines cobines at gmail.com
Wed Feb 17 02:45:38 CET 2010


2010/2/17 Zitt Zitterkopf <zittware at hotmail.com>:
> Exec( '/bin/mount', '/dev/'+ aline +' /media/' + aline + ' &> /dev/null');

Try:

Exec( '/bin/mount', '/dev/'+ aline +' /media/' + aline + ' 1>/dev/null');

for redirecting STDOUT or

Exec( '/bin/mount', '/dev/'+ aline +' /media/' + aline + ' 2>/dev/null
1>/dev/null');

for redirecting STDOUT and STDERR.

> I tried the following kludge instead:
> var
> OurPipe    : Text;
>
>          popen( OurPipe, '/bin/mount /dev/'+ aline +' /media/' + aline,
> 'R');
>          Try
>              if (fpgeterrno=0) then Flush (OurPipe)
>              ELSE MessageBox ('mount pipe error ' + IntToStr(fpgeterrno),
> nil,
>                   mfError or mfOKButton);
>          Finally
>              PClose( OurPipe );
>          END;

This works for me:

var f: textfile;
    s: string;
begin
  if popen(f, '/bin/mount /dev/'+ aline +' /media/' + aline + ' 2>&1',
'r') = 0 then
  begin
    readln(f,s);

    // test output 's'

    pclose(f);
  end
  else
    error := fpgeterrno;   // failed
end;

This puts stdout and stderr into one pipe however. There must be some
way to read from stdout and stderr separately, maybe using TProcess,
but I've never needed it so I don't know how.

--
cobines



More information about the fpc-pascal mailing list