ex37.pp 929 B

123456789101112131415161718192021222324252627282930313233343536
  1. Program Example37;
  2. { Program to demonstrate the Popen function. }
  3. uses linux;
  4. var f : text;
  5. i : longint;
  6. begin
  7. writeln ('Creating a shell script to which echoes its arguments');
  8. writeln ('and input back to stdout');
  9. assign (f,'test21a');
  10. rewrite (f);
  11. writeln (f,'#!/bin/sh');
  12. writeln (f,'echo this is the child speaking.... ');
  13. writeln (f,'echo got arguments \*"$*"\*');
  14. writeln (f,'cat');
  15. writeln (f,'exit 2');
  16. writeln (f);
  17. close (f);
  18. chmod ('test21a',octal (755));
  19. popen (f,'./test21a arg1 arg2','W');
  20. rewrite (f);
  21. if linuxerror<>0 then
  22. writeln ('error from POpen : Linuxerror : ', Linuxerror);
  23. for i:=1 to 10 do
  24. writeln (f,'This is written to the pipe, and should appear on stdout.');
  25. Flush(f);
  26. Writeln ('The script exited with status : ',PClose (f));
  27. writeln;
  28. writeln ('Press <return> to remove shell script.');
  29. readln;
  30. assign (f,'test21a');
  31. erase (f)
  32. end.