Linux multi-command statement and redirect

Multi-command statement

We enter the Linux shell commands are generally executed one by one, but we can also write more than one line command statement, the following cite a few common methods


 

";" Semicolon usage

Way: command1; command2

Use; No. separating each command, each command in the order from left to right, the execution order, do not care whether the failure to each other, all commands will be executed.

E.g:

root @ Ksha: ~ / Desktop / LS Test # 
1.txt 2 .txt 

root @ Ksha: ~ / Desktop / Test # -l 1.txt LS; LS -l 2 .txt 
-rwxrw the root-rw- the root. 1 0. 8 January 3 14:37 .txt 
-rwxrw the root-rw- the root. 1. 8 0 2 14:37 dated 3 .txt 
 the root Ksha @: ~ / Desktop / Test # -l 3.txt LS; LS -l 2 .txt  LS : Can not access '3.txt' : no such file or directory -rwxrw-rw- 1 root root 0 8 Yue 3 14:37 2.txt

First, we can see that there are only two test folder files

The second command 

ls -l 1.txt;ls -l 2.txt

Normal execution no problem

The third command

ls -l 3.txt;ls -l 2.txt

You can still perform, even though the destination folder is not 3.txt file and error but does not affect the execution of the command of the second half

This is the role of the semicolon command execution order from left to right, whether or not a failure, all commands will be executed


 

"|" Pipe character usage

An output command, the next command as a parameter

Way: command1 | command2

We often use this command to streamline some operations the following example

For example, we would like to see the current state of the network using the command netstat will find out all of a sudden a lot of data, such as that we want to see the operation of 18565 ports it? Is it one by one to find it?

We can use the following command

root@Ksha:~/桌面/test# netstat|grep "18565"
unix  3      [ ]         STREAM     CONNECTED     18565    /var/run/dbus/system_bus_socket

grep statement can be a regular match

We look at these first command netstat show the network status and the back pipe symbol grep "18565" for the results of previous netstat command was subjected to regular matching result is then displayed

So is the use of a pipeline at the output of the command, the next command as a parameter

So if there is a two commands does not hold another naturally there will not be any results

 


 

"&" Symbol usage

& Start back on the set this parameter indicates the process as a background process

Way: command1 &

By default, the process is the foreground process, then put the Shell to occupy, we can not carry out other operations, for those processes that do not interact very often, we want to start it in the background, you can add a parameter at startup '&' for this purpose.


"&" Symbol usage

when executing a command shell, and returns a return value, the return value is stored in the shell variable $? in. ? When the $ == 0, indicating success;? When $ == 1:00 (I think the number is non-zero, the return value 0-255) indicating execution failed.

Sometimes, the next command depend on whether the previous command executed successfully. Such as: re-executed after the successful execution of a command another command or another command is executed after the failure of a command and so on.

Syntax is as follows:

command1 && command2 [&& command3 ...]

Use && connection command, and implementing logical functions. 

Only && left of the command returns true (command returns the value of $? == 0) && right of the command will be executed. 

As long as there is a command returns false (command returns the value of $? == 1), the latter command will not be executed.

root @ Ksha: ~ / Desktop / the Test # && LS LS 3.txt the -l the -l 2 .txt 
LS: can not access '3.txt': No such file or directory

This command, for example, because the file does not 3.txt file, so the back of the ls -l 2.txt command is not performed


 "||" symbol usage

 "||" symbols with rule logic "or"

Syntax is as follows:

command1 || command2 [|| command3 ...]

Use || connection command, or implementing logic functions. 

root @ Ksha: ~ / Desktop / Test # LS LS -l -l 1.txt || 2 .txt 
-rwxrw the root-rw- the root 0. 1. 8. 1 14:37 dated. 3 .txt 
root @ Ksha: ~ / Desktop / # LS 3.txt the -l || the Test the -l 2 LS .txt 
LS: can not access '3.txt' : no such file or directory  -rwxrw-rw- 1 root root 0 8 Yue 3 14:37 2.txt

We analyze the first half of the two commands may have been first performed, the command is not performed half; front half portion of the second command is not executable, the command execution attempt latter half

From the results, "||" role that commands can be executed from left to right if a command after the command is not executed


 

 

Shell input / output redirection

Most UNIX systems command accepts input and output generated from your terminal sends back to your terminal. A command normally read input from a place called standard input by default, which happens to be your terminal. Similarly, a command is usually writes its output to standard output, by default, this is your terminal.

Redirect command list is as follows:

command Explanation
command > file Redirect the output to a file.
command < file Redirect input to file.
command >> file The additional output redirected to the way file.
n > file The file descriptor n file redirected to file.
n >> file The file descriptor n to file additional way redirected to file.
n >& m M and n are the combined output file.
n <& m M and n are the combined input file.
<< tag It will mark the beginning of the content between the tag and the end tag tag as input.

0 Note that the file descriptor is typically standard input (STDIN), 1 is a standard output (STDOUT), 2 is the standard error output (STDERR).


 

Output redirection

Redirection is typically done by inserting a specific symbol between commands. In particular, these symbols following syntax:

command1> file1

The above command will execute command1 and then output the contents stored in file1.

For example, we can execute the commands netstat -pantu

root@Ksha:~/桌面/test# netstat -pantu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           606/dhclient       

So we want these results are saved in a file how do

You can perform

root @ Ksha: ~ / Desktop / test netstat -pantu #> 3.txt

After the execution, the terminal does not output, because the output has been redirected from the default standard output device (terminal) to the specified file.

We look at the results of open 3.txt

Note any file1 already existing content will be replaced with the new content. If you want to add new content at the end of the file, use >> operator.


 

Input redirection

And as output redirection, Unix commands can also get input from a file, the syntax is:

command1 < file1

In this way, would need to get input from the keyboard command will be transferred to read the contents of the file.

NOTE: output redirection is greater than (>), input redirection is less than (<).

For example, we write first in 3.txt on several file names

 We then execute the command:

root @ Ksha: ~ / Desktop / LS Test # -l <. 3 .txt 
total amount. 4 
-rwxrw the root-rw- the root 0. 1. 8. 1 14:37 dated. 3 .txt 
-rwxrw the root. 1 rw- the root-dated. 3 0. 8 2 14:37 .txt 
-rw-R & lt - r--. 1 the root 18 is the root. 8 dated 4 18:15 3.txt

We will find the command will directly read a few documents written in the name of our just 3.txt

 

We can also replace the input and output simultaneously, performing Command1, INFILE read from the file, and then write the output to the outfile.

command1 < infile > outfile

You can own to try to practice


"> &" Combination

> Can play a & merged output

Under normal circumstances, each Unix / Linux command is running will open three files:

  • Standard input file (stdin): stdin file descriptor is 0, Unix default program data read from stdin.
  • Standard output file (stdout): stdout file descriptor 1, Unix program default data output to stdout.
  • The standard error file (stderr): stderr file descriptor 2, Unix program writes error information to stderr stream.

By default, command> file redirect stdout to the file, command <file to redirect stdin to file.

2  represents the standard error file (stderr).

If you want to merge after the stdout and stderr redirected to a file, you can write:

command > file 2>&1

For example, if we execute this direct command (the current directory does not exist 4.txt)

root @ Ksha: ~ / Desktop / the Test # LS 3.txt 4.txt the -l> 5.txt
LS: can not access '4.txt': No such file or directory

First, you find the command line prompt an error and has an output in the 5.txt

So we do not want the wrong file appears in the command line but appear together with the results in 5.txt in how to do?

We can execute this command:

root@Ksha:~/桌面/test# ls -l 3.txt 4.txt>5.txt 2>&1

You will find that at this time there is no error and we'll see what's inside open 5.txt

Good we have achieved this function

Multi-use command statement and redirect the combined need more practice will be skilled in the use

 

Guess you like

Origin www.cnblogs.com/CYHISTW/p/11299200.html