Execute Combine Multiple Linux Commands in One Line

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:

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

Multiple commands on a single line in Linux

First, if you want to run multiple commands in one line, separate them by a ;:

cmd1 ; cmd2 ; cmd3

The && is the logical and operator. If you issue

cmd1 && cmd2

cmd2 will only run if cmd1 succeeded. That's important to mention (also see below).


If you use the & to run a command in background simply append the next command without the ; delimiter:

cmd1 & cmd2

The & is not a logical operator in this case, it tells bash to run cmd1 in background.

In your case, the commandline needs syntactically look like this:

killall vsftpd && /usr/sbin/vsftpd & echo "OK"

However, I guess you really meant this:

killall vsftpd ; /usr/sbin/vsftpd & echo "OK"

because otherwise you would not be able to start the process if it is not already running, since killall would return a non zero return code.

Even having this the code is quite fragile. I suggest to use your operating systems facilities to start vsftp as a daemon. I mean facilities like the command start-stop-daemon.

Running multiple commands in one line in shell

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

Or

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.

combine multiple linux commands in one line

you can also do-it like that :

nohup sh -c 'php 1.php; php 2.php; php 3.php' &

edit :
to answer your question, the process are parallel. you can verify this by writing the ps command.
eg : with the sleep command :

nohup sh -c 'sleep 30 && sleep 30' &

output :

 ....
6920 7340 7340 6868 pty2 17763 16:33:27 /usr/bin/sleep
6404 4792 4792 7004 pty2 17763 16:33:26 /usr/bin/sleep
....

edit 2 :
Ok then try with parallel command. (you probably have to install it)

Create a file cmd.txt :

1.php
2.php
3.php

Then execute this command (haven't tried yet, but it should work). you can change the --max-procs numbers if you have more/less than 4 core :

cat cmd.txt | parallel --max-procs=4 --group 'php {}'

hope it works...

Trying to append ( ) multiple commands to a file in one line

If you only want to run it once, consider a subshell

( echo "Hello World" ; pwd ) >> practice.txt

If you expect to run the collection several times (perhaps declaring something useful in your shell's .*rc file), you could put both calls into a function

fn() {
echo "Hello World"
pwd
}

This can also be done on a single line

% fn() { echo "Hello World" ; pwd }
% fn >> practice.txt
% cat practice.txt
Hello World
/home/sean

I want to execute multiple linux command lines in one .txt file(as parallel). I am using GNU parallel

Let us assume you have files like:

/data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam

And you want to run:

delly call -t DEL -n -q 15 -0 6_LEP-11_00186s.md.ir.br.bam.DEL.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta /data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam /data/jaesoon/DB/DB3/3_blood_4s_merged.bam
delly call -t DUP -n -q 15 -0 6_LEP-11_00186s.md.ir.br.bam.DUP.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta /data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam /data/jaesoon/DB/DB3/3_blood_4s_merged.bam
delly call -t INV -n -q 15 -0 6_LEP-11_00186s.md.ir.br.bam.INV.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta /data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam /data/jaesoon/DB/DB3/3_blood_4s_merged.bam
delly call -t TRA -n -q 15 -0 6_LEP-11_00186s.md.ir.br.bam.TRA.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta /data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam /data/jaesoon/DB/DB3/3_blood_4s_merged.bam
delly call -t INS -n -q 15 -0 6_LEP-11_00186s.md.ir.br.bam.INS.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta /data/jaesoon/DB/DB6/6_LEP-11_001E6s.md.ir.br.bam /data/jaesoon/DB/DB3/3_blood_4s_merged.bam

Then you can do that by:

parallel delly call -t {1} -n -q 15 -0 {2/}.{1}.bcf -g /data/jaesoon/DB/DB6/human_g1k.v37.fasta {2} /data/jaesoon/DB/DB3/3_blood_4s_merged.bam ::: DEL DUP INV TRA INS ::: /data/jaesoon/DB/DB6/*.bam

May I suggest you read GNU Parallel 2018 (available at
http://www.lulu.com/shop/ole-tange/gnu-parallel-2018/paperback/product-23558902.html
or download it at: https://doi.org/10.5281/zenodo.1146014) Read
at least chapter 1+2. It should take you less than 20 minutes and your command line
will love you for it.



Related Topics



Leave a reply



Submit