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
failureComments
runreturns aProc. Storing it in$proc— and then reading it — is important here: a failing command whoseProcis discarded would throw, but inspecting the result counts as handling it..exitcodeis the number the command returned:4. Testing theProcas a Boolean answers the simpler question — it is true only when the exit code is0, so here it is false and the program printsfailure.