Run several commands linux

Chaining Commands in Linux

Chaining commands in Linux allows us to execute multiple commands at the same time and directly through the terminal. It’s like short shell scripts that can be executed through the terminal directly. Linux command chaining is a technique of merging several commands such that each of them can execute in sequence depending on the operator that separates them and these operators decide how the commands will get executed. It allows us to run multiple commands in succession or simultaneously.

Some commonly used chaining operators are as follows:

Working with chaining operators

1. Ampersand(&) Operator: It is used to run a command in the background so that other commands can be executed. It sends a process/script/command to the background so that other commands can be executed in the foreground. It increases the effective utilization of system resources and speeds up the script execution. This is also called as Child process creation or forking in other programming languages. Ampersand sign can be used as follows:

ping -cl google.com & #change the command before & ping -c1 google.com & ping -c1 geeksforgeeks.org &

2. AND (&&) Operator: The command succeeding this operator will only execute if the command preceding it gets successfully executed . It is helpful when we want to execute a command if the first command has executed successfully.

echo "hello there" && echo "this is gfg" apt update && echo "hello"

AND chaining operator

3. Semi-colon(;) Operator: It is used to run multiple commands sequentially in a single go. But it is important to note that the commands chained by (;) operator always executes sequentially. If two commands are separated by the operator, then the second command will always execute independently of the exit status of the first command. Unlike the ampersand operator, the execution of the second command is independent of the exit status of the first command. Even if the first command does not get successfully executed i.e, the exit status is non-zero, the second command will always execute.

semi colon chaining operator

The three commands will get executed sequentially. But the execution of the command preceding ( ; ) operator will execute even if the execution of the first command is not successful.

4. Logical OR (||) Operator: The command succeeding this operator will only execute if the execution of the command preceding it fails. It is much like an else statement. If the execution status of the first command is non-zero then the second command will get executed.

echo "hello there" || echo "This is gfg" apt update || echo "hello"

or chaining operator

5. Piping (|) Operator: This operator sends the output of the first command to the input of the second command.

In the above command wc -l displays the number of lines. ls -l displays the lists the files in the system. The first command displays the number of files in the directory. ls – l lists the names of the files and this output is sent to the next command which counts the number of lines in the input. As a result, by using pipe we get the number of files in the directory.

piping operator

6. NOT (!) operator: It is used to negate an expression in command/. We can use it to delete all files except one in a directory.

touch a.txt b.txt c.txt d.txt e.txt rm -r !(a.txt)

This command will remove all files in the directory except a.txt. In the image below, we create some files using the touch command inside a directory. ls shows the files in the directory. To delete all files except ‘a.txt’ we use! Operator. If we list the files again, we can see that all files except a.txt are removed.

Читайте также:  Linux сеть общая папка

NOT operator

7. Redirection Operators(‘’,’>>’): This operator is used to redirect the output of a command or a group of commands to a stream or file. This operator can be used to redirect either standard input or standard output or both. Almost all commands accept input with redirection operators.

The first command creates a file with the name ‘file_name’ (The redirection operator >> allows us to give input in the file) while the second command will sort the contents of the file. Refer to the image below, we first create a file with numbers and then use this command. This sorts the content of the files.

8. AND, OR Operators as an if-else condition: This command can be used as an if-else statement. It is a combination of logical AND and logical OR operators.

[ ! -d ABC ] && mkdir ABC || cd ABC

This command will first check if the directory ‘ABC’ exists or not. If it does not exist then a new directory is created else ‘ABC’ becomes the current directory. Refer to the image below, the directory named ‘ABC’ does not exist and hence it is created. When the same command is executed the second time, then the directory already exists and hence ‘ABC’ becomes the current directory.

9. Concatenation Operator(\): Used to concatenate large commands over several lines in a shell. It also improves the readability for the users. A large command is split over several lines and hence, it is used to execute large commands.

It will open a file named text(1).

10. Precedence: This command is used to set precedent value so that multiple commands can execute in a given order.

cmd1 && cmd 2 || cmd3 ( cmd1 && cmd 2 ) || cmd3

In the first case, if the first command is successful then the second will get executed but the third command will not execute. But in the second case, the third command will get executed as the precedence is set using the () operator. Refer to the image below: If the directory exists (the first command), then the current directory becomes PQR (second command) but in the first case the third command is not getting executed while in the second case when the precedence operator is used then the third command is also executed.

precendence chaining operator

11. Combination Operator (<>): The execution of the command succeeding this operator depends on the execution of the first command. The set of commands combined using <> operator executes when the command preceding it has successfully executed.

[ -f hello.txt ] && echo «file exists» ; echo «hello» [ -f hello.txt ] &&

In the first case, hello will always get printed. If the file exists then the command will get executed as it is preceding the && operator. If we want to execute both second and third commands only if the file exists, then we use <> operators to combine the commands.

Источник

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:

Читайте также:  Linux ldap client gui

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.

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:

Читайте также:  Yandex disk linux kde

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 

Источник

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