Howto do command sequence in linux with conditional check

One of the cool things you can do in your CLI (Command Line Interface) or so called Terminal console is triggering one line of multi command sequence with conditional check. I'm always using this trick when I don't wanna wait for the first command which takes some minutes to finish to trigger the next command. Some people wrote a bash script file to do it but today i wanna show you how to do it in one single line of command. One of my common use of this trick is to trigger a loud song or alert after my dd command (which takes about 15 minutes) to copy a 2GB image to my SD cards.

So, while waiting for these command to finish, I can do something else... Like playing Gangster Battle on Facebook 😜 or chatting on the IRC (wave me @ irc.freenode.net on #ubuntu-my)

Here's the howto:

Executing two or more commands in sequence

To execute two or more commands in sequence regardless of the failure/success of the previous command, you can use the semi colon ";" in between of your commands. For example:

$ mycommand1 ; mycommand2

In this example, mycommand2 will be executed after mycommand1 have been accomplished.

Execute the next command only if the first command fails

To execute the next command in condition of when the first command fails, you can use logical OR operator which is double pipes "||". I'm not sure what you call them but here's the example:

$ mycommand1 || mycommand2

mycommand2 in this example will only be executed if mycommand1 failed.

Execute the next command only if the first is successful

I hope you got the idea now. Yes, we can use logical operator in between of our commands. To execute the next command in condition of when the first command is successfully accomplished, you can use logical AND operator which is double ampersand "&&". Look at this example:

$ mycommand1 && mycommand2

In this last example, mycommand2 will only be executed when mycommand1 is successful.

That's all for now mates. Hope you can have more control on your linux terminal console. Have fun!! 😁

Comments

dolphin said…
hail to master l33t 455!!
bila nak pekena nasik gulai kawah ?
jual sticker banyak dah masyukkk
Unknown said…
wowwww,

this is deep.

x terjangkau dek minda saya yg kecil nih.

thanx for stopping by my blog!
ApOgEE said…
Hi VersedAnggerik, thanks for visiting & commenting my blog. saya pun tak terjangkau juga kalau u kasi terms undang-undang... hehehe
flip9 said…
waa~
baru saya faham..
thnx!

Popular posts from this blog

How to Create Hyperlink on Blogger Post

How to Add a Sudo User on AlmaLinux 9.2 (Turquoise Kodkod): A Step-by-Step Guide

How to Show and Hide Text in Blog Post