I am currently exploring Debian packages, and I have been reading some code samples. And on every line in, for example, the postinst script is a pattern.
some command || true another command || true
So if some command fails, then the line returns true but I don’t see how this affects the output of the program.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The reason for this pattern is that maintainer scripts in Debian packages tend to start with set -e, which causes the shell to exit as soon as any command (strictly speaking, pipeline, list or compound command) exits with a non-zero status. This ensures that errors don’t accumulate: as soon as something goes wrong, the script aborts.
In cases where a command in the script is allowed to fail, adding || true ensures that the resulting compound command always exits with status zero, so the script doesn’t abort. For example, removing a directory shouldn’t be a fatal error (preventing a package from being removed); so we’d use
rmdir ... || true
since rmdir doesn’t have an option to tell it to ignore errors.
Method 2
While it does not affect the output of the program just run – it permits the caller to proceed as if all is okay aka affects future logic.
Rephrased: it masks the error status of the previous command.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f4f0faf1f8fcf5d9e1a9aea8">[email protected]</a>:[/usr/sbin]cat /tmp/false.sh #!/bin/sh false <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f8fcf6fdf4f0f9d5eda5a2a4">[email protected]</a>:[/usr/sbin]cat /tmp/true.sh #!/bin/sh false || true <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19c98929990949db189c1c6c0">[email protected]</a>:[/usr/sbin]sh /tmp/false.sh; echo $? 1 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92fffbf1faf3f7fed2eaa2a5a3">[email protected]</a>:[/usr/sbin]sh /tmp/true.sh; echo $? 0
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0