Linux run multiple commands

Execute combine multiple Linux commands in one line

If you want to execute each command only if the previous one succeeded, then combine them using the && operator:

cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install 

If one of the commands fails, then all other commands following it won’t be executed.

If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:

cd /my_folder; rm *.jar; svn co path to repo; mvn compile package install 

In your case, I think you want the first case where execution of the next command depends on the success of the previous one.

You can also put all commands in a script and execute that instead:

#! /bin/sh cd /my_folder \ && rm *.jar \ && svn co path to repo \ && mvn compile package install 

The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.

A more convenient way compared to using backslashes and && everywhere is to instruct the shell to exit the script if any of the commands fail. You do that using the set built-in function with the -e argument. With that, you can write a script in a much more natural way:

#! /bin/sh set -e cd /my_folder rm *.jar svn co path to repo mvn compile package install 

Save that to a file, for example myscript , and make it executable:

You can now execute that script like other programs on the machine. But if you don’t place it inside a directory listed in your PATH environment variable (for example /usr/local/bin , or on some Linux distributions ~/bin ), then you will need to specify the path to that script. If it’s in the current directory, you execute it with:

The commands in the script work the same way as the commands in the first example; the next command only executes if the previous one succeeded. For unconditional execution of all commands, simply don’t call set -e :

#! /bin/sh cd /my_folder rm *.jar svn co path to repo mvn compile package install 

Источник

How to run multiple commands in Linux

Different types of commands are needed to run from the terminal in Linux. Sometimes we need to run multiple commands at a time where the commands can depend on each other or not. Running multiple commands at once is called command chaining. Many types of operators can be used for command chaining for different purposes. How you can run two or more commands by using most commonly used operators is shown in this tutorial.

Читайте также:  Linux console test internet speed

Pipe (|) operator

pipe(|) operator is used to run two or more related commands at a time. The input of the next command will be the output of the previous command. So, the success of each command depends on the success of earlier command without first command. In the following command, the first command, ls will find out the list of files and folders of the current location and send the output as input for the second command, wc. It will print the total number of lines, words, and characters based on the input data.

Semicolon (;) Operator

Semicolon(;) operator is used to running two or more unrelated commands at a time. This means that the output of each command is not dependent on other commands. In the following example, three types of commands are combined together and the failure of each command will not create an effect on the output of other commands. The first command will print the content of a file, the second command will make a directory and the third command will change the current directory.

Logical AND (&&) operator

The commands which run by Logical AND (&&) are related with each other like pipe (|) command. So, if the previous command will not execute successfully then the next commands will not work. In the following example, two commands, mkdir, and rmdir combined by && operators. So, it mkdir command is failed to execute successfully then rmdir command will not execute. According to the output of ls command, myDir directory already exists in the current location. So the first command will not execute and for this second command will not execute also.

Logical OR (||) operator

Logical OR (||) operator is the opposite of the Logical AND (&&) operator. The next command will execute if the previous command fails to execute. Three cat commands are combined with OR (||) operator in the following example. When you will run the command, first of all, it will try to display the content of cat.txt file. If no such file exists in the current location then it will try to execute the next command. According to the output, bird.txt file exists in the current location and the content of this file is displayed.

Multiple commands with multiple operators

You can use multiple operators to run multiple commands at a time. In the following example, three commands are combined with OR (||) and AND (&&) operators. After running the command, first of all, it will change the current directory to newdir if the directory exists. If this command fails then it will create the directory by executing the second command and print the message, “directory is created.” According to the output, newdir directory not exist in the current location. So, the error message is displayed and the directory is created later.

Combination operator <>

Two or more commands can be combined using this operator and if the execution of the first command fails then the second command will not execute. In the following example, OR, AND and combination operators are used together. First commands will check the temp directory is exist in the current location or not. If the first command fails then it will create a temp directory and print a message. The last command will show the current directory list.

Читайте также:  Build win32 on linux

Precedence operator ()

You can use this operator for grouping the commands at the time of execution. Here, each group will work as a single task. In the following example, two command groups are defined and if the first group fails to execute then the second group will execute.

Conclusion

This tutorial explained the mostly used operators for running multiple commands in Linux. But there are many others operators exist in bash which are used to run two or more commands together. These are ampersand (&), redirection (,>>), Logical NOT (!), Combination (<>) etc.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Run Multiple Linux Commands in One Go

In this quick, beginner’s tip, learn to run multiple Linux commands one after another in a single command.

There are times when you want to execute multiple commands but don’t want to wait for one to end so that you can run the next.

Luckily, Linux gives you multiple ways to run multiple commands and in this tutorial, I will show you three ways to do so:

  • Using the semicolon (;) operator
  • Using the AND (&&) operator
  • Using the OR (||) operator

1. Run multiple commands using a semicolon

This method is useful when you want to execute chained command irrespective of whether the previous command was successfully executed or not.

Chaining multiple commands using a semicolon is the most famous method of running multiple commands and there’s a reason why.

You can add any command in the chain of commands as every command will be executed individually and does not depend on the status of the previous command execution.

Command_A ; Command_B ; Command_C

As you can see, all I did is separate two commands with space and one semicolon.

Pretty easy. Right? Now, let’s have a look at an example:

Here, I’ve used one faulty command cr which sure gave me an error but the other two commands executed independently.

2. Run multiple commands using AND (&&) operator

When you chain multiple commands with && , the second command will only be executed if the first runs with no errors.

If you want to run multiple commands in such a way that if one of the commands returns an error, the later part of the chain won’t be executed, you can use the AND operator.

To use execute multiple commands with the AND operator, you’d have to follow the following syntax:

Command_A && Command_B && Command_C

For example, here, I tried 3 commands and the middle one is faulty:

use AND operator to run multiple commands in Linux

As you can see, it gave me an error for the second command, and the third command was not executed.

3. Run multiple commands using OR (||) operator

The whole idea of the OR operator lies in its name.

Читайте также:  Eclipse sdk for linux

It will execute only one of the two commands that are chained together.

But how it will decide which one to execute and which to not? Simple.

If the first command gives an error then it will execute the 2nd command.

Think of it as an if-else statement.

To use the OR operator to run multiple commands, it needs to be executed in the following manner:

For example, here, I used the cat command to print the contents of the New.txt file and if there is no such file, the second command, touch will be used to create one:

As you can see, first, it gave me an error, and later on, it executed the touch command to create a new file.

Run commands in the background and foreground

If taking care of command execution is the main priority, running them in the background or foreground can do miracles for you.

Sounds interesting? Here’s how you do it:

I hope you will find this guide helpful.

Источник

Running multiple commands in one line in shell

So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.

Is cp the best way to do this, followed by rm ? Or is there a better way?

I want to do it all in one line so I’m thinking it would look something like:

cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple 

Is this the correct syntax?

6 Answers 6

You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple 
cp /templates/apple /templates/used && mv /templates/apple /templates/inuse 

To summarize (non-exhaustively) bash’s command operators/separators:

  • | pipes (pipelines) the standard output ( stdout ) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
  • |& pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
  • && executes the right-hand command of && only if the previous one succeeded.
  • || executes the right-hand command of || only it the previous one failed.
  • ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.

Why not cp to location 1, then mv to location 2. This takes care of «removing» the original.

And no, it’s not the correct syntax. | is used to «pipe» output from one program and turn it into input for the next program. What you want is ; , which seperates multiple commands.

cp file1 file2 ; cp file1 file3 ; rm file1 

If you require that the individual commands MUST succeed before the next can be started, then you’d use && instead:

cp file1 file2 && cp file1 file3 && rm file1 

That way, if either of the cp commands fails, the rm will not run.

Источник

Оцените статью
Adblock
detector