Running an external program with non-zero return values

I want to parse a diff from two versions of a file. Thought to do this by running diff as an external program and reading its output into a String:

read(`diff version1.txt version2.txt`, String)

This works fine and returns "" if both files have the same contents. However if the files differ julia throws an error

julia> read(`diff version1.txt version2.txt`, String)
ERROR: failed process: Process(`diff version1.txt version2.txt`, ProcessExited(1)) [1]

because the return value of the diff command is 1 if it finds differences. Even trying to catch this error does not provide me the output of the command:

julia> a = try read(`diff version1.txt version2.txt`, String)


How can I read output of bash commands with non-zero return values?

I’m not sure if there’s a built-in way to by-pass checking the return status.

EDIT: see the following post - wrap the command in ignorestatus


  Mark a command object so that running it will not throw an error if the result code is non-zero.

Here’s the code that’s run (from process.jl)

read(cmd::AbstractCmd, ::Type{String}) = String(read(cmd))::String

function read(cmd::AbstractCmd)
    procs = open(cmd, "r", devnull)
    bytes = read(procs.out)
    success(procs) || pipeline_error(procs)
    return bytes

Maybe write your own customized version that omits checking the return status?

1 Like
read(ignorestatus(`diff version1.txt version2.txt`), String)