Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-04

Exit codes of called programs.

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

A.J. Venter

Hi,
I often write frontends for commandline programs.
When I need their output, I call assignstream, when I don't I use shell.

What I would like however, is a way to check the exit codes of such programs, so that I can for example tell if a program failed to run correctly. Not all programs have output, and you cannot always get the output via assignstream, not to mention that you need to do a lot of string parsing to make sense of it when you do. This is not always easy, as you cannot always recognize every possible error message.

A system that can check wether the exit status was zero, and report and error if it wasn't would be much more ellegant
How could I ran another program from within my own, while detecting the exit code ?
I foresea something like this.

If runWithExitStatus('ls -alh')  0 then
Begin
	Writeln ('Error reading files');
	halt(1);
end;
Story of my life: "Semper in excretum, set alta variant" A.J. Venter DireqLearn Linux Guru
Jonas Maebe
to A.J. Venter
A.J. Venter wrote 7 lines
I often write frontends for commandline programs. When I need their output, I call assignstream, when I don't I use shell. What I would like however, is a way to check the exit codes of such programs, so that I can for example tell if a program failed to run correctly.

If you use shell, you can only get the exit code of the shell (since that is the program that your program started). If you use Dos.exec(), use the DosExitCode() function afterwards. AssignStream doesn't seem to be able to return exitcodes.

Jonas

A.J. Venter
to Jonas Maebe
Jonas Maebe wrote 14 lines
On dinsdag, apr 15, 2003, at 10:50 Europe/Brussels, A.J. Venter wrote: > I often write frontends for commandline programs. > When I need their output, I call assignstream, when I don't I use > shell. > > What I would like however, is a way to check the exit codes of such > programs, so that I can for example tell if a program failed to run > correctly. If you use shell, you can only get the exit code of the shell (since that is the program that your program started). If you use Dos.exec(), use the DosExitCode() function afterwards. AssignStream doesn't seem to be able to return exit codes.

OK so shell is out, after all I am not all that concerned with how bash exited. Assignstream as well. I did not even know that dos exec could work under Linux, I somehow seem to think this is a less than adequate approach though.

How do C programmers do it under Linux ? I am sure it must be possible to use the same technique.

A.J.

Story of my life: "Semper in excretum, set alta variant" A.J. Venter DireqLearn Linux Guru
Jonas Maebe
to A.J. Venter
A.J. Venter wrote
OK so shell is out, after all I am not all that concerned with how bash exited. Assignstream as well. I did not even know that dos exec could work under Linux, I somehow seem to think this is a less than adequate approach though.

The "dos" unit is simply an implementation of Borland's Dos unit, ie. it has the same interface, but the implementation is completely OS-dependent. There's nothing bad about using it under Linux or Win32. It doesn't allow you to capture the output of a program though. popen() (in the Linux unit) does, but I don't know whether it allows you to get the exit code. Check the documentation (units.pdf).

Jonas

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 14 lines
On woensdag, apr 16, 2003, at 15:28 Europe/Brussels, A.J. Venter wrote: > OK so shell is out, after all I am not all that concerned with how bash > exited. Assignstream as well. I did not even know that dos exec could > work under Linux, I somehow seem to think this is a less than adequate > approach though. The "dos" unit is simply an implementation of Borland's Dos unit, ie. it has the same interface, but the implementation is completely OS-dependent. There's nothing bad about using it under Linux or Win32. It doesn't allow you to capture the output of a program though. popen() (in the Linux unit) does, but I don't know whether it allows you to get the exit code. Check the documentation (units.pdf).

It does if you use pclose() to close it the file.
The return value of pclose is the exit code of the program.

Michael.