n 91763d2fce34e14a90d4b79f7f9ee750 operator.
(2) Example analysis
[root@localhost ~]# w
20:41:36 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 223.166.20.53 20:24 0.00s 0.05s 0.00s w
[root@localshost ~]# w > users #w命令执行后的结果输出到users文件中
[root@localshost ~]# ll
-rw-r--r-- 1 root root 204 Jan 3 20:41 users
[root@localshost ~]# cat users #查看users文件内容,正是w命令执行后的输出结果
20:41:58 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 223.166.20.53 20:24 6.00s 0.05s 0.00s w
[root@localshost ~]# who
root pts/0 2021-01-03 20:24 (223.166.20.53)
[root@localshost ~]# who > users #将who命令执行结果输出重定向到users文件
[root@localshost ~]# cat users #发现users文件中原先w命令的输出内容被who命令覆盖了
root pts/0 2021-01-03 20:24 (223.166.20.53)
[root@localshost ~]# echo "Hello world" >> users #使用>>操作符则会追加在后面输出
[root@localshost ~]# cat users
root pts/0 2021-01-03 20:24 (223.166.20.53)
Hello world
[root@localshost ~]#
Note:
There are two #s in many command lines in the above examples:
3. Input redirection1. Command parsingUnix commands can also get input from files, the syntax is: command < file
#获取file文件中的内容作为输入内容,并用于commmand执行
Note:
The output redirection is the greater than sign (>), and the input redirection is the less than sign (dbdd0c2bfb3f0267782696660fa6bb74 file redirects stdout to file, command < file redirects stdin to file. 2. Detailed explanation of commandscommand 2>file
#stderr 重定向到 file
command 2>>file
#stderr 追加到 file 文件末尾
command > file 2>&1
command >> file 2>&1
#stdout 和 stderr 合并后重定向到 file
command < file1 >file2
#对 stdin 和 stdout 都重定向
#command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2
5. Here DocumentHere Document is a special redirection method in Shell, used to redirect input to an interactive shell script or program. 1. Grammar Its basic form is as follows: command << delimiter
document
delimiter
#作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。
Note: The delimiter at the end of
must be written in top case , there cannot be any characters in front of it, nor can there be any characters in the back, including spaces and tab indentations. The spaces before and after the starting delimiter will be ignored. 2. Example analysisEOF is the abbreviation of END Of File, which represents a custom terminator. Since it can be customized, EOF does not have a fixed value and you can set an alias at will. For example, in Linux, pressing Ctrl-D can be used as EOF instead. EOF usually works with cat to output multi-line text.
The examples are as follows:
[root@localhost ~]# wc -l << EOF
> a
> b
> c
> d
> e
> EOF
5 #输入内容为5行
[root@localhost ~]# cat << EOF
> a
> b
> c
> d
> e
> f
> EOF
a
b
c
d
e
f
EOF can also be customized as follows: [root@iZ2ze95cxr3kx9il409khtZ ~]# cat << CCC
> a
> b
> c
> d
> CCC
a
b
c
d
When executing script input, you can use the following form:#拥有大量输入的时候可以用下面的形式,将标准输入的内容重定向到(输入到)test.sh文件中。
[root@localhost ~]# cat << EOF >test.sh
> 123123123
> 3452354345
> asdfasdfs
> EOF
[root@localhost ~]# cat test.sh
123123123
3452354345
asdfasdfs
[root@localhost ~]#