Multiple commands on linux command line

Multiple commands on a single line in Linux [duplicate]

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

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

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

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 .

Thanks for pointing out the difference between && and ; . Seems in most cases ; is preferable above && .

You can encapsulate commands (or sequences of commands) in parentheses, like so:

# killall vsftpd && (/usr/sbin/vsftpd &) && echo "OK" 

However, this doesn’t make much sense semantically as the && token means “run subsequent command if previous command successful” (i.e., return value of 0), and the single & puts the command preceding it into the background (continuing immediately with the next command), which always yields success.

In your case, there really isn’t any way to determine the success of running the vsftpd command when it is backgrounded, unless the executable offers command-line arguments running the thing as a daemon so you needn’t background it manually.

Источник

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.
Читайте также:  Залогинившийся пользователь в linux

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.

Источник

How to run multiple Unix commands in one time?

I’m still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file, it will run all the commands inside that file? or is there any way(or better) which i do not know? Thanks for giving all the comments and suggestions, I will appreciate it.

8 Answers 8

Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:

Then paste your commands inside of it, one to a line.

Save your file, usually with the .sh extension (but not required) and you can run it like:

Or you could change the permissions to make it executable:

Lots of resources available on this site and the web for more info, if needed.

hi, may i know why «history» is not working? I searched in the web and i saw we can execute command «history» in the shell script if use «. script_name», what if for my machine the permission is denied?

Just separate your commands with &&

Nice answer for the title of the question, but missed the three references to «file» in the body of the question. 🙂

No, that’s the AND operator. You need a single & to run in the background. Also, this isn’t the answer to the real question.

Keith, he meant the AND operator. It will execute them consecutively and that’s what we want here. Else we would have thread safety issues.

@sarnold OP suggested using a file might be a way, but the && operator is also a great way to run multiple commands.

We can run multiple commands in shell by using ; as separator between multiple commands

If we use && as separator then next command will be running if last command is successful.

you can also use a semicolon ‘;’ and run multiple commands, like : $ls ; who

Читайте также:  Arch linux pamac install

Yep, just put all your commands in one file and then

This will run the commands in sequence. If you want them all to run in parallel (i.e. don’t wait for commands to finish) then add an & to the end of each line in the file

If you want to use multiple commands at command line, you can use pipes to perform the operations.

It will give number of times «Hello» exist in that file.

Sure. It’s called a «shell script». In bash, put all the commands in a file with the suffix «sh». Then run this:

Note that ending the file with a suffix of «sh» has absolutely no effect whatsoever. The chmod only makes a difference if you include a shebang and you execute the file — the ./myfile instead of source myfile or . myfile .

To have the commands actually run at the same time you can use the job ability of zsh

$ zsh -c «[command1] [command1 arguments] & ; [command2] [command2 arguments]»

Or if you are running zsh as your current shell:

$ ping google.com & ; ping 127.0.0.1

The ; is a token that lets you put another command on the same line that is run directly after the first command.

The & is a token placed after a command to run it in the background.

Источник

How can I run multiple commands which have & in one command line?

I’ve encountered a headache problem. I want to execute mulitple commands in background, so I want to start them in bash one by one. It’s easy to start one command in linux shell in background, just like this:

But if I want to run multiple commands in background, I tried the following command format, but failed:

Panther, it seems using () could help fix this issue much easier than using a script. Let’s try to make full use of the features provided by the shell itself firstly, instead of composing what we want by ourselves. But of course, using script also could fix this issue. Thanks anyway.

What if I want to run a ‘tail -F > ‘ followed by ‘jobs -l’, which is again followed by ‘disown -h %1’

2 Answers 2

If you want to run them sequentially:

If you want them to run parallel:

In bash you can also use this (space behind the < and the ; are mandatory):

() could help bash to distinguish the & and &&. So just use () to seperate the execution unit when we want to use the & and && togather.

This starts myCommand1 and sends it to the background as it’s followed by ampersand, then immediately starts myCommand2 and sends also this to the background, therefore releasing the shell again.

Lists

For better understanding you may substitute pipeline by command here.

A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or .

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.

AND and OR lists are sequences of one or more pipelines separated by the && and || control operators, respectively.
Source: man bash

Let’s break that down into examples. You can build a list by combining commands and separating them with one of these: ; & && || :

command1 ; command2 # runs sequentially command1 && command2 # runs sequentially, runs command2 only if command1 succeeds command1 || command2 # runs sequentially, runs command2 only if command1 fails command1 & command2 # runs simultaneously 

You can terminate lists with one of these: ; & .
Normally you execute a command or a list by pressing Enter , that equals . The semicolon ; serves the very same purpose especially in scripts. Ampersand & however starts the command(s) in a subshell in the background, immediately releasing the shell.

Читайте также:  Failed connection refused linux

You can use round () or curly brackets <> to further group lists, the difference being that round brackets spawn a subshell and curly ones don’t. Curly brackets need a space after the first and a semicolon or a newline before the closing bracket. For example:

# if c1 succeeds start a shell in the background # and run c2 and c3 sequentially inside it c1 && ( c2 ; c3 ) & # run c1 and if it succeeds c2 sequentially as a group command # if c1 or c2 fail run c3 in the background < c1 && c2 ;>|| c3 & 

This can get quite complicated, if you’re unsure use true and false to test whether the construction works as expected:

Job Control

The jobs command displays a list of the background jobs that are running or have recently been finished in the current shell. There are a number of keyboard shortcuts and commands for job control:

  • Ctrl + Z types the suspend character that causes the process currently running in the foreground to be stopped, it is not terminated, but remains in the jobs list
  • Ctrl + Y types the delayed suspend character that causes the process currently running in the foreground to be stopped when it attempts to read input from the terminal
  • fg = % brings a process into the foreground starting it if necessary, you can specify the process as follows:
 % # last process in the jobs list %1 # 1st process in the jobs list %abc # process beginning with the string “abc” %?abc # process containing the string “abc” anywhere 
 %& # last process in the jobs list %1& # 1st process in the jobs list %abc& # process beginning with the string “abc” %?abc& # process containing the string “abc” anywhere 
 wait %1 # 1st process in the jobs list 

Imagine you started a lengthy process ( jobs reveals it’s number 3) and then realize you want the computer to be suspended when it finishes, plus echo a message if the process didn’t succeed:

 wait %3 || echo failed ; systemctl suspend 

Источник

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