Categories:
Administration (10)
Adware Spyware (7)
Apache (10)
Bluetooth (125)
DOS Commands (1)
Edge (28)
General (2)
Internet Explorer (217)
Media Center (12)
Media Player (11)
Mozilla Firefox (18)
PHP (15)
Programming (8)
Security (4)
Silverlight (15)
Tips (17)
Tools (218)
Tutorials (5)
Windows 10 (166)
Windows 7 (636)
Windows 8 (1091)
Windows Phone (33)
Windows Server 2008 (165)
Windows Server 2012 (84)
Windows Server 2016 (23)
Windows Vista (52)
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, 1265👍, 0💬
Popular Posts:
What is "SynTPEnh Caller Service" in my Windows 7 service list? And how is "SynTPEnh Caller Service"...
How to start Apache server manually on Windows systems? If you followed our Apache server installati...
I have created a table in MS Access. How do I create a form to help enter data into the table? Assum...
What is "Superfetch" in my Windows 7 service list? And how is "Superfetch" service related to SysMai...
Every time I open Internet Explorer a bar pops up saying that it is runner with out add ons. I check...