Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash ignore specific error code (pytest no tests warning error code 5)

I have a program which tests the error code of a script/command I provide. In some cases the command line returns a warning error code which i would like to ignore (exit with 0 instead of 5). I would like other error codes to pass the same, and for that to be a simple as possible (to keep that a single line command).

I've tried using pytest || [ $? -eq 5 ], which ignores error code 5 coming from command but will replace all non-zero error codes with 1 (instead of the actual error code).

I guess someone more experienced than me with bash could come up with a better solution faster than I.

Some more specific context:

I'm using pytest to test my projects. Some of them have no tests at the moment and pytest's default behavior is to treat that as an error (error code 5). I would like to treat that error as a success. Actually, I would like Travis to threat that error as success.

Thanks!

like image 363
NirIzr Avatar asked Sep 03 '25 09:09

NirIzr


2 Answers

Using arithmetic expression:

exit $(( $? == 5 ? 0 : $? ))
like image 152
Ruslan Osmanov Avatar answered Sep 04 '25 22:09

Ruslan Osmanov


You just have to catch, test, and (re)raise the appropriate status.

command
case $? in
  5) exit 0 ;;
  *) exit ;;
esac
like image 41
chepner Avatar answered Sep 04 '25 23:09

chepner