Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute combine multiple Linux commands in one line

I am trying to merge multiple linux commands in one line to perform deployment operation. For example

cd /my_folder rm *.jar svn co path to repo mvn compile package install 
like image 558
d-man Avatar asked Oct 25 '12 21:10

d-man


People also ask

How do I run multiple commands in one line in Linux?

On Linux, there are three ways to run multiple commands in a terminal: The Semicolon (;) operator. The Logical OR (||) operator. The Logical AND (&&) operator.

How do you run multiple commands in one line?

Running Multiple Commands as a Single Job We can start multiple commands as a single job through three steps: Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.

Can you run multiple commands in execute?

The /execute command should be able to run multiple commands.


1 Answers

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:

chmod +x myscript 

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:

./myscript 

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 
like image 66
Nikos C. Avatar answered Oct 06 '22 00:10

Nikos C.