Course of Raku / Objects, I/O, and exceptions / Input and output / Running external programs 🆕 / Exercises / Check the exit code

Solution: Check the exit code

Here is a possible solution to the task.

Code

my $proc = run 'sh', '-c', 'exit 4';

say $proc.exitcode;
say $proc ?? 'success' !! 'failure';

🦋 You can find the source code in the file check-exit-code.raku.

Output

4
failure

Comments

  1. run returns a Proc. Storing it in $proc — and then reading it — is important here: a failing command whose Proc is discarded would throw, but inspecting the result counts as handling it.

  2. .exitcode is the number the command returned: 4. Testing the Proc as a Boolean answers the simpler question — it is true only when the exit code is 0, so here it is false and the program prints failure.

Course navigation

Check the exit code   |   Pass a variable to a child