Coder Perfect

Combine numerous Linux instructions into a single command line.

Problem

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

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

Asked by d-man

Solution #1

If you want each command to be executed only if the preceding one was successful, use the && operator to combine them:

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

If one of the commands fails, none of the other commands will be run.

If you wish to run all commands regardless of whether or not the previous ones failed, use semicolons to separate them:

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

In your example, I believe you want the first case, where the following command’s execution is contingent on the prior one’s success.

You may alternatively compile all of the commands into a script and run it 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 prevent the shell from mistaking the next line for a new command; if you don’t use them, you’ll have to write the entire command on a single line.

Instructing the shell to terminate the script if any of the commands fail is a more convenient method than using backslashes and && everywhere. You can achieve this by passing the -e parameter to the set built-in function. You can now write a script in a much more natural manner:

#! /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 run the script as you would any other software on the system. 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 in the same way as the commands in the previous example; the next command will only run if the previous one was successful. Simply do not call set -e: for unconditional execution of all commands.

#! /bin/sh

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

Answered by Nikos C.

Solution #2

Using ; to separate commands only works in the forefront, according to my experience. as an example:

If there are no problems, cmd1; cmd2; cmd3 & – will only run cmd3 in the background, whereas cmd1 && cmd2 && cmd3 & – will run the full chain in the background.

Using parenthesis to cater for unconditional execution fixes this:

cmd1; cmd2; cmd3; cmd4; cmd5; cmd6; cmd7; cmd8; cm Even if any step fails, & – will run the chain of commands in the background.

Answered by Dean

Solution #3

You can use a semicolon to separate your commands:

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

Is that what you’re getting at?

Answered by andrux

Solution #4

You can use the pipe line key “|” to run them all at once:

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

Answered by Kelly Poore

Solution #5

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

Answered by Mark Stevens

Post is based on https://stackoverflow.com/questions/13077241/execute-combine-multiple-linux-commands-in-one-line