Executing two or more commands in sequence - Why/When/How

Forum rules
Before you post please read how to get help

Executing two or more commands in sequence - Why/When/How

Postby Habitual on Tue Jul 26, 2011 1:47 pm

As you may or may not know every command exits with a status. An exit status of 0 is true, while everything else is false.
Additionally, the exit status is stored in the $? variable.

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

Code: Select all
$ mycommand1 ; mycommand2

In this example, mycommand2 will be executed after mycommand1 have been accomplished, successfully, or unsuccessfully.
you can test this with these 2 commands by using:
Code: Select all
false ; whoami
true ; whoami

whoami executes in both examples regardless of the true|false command's exit status.

[Logical OR] OR logical operator
To execute the next command in condition of when the first command fails, you can use logical OR operator which is double pipes "||".
Code: Select all
$ mycommand1 || mycommand2


mycommand2 in this example will only be executed if mycommand1 failed.
you can test this with these 2 commands by using:
Code: Select all
false || whoami
true || whoami


[Logical AND]AND logical operator.
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:
Code: Select all
$ mycommand1 && mycommand2 && myucommand

You typically see this when you have to install from source
Code: Select all
./configure && make && make install

each successive command depends on the successful completion of the previous.

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

I am not a programmer. I am a "Machete Coder" (Chop and Hack / Trial and Error methods)
So don't hold me responsible! As will all things Linux, verify it for yourself!

References:
Command Lists Logical Operators
Advanced Bash-Scripting Guide

Enjoy!
Last edited by Habitual on Tue Jul 26, 2011 10:00 pm, edited 1 time in total.
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2274
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Linux Mint is funded by ads and donations.
 

Re: Executing two or more commands in sequence - Why/When/Ho

Postby xenopeek on Tue Jul 26, 2011 2:48 pm

Habitual wrote:[Logical And/Or] or is it [Logical Either/Or]? <--- Clarification needed here.
Code: Select all
$ mycommand1 ; mycommand2

There is nothing logical about the ; control operator :lol: It does sequential execution of each command. And don't forget about the & control operator, also useful here I think.

From the manpage:
If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 15433
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Executing two or more commands in sequence - Why/When/Ho

Postby Habitual on Tue Jul 26, 2011 10:04 pm

Vincent:

Yes, I was confusing the Logic issue with Truth Tables.
Something I picked up in my software-cracking days with PhrozenCrew.

I have to (re)learn new stuff to push out the old stuff, I guess.
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2274
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: Executing two or more commands in sequence - Why/When/Ho

Postby Arliegiles on Fri Sep 30, 2011 5:54 am

We can execute multiple commands by using logical operator..
Arliegiles
Level 1
Level 1
 
Posts: 2
Joined: Tue Sep 27, 2011 5:18 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest