The Linux system processes each object as a file. This includes the input and output processes. Linux uses file descriptors to identify each file object. A file descriptor is a non-negative integer that uniquely identifies the open file in the session. Each procedure can have up to 9 file descriptors at a time. For special purposes, the bash shell retains the earliest 3 file descriptors (0, 1, and 2)
Standard file descriptor for Linux
File descriptor |
Abbreviation |
Describe |
0 |
Stdin |
Standard input |
1 |
STDOUT |
Standard output |
2 |
Srderr |
Standard error |
Stdin
The stdin file describes the standard input that represents the shell. For the terminal interface, the standard input is the keyboard, the shell from the Sdtin file descriptor corresponding to the keyboard to get input, when the user input processing each character, when using the input redirection symbol (<), Linux will be redirected to the specified file to replace the standard input file descriptor. It reads the file and extracts the data as if it were typed from the keyboard
When you enter only the cat command, it accepts the stdin input, and when the user enters each line, the cat command displays each line in the output, pressing CTRL + C to exit
Code 5-1
[Email protected]:~# Cat A testthis is a testthis are a second testthis is a second test^c
Force the cat command to accept input from another non-stdin file by using the stdin redirection symbol
Code 5-2
[Email protected]:~# cat Text This is the first linethis are the second linethis is the third line[email protected]:~# cat < text This is the first linethis are the second linethis is the third line
STDOUT
The StdOut file description represents the standard shell output. On the terminal interface, the standard output is the terminal display. All output from the shell will be directed to the standard output
Code 5-3
[Email protected]:~# ls-l/home/> Demo1[email protected]:~# cat demo1 total 36drwxr-xr-x root root 4096 Nov 28 16:52 backupdrwxr-xr-x 2 root root 4096 10:29 confdrwx------ 2 root root 16384 16:53 lost+found Drwxr-xr-x 4 root root 4096 10:29 mysqldrwxr-xr-x 8 root root 4096 Oct 8 13:09 SOFTWAREDRW Xr-xr-x 2 root root 4096 10:41 tmp
Code 5-4
[Email protected]:~# ls-l/home/software/>> demo1 [email protected]:~# cat Demo1 Total 36drwxr-xr-x-root root
4096 16:52 backupdrwxr-xr-x 2 root root 4096 10:29 confdrwx------ 2 root root 16384 24 16 : Lost+founddrwxr-xr-x 4 root root 4096 10:29 mysqldrwxr-xr-x 8 root root 4096 Oct 8 13:09 so Ftwaredrwxr-xr-x 2 root root 4096 10:41 tmptotal 233600drwxr-xr-x 9 root root 4096 Nov A Pache-tomcat-1drwxr-xr-x 9 root root 4096 Nov apache-tomcat-2drwxr-xr-x 9 root root 4096 22 08: Apache-tomcat-rentaldrwxr-xr-x 3 root root 4096 Jan 8 codedrwxr-xr-x 8 UUCP 143 4096 Nov java-rw-r--r--1 root root 55062846 10:41 rental.tar.gz-rw-r--r--1 root root 30531249 O CT 8 10:14 scala-2.10.3.tgz-rw-r--r--1 root root 153586658 10:34 software.tar.gz
When the command generates an error message, the shell does not redirect the error message to the output redirection file. The shell creates an output redirect file, but the error message is displayed on the monitor
Code 5-5
STDERR
The shell handles error messages through special stderr file descriptors. Error messages generated when programs and scripts run in the shell or shell are sent to this location to write the standard error symbol in front of the redirect symbol, and the error message can be printed into the error file, but only the wrong message can be printed
Code 5-6
[Email protected]:~# ls-l/badfile/2> error[email protected]:~# cat error Ls:cannot access/badfile/: No such file o R Directory
REDIRECT Errors and data
Code 5-7
[Email protected]:~# ls-ltotal 4-rw-r--r--1 root root 4 20:41 demo1[email protected]:~# ls-al demo1 Demo2 D Emo3 Demo4 demo5 demo6 2>error 1>text[email protected]:~# lsdemo1 error text[email protected]:~# cat Error Ls:cannot access demo2:no Such file or directoryls:cannot access demo3:no such file or Directoryls:cannot access demo 4:no such file or directoryls:cannot access demo5:no such file or directoryls:cannot access demo6:no such file or dir Ectory[email protected]:~# cat text-rw-r--r--1 root root 4 20:41 demo1
The output of stderr and stdout can be redirected to the same output file
Code 5-8
[Email protected]:~# ls-ltotal 4-rw-r--r--1 root root 4 20:41 demo1[email protected]:~# ls-al demo1 Demo2 D Emo3 Demo4 demo5 demo6 &>text[email protected]:~# cat text ls:cannot access demo2:no such file or DIRECTORYLS:CA Nnot access demo3:no Such file or directoryls:cannot access demo4:no such file or Directoryls:cannot access Demo5:no Such file or directoryls:cannot access demo6:no such file or directory-rw-r--r--1 root root 4 20:41 demo1
If you deliberately generate an error message in a script, you can redirect a separate line of output to stderr, which, by default, directs stderr to stdout, but if stderr is redirected when the script is run, all text directed to stderr in the script is redirected
Code 5-9
[Email protected]:~# cat Demo2 #!/bin/bashecho "This is a error" >&2 echo "This is normal output" [email protected] : ~#./demo2 This is a errorthis is normal output[email protected]:~#./demo2 2>errorthis is normal output[email Protec ted]:~# Cat Error This is an error
Redirecting output in Scripts
Code 5-10
[Email protected]:~# cat Demo3 #!/bin/bashexec 2>errorecho "Hello World" exec 1>outecho "Hello Spring" >& 2echo "Hello MyBatis" [email protected]:~#./demo3 Hello world[email protected]:~# cat out Hello mybatis[email protected]:~ # Cat Error Hello Spring
exec 0<text, this command tells the shell to get input from the text file instead of stdin
Code 5-11
[Email protected]:~# cat text Hello springhello Hibernatehello mybatis[email protected]:~# cat Demo4 #!/bin/bashexec 0< Textcount=1while Read Linedo echo "line:# $count: $line" ((count++)) Done[email protected]:~#./demo4 Line: #1: Hello springline: #2: Hello hibernateline: #3: Hello MyBatis
Linux is not limited to 3 default file descriptors, the shell can have up to 9 open file descriptors, other file descriptors from 3 to 8, and other descriptors similar to the 3 default file descriptor usages, exec 3>>testfile will append the output to a new file
The following example: First, the script redirects the file descriptor 3 to the current location of the file descriptor 1, which is stdout, which means that the file descriptor 3 is sent to the screen
After sending some output to stdout, the script redirects stdout to the current location of file descriptor 3, which is the display, where it was originally located
Code 5-12
[Email protected]:~# cat Demo5 #!/bin/bashexec 3>&1exec 1>outecho "Hello World" echo "Hello Spring" >& 3echo "Hello Hibernate" exec 1>&3echo "Hello MyBatis" >&3echo "Hello Shiro" [email protected]:~#./demo5 Hello Springhello mybatishello shiro[email protected]:~# cat out Hello Worldhello Hibernate
In code 5-13, the file descriptor 6 is used to save the location of the stdin, and then the script redirects stdin to a file. All input to the Read command is from the redirected stdin, which is the input file. After all rows have been read, the script redirects stdin to the file descriptor 6, which restores the stdin to its original location
Code 5-13
[Email protected]:/data# cat text Springmybatishibernateshirohadoopspark[email protected]:/data# cat Demo2 #!/bin/ Bashexec 6<&0exec 0<textcount=1while Read Linedo echo "line #count: $line" ((count++)) Doneexec 0< &6read-p "is learned now?" Answercase $answer in y|y) echo "Good job";; N|n) echo "GoodBye";; Esac[email protected]:/data#/demo2 line #count: springline #count: Mybatisline #count: Hibernateline #count: Shiroline # Count:hadoopline #count: Sparkare you learned Now?ngoodbye
You can also open a single file descriptor as input and output, you can read the data from the same file, and then write the data to the same file, the following example: The EXEC command will be used as the read input and write to the file descriptor 3 assigned to the file text. Next, it reads the first line in the file with the read command by assigning a good file descriptor, and then the input that is read is displayed on the stdout. It then uses the Echo statement to write a row of data to a file that the file descriptor opens
When a script writes data to a file, it starts at the location where the file pointer is located. The read command reads the first row of data. So it causes the file pointer to point to the first character of the second row of data. When the Echo statement prints data to a file, it places the data at the current position of the file pointer, overwriting the data at that location
Code 5-14
[Email protected]:/data# cat text Springmybatishibernateshirohadoopspark[email protected]:/data# cat Demo3 #!/bin/ Bashexec 3<>textread Line<&3echo "Read: $line" echo "This was a Test line" >&3[email protected]:/data#. /demo3 read:spring[email protected]:/data# Cat text springthis is a test lineirohadoopspark
When a new input or output file descriptor is created, Linux automatically closes them when the script exits, closes the file descriptor, redirects it to the special symbol &-
EXEC 3>&-
Code 5-15
[Email protected]:/data# cat Demo4 #!/bin/bashexec 3>textecho "Hello Spring" >&3exec 3>&-echo "Hello Hibernate ">&3[email protected]:/data#/demo4./demo4:line 5:3: Bad file Descriptor[email protected]:/data# cat Text Hello Spring
As in code 5-15, once you close the file descriptor, you cannot write any data to it in the script, or the shell generates an error message
It is also important to note that after you close the file descriptor, if you open the same output file later, the shell replaces the existing file with a new file, which means that the previous data does not exist
Code 5-16
[Email protected]:/data# cat Demo5 #!/bin/bashexec 3>textecho "Hello World" >&3exec 3>&-cat textexec 3& Gt;textecho "Hello Spring" >&3echo "Hello Hibernate" >&3[email protected]:/data#./demo5 Hello World[email protected]:/data# Cat text Hello Springhello Hibernate
If you do not want the error message to appear when you run the program, you can redirect stderr to the next special file called null in/dev, and no data from the shell output to the null file will be saved
Code 5-17
[Email protected]:/data# ls-al/home/total 44drwxr-xr-x 8 root root 4096 10:29. Drwxr-xr-x Root Root
4096 Nov 11 20:08.. Drwxr-xr-x-root root 4096 16:52 backupdrwxr-xr-x 2 root root 4096 10:29 confdrwx ------ 2 Root root 16384 16:53 lost+founddrwxr-xr-x 4 root root 4096 10:29 mysqldrwxr-xr-x 8 root Root
4096 Oct 8 13:09 softwaredrwxr-xr-x 2 root root
If/dev/null is used as an input file, the existing file data will be emptied and used to clear the data without deleting the file
Code 5-18
Create a local temporary file, point can be added without adding at least 3 x at the end and only X (must be capitalized)
Code 5-19
[Email protected]:/data# mktemp test. Xxxtest.otx[email protected]:/data# mktemp Test. Xxxxxtest.gydv4[email protected]:/data# ls-ltotal 0-rw-------1 root root 0 Dec 5 05:29 test.gydv4-rw-------1 root R Oot 0 Dec 5 05:29 test.otx
Using Mktemp in Scripts
Code 5-20
[Email protected]:/data# cat Demo6 #!/bin/bashtempfile= ' mktemp test.xxx ' exec 3> $tempfileecho "Hello Java" echo "Hello Spring ">&3echo" Hello Hibernate ">&3echo" Hello MyBatis ">&3echo" Hello Shiro ">&3echo" Hello Drools ">&3echo" Hello Activity ">&3exec 3>&-echo" $tempfile content: "Cat $tempfilerm-RF $ Tempfile[email protected]:/data#./demo6 Hello Javatest.hev Content:hello Springhello Hibernatehello MyBatisHello Shirohello Droolshello Activity
The mktemp command returns the full path using the-t parameter
Code 5-21
[Email protected]:/data# ls-l/tmp/test*-rw-------1 root root 0 Dec 5 05:43/tmp/test.4wexx-rw-------1 root root 0 Sep 20:43/tmp/testing.t90-rw-------1 root root 0 Sep 20:43/tmp/testingw85-rw-------1 root root 0 Dec 5 05:43 /tmp/test. Jxq[email protected]:/data# rm/tmp/test*[email protected]:/data# mktemp-t test.xxx/tmp/test. Zuv[email protected]:/data# mktemp-t Test. Xxxxx/tmp/test. Jcyno[email protected]:/data# ls-l/tmp/test*-rw-------1 root root 0 Dec 5 05:44/tmp/test. JCYNO-RW-------1 root root 0 Dec 5 05:44/tmp/test. Zuv
Use-D to create a temporary directory, plus-T can also return the path to create a temporary directory
Code 5-22
[Email protected]:/data# mktemp-d test. Xxxtest.u31[email protected]:/data# mktemp-d Test. Xxxxxtest.uzbuc[email protected]:/data# ls-ltotal 8drwx------2 root root 4096 Dec 5 05:46 test. u31drwx------2 root root 4096 Dec 5 05:46 test. Uzbuc[email protected]:/data# MKTEMP-DT Test. Xxxxx/tmp/test. Wl6op[email protected]:/data# MKTEMP-DT Test. Xxxxxxxx/tmp/test.bikcpq7c[email protected]:/data# ls-l/tmp/test*-rw-------1 root root 0 Dec 5 05:44/tmp/ Test. JCYNO-RW-------1 root root 0 Dec 5 05:44/tmp/test. Zuv/tmp/test.bikcpq7c:total 0/tmp/test. Wl6op:total 0
Tee can be output to the monitor while redirecting files, but generally overwrite the original file content, you can use the-a append content
Code 5-23
[Email protected]:/data# date | Tee Textmon Dec 5 06:18:04 CST 2016[email protected]:/data# cat text Mon Dec 5 06:18:04 CST 2016[email protected]: /data# who | Tee text root pts/0 2016-12-05 04:26 (122.90.143.21) [email protected]:/data# cat text root pts/0 2016-12-05 04:26 (122.90.143.21) [Email protected]:/data# date | Tee-a text Mon Dec 5 06:18:29 CST 2016[email protected]:/data# cat text root pts/0 2016-12-05 04:26 (122.90.1 43.21) Mon Dec 5 06:18:29 CST 2016
Linux shell Scripting--rendering data (v)