Collections:
Other Resources:
Run Second Command If the First Fails
How to run two commands with a condition that the second one will run only if the first one fails?
✍: FYIcenter.com
You can run two commands with a condition that
the second one will run only if the first one fails
with the syntax below:
command-1 || command-2
For example, if you want to write a message to the screen only after the "ping" command fails to reach a remote server, you can use the following command line:
ping host-name || echo Server not reachable
If you try this with google.com, the message will not be displayed:
C:\fyicenter>ping google.com || echo Server not reachable
Pinging google.com [172.217.10.78] with 32 bytes of data:
Reply from 172.217.10.78: bytes=32 time=23ms TTL=56
Reply from 172.217.10.78: bytes=32 time=23ms TTL=56
Reply from 172.217.10.78: bytes=32 time=30ms TTL=56
Reply from 172.217.10.78: bytes=32 time=29ms TTL=56
Ping statistics for 172.217.10.78:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 23ms, Maximum = 30ms, Average = 26ms
If you try this with a server that is not reachable, the message will be displayed:
C:\fyicenter>ping junk.com || echo Server not reachable
Pinging junk.com [23.23.86.44] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 23.23.86.44:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Server not reachable
2022-08-26, ∼2298🔥, 0💬
Popular Posts:
How to download ActivePerl 5.8.8.820? If you are interested in using Perl on your Windows system, yo...
How to remove quickset.exe from the startup application list to gain performance and reduce security...
Can I remove startup application "OSA9.exe - Microsoft Office Launch Helper" to speedup my computer ...
Can I remove startup application "atiptaxx.exe - ATI Desktop Control Panel" to speedup my computer a...
How to remove BacsTray.exe from the startup application list to gain performance and reduce security...