Using bash exit codes in Prolog

Hi everyone.
I’m using process_create/3 to run an external bash command and read the output. This is the code:

process_create(
            path(sshpass),
                ['-p', Pass, 'ssh', '-l', User, Host, Cmd],
                [process(PID), stdout(pipe(Out))]
            ),
            (read_lines(Out, Lines), atomic_list_concat(Lines, " ", Output)),
            close(Out)

(read_lines/2 is a custom predicate that read all lines of output.)

To go on with my code, I must know if the command succeeded or not. But, reading docs, I didn’t find anything for this purpose.
Is there a way to get the bash exit code of the process?

Thanks in advance.

Is it the small print at the very bottom of the docs?

Errors

  • process_error(Exe, Status) where Status is one of exit(Code) or killed(Signal). Raised if the process is waited for (i.e., Options does not include process(-PID)), and does not exit with status 0.

So you probably need to catch the error?

ie process_create/3 will throw this particular error if the exit code is not 0. You will also get the exit code as exit(Code).

Using catch/3 is one option. The other is to use the process(PID) option of process_create/3 and call process_wait/2

3 Likes

Relevant thread: How to get shell echo in Prolog variables

I haven’t gotten around to producing a version using strings, yet.

Thanks for your reply.

I used process_wait(PID, Exit) and then added a check of Exit value with Exit == exit(0).