重定向
标准输入(standard input) 0 默认接受来自键盘的输入。
标准输出(standard output) 1 默认输出到终端窗口(屏幕)。
标准的错误输出(standard error output) 2 默认输出到终端窗口(屏幕)。
# 查看当前终端的进程号
[root@sc ~]# echo $$
13082
cd /etc/proc/12082/fd
[root@sc fd]# ll
总用量 0
lrwx------ 1 root root 64 7月 25 15:20 0 -> /dev/pts/2
lrwx------ 1 root root 64 7月 25 15:20 1 -> /dev/pts/2
lrwx------ 1 root root 64 7月 25 15:20 2 -> /dev/pts/2
lrwx------ 1 root root 64 7月 25 15:39 255 -> /dev/pts/2
[root@sc fd]# ls
0 1 2 255
[root@sc dev]# cd pts
[root@sc pts]# ls
0 1 2 ptmx
[root@sc pts]# ll
总用量 0
crw--w---- 1 root tty 136, 0 7月 25 11:53 0
crw--w---- 1 root tty 136, 1 7月 25 11:11 1
crw--w---- 1 root tty 136, 2 7月 25 15:43 2
c--------- 1 root root 5, 2 7月 25 11:08 ptmx
# c开头的文件是字符设备文件:与输入和输出字符相关的文件 character 字符
# /dev/pts/2 -->编号为2的一个字符设备文件
# /dev 存放设备文件的 device 键盘、鼠标、显示器、磁盘等设备,一个设备会有相关文件和它对应
任何命令只要有输出,就可以使用输出重定向。
重定向标准输出
正确的输出重定向
> 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上,会覆盖原来文件里的内容,如果文件不存在会新建。
>> 将命令的执行结果追加输出到指定文件,而不是直接显示在屏幕上,不会覆盖原来文件里的内容,如果文件不存在会新建。
1> = >
1>> = >>
错误的输出重定向
2> 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上,会覆盖原来文件里的内容,如果文件不存在会新建。
2>> 将命令的执行结果追加输出到指定文件,而不是直接显示在屏幕上,不会覆盖原来文件里的内容,如果文件不存在会新建。
# 正确的输出重定向
[root@sc chong]# ls > a.txt
[root@sc chong]# cat a.txt
a.txt
# 错误的输出重定向
[root@sc chong]# fsif 2>b.txt
[root@sc chong]# cat b.txt
-bash: fsif: 未找到命令
# 正确的和错误的输出分流,存放到不同的文件里
[root@sc chong]# ls >c.txt 2>d.txt
[root@sc chong]# cat c.txt
a.txt
b.txt
c.txt
d.txt
[root@sc chong]# cat d.txt
[root@sc chong]#
不管正确的还是错误的都重定向到同一个文件
&> 将命令的执行结果输出到指定的文件中,而不是直接显示在屏幕上,会覆盖原来文件里的内容,如果文件不存在会新建。
&>> 将命令的执行结果追加输出到指定文件,而不是直接显示在屏幕上,不会覆盖原来文件里的内容,如果文件不存在会新建。
id rootroot 1>aa.txt 2>&1 # 正确和错误的都往同一个文件里输入,会覆盖
id rootroot 1>>aa.txt 2>&1 # 正确和错误的都往同一个文件里输入,不会覆盖,只会追加
重定向标准输入
< 将命令中接收输入的途径有默认的键盘更改为指定的文件。
<< 追加输入重定向。
[root@sc chong]# wc -l c.txt
4 c.txt
[root@sc chong]# wc -l <c.txt
4
重定向的本质是去修改文件描述符1对应的那个文件。
改变了字符输出的方向,原来是往/dev/pts/2文件输出,现在往/dev/pts/3文件输出。
[root@sc pts]# echo hello > /dev/pts/3
[root@sc pts]# hello
hello
使用ssh命令远程连接到192.168.102.141这台服务器,以root用户登录。
[root@sc pts]# ssh root@192.168.102.141
The authenticity of host '192.168.102.141 (192.168.102.141)' can't be established.
ECDSA key fingerprint is SHA256:GJLkvon/6JIxY0thYpMWcSIQBVdWFjwOZah+sPUmZ6E.
ECDSA key fingerprint is MD5:cb:bf:d9:ec:3d:33:6c:a4:5c:3c:e2:ce:b8:60:b8:33.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.102.141' (ECDSA) to the list of known hosts.
root@192.168.102.141's password:
Last login: Tue Jul 25 15:51:59 2023 from 192.168.102.1
查看当前linux系统用户登录的信息
[root@sc pts]# w
15:45:43 up 2:07, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.102.1 11:08 3:52m 0.06s 0.06s -bash
root pts/1 192.168.102.1 11:11 4:33m 0.00s 0.00s -bash
root pts/2 192.168.102.1 15:20 7.00s 0.01s 0.00s w
每打开一个终端,背后就会产生一个bash进程,bash进程会输入和输出的字符设备文件。
user 登录的用户名。
TTY 表示终端的类型 terminate type
tty1~tty6 --》真正的终端,Linux系统里最多允许开6个终端
CTRL+alt+F1~F6 切换到tty1~tty6
pts/0~n --》伪终端--》模拟出来的终端 --》shell在windows里模拟出来的一个终端,给我们使用,输入命令,输出效果。
面向最终用户使用的一个端口 --》终端用户
form 从哪里登录的Linux系统
WHAT 正在终端运行的命令
wall hello --》给所有的终端发送
echo hello >/dev/pts/1 --》单独给/dev/pts/1终端发送hello
pkill
杀死进程的命令
-t pts/3 根据终端编号
-9 强制杀死这个进程
临时杀死某个登录的用户的进程
pkill -t pts/3 -9
问题
如何看到tty1~tty6终端
答案:w命令
[root@sc ~]# w
10:14:44 up 32 min, 2 users, load average: 0.01, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 10:14 12.00s 0.00s 0.00s -bash
root pts/0 192.168.102.1 09:42 4.00s 0.01s 0.00s w
如何查看Linux里的所有进程?
答案:ps -aux
如何根据pid杀死进程?
答案:kill -9
[root@sc ~]# ps aux|grep bash
root 2355 0.0 0.1 115680 2144 pts/0 Ss 09:42 0:00 -bash
root 4430 0.0 0.1 115676 2112 tty1 Ss+ 10:14 0:00 -bash
root 4491 0.0 0.0 112828 988 pts/0 S+ 10:15 0:00 grep --color=auto bash
[root@sc ~]# kill -9 2355
编写脚本杀死所有终端
# 查找出终端类型是pts/2的进程的pid号
[root@sc pts]# ps aux|grep pts/2
root 13079 0.0 0.3 161820 6104 ? Ss 15:20 0:00 sshd: root@pts/2
root 13082 0.0 0.1 115760 2220 pts/2 Ss 15:20 0:00 -bash
root 17363 0.0 0.1 155452 1872 pts/2 R+ 16:24 0:00 ps aux
root 17364 0.0 0.0 112828 988 pts/2 R+ 16:24 0:00 grep --color=auto pts/2
# 查看bash进程,可以根据终端类型来确定那个进程
[root@sc pts]# ps aux|grep bash
root 13082 0.0 0.1 115760 2220 pts/2 Ss 15:20 0:00 -bash
root 15126 0.0 0.1 115680 2068 pts/3 Ss+ 15:51 0:00 -bash
root 16696 0.0 0.1 115676 2176 tty1 Ss+ 16:15 0:00 -bash
root 17454 0.0 0.0 112828 988 pts/2 S+ 16:26 0:00 grep --color=auto bash
# 编写kill所有bash的脚本
[root@sc lianxi]# cat kill_bash.sh
#!/bin/bash
for i in $(ps aux|grep bash|awk '{print $2}')
do
kill -9 $i
done
# 执行脚本
[root@sc lianxi]# bash kill_bash.sh
本文详细介绍了Linux系统中标准输入、输出和错误输出的概念,以及如何通过输出重定向来管理这些输出。同时,讨论了进程的基本概念,如PID、文件描述符,并展示了如何使用`ps`和`kill`命令管理进程。此外,还提到了shell脚本编写,特别是用于杀死特定进程的脚本示例。
1414

被折叠的 条评论
为什么被折叠?



