Shell Script OR Operator Questions

Asked 2 years ago, Updated 2 years ago, 76 views

service ssh status > /dev/null || sudo service ssh start

A script that runs the SSH service on the Linux cli.

The middle || operator is OR, right?

How does it work if the || operator is inserted between two commands, not just any conditional statement?

If you actually do it, there's nothing that stands out except for the start of SSH.

bash

2022-09-20 16:34

1 Answers

a || b

Run the a command first, and then run b only if the exit status of the command is not zero.

It's like a historical tradition, where a program returns an end state of zero if it's working as intended until the end, and a non-zero number if it's terminated in the middle with some exception.

Remember that after you run a, b only runs when it doesn't work normally.


2022-09-20 16:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.