四、管道与重定向

四、管道与重定向

1 重定向

0,标准输入(键盘)
1,标准输出
2,标准错误,
3+,进程在执行过程中打开的其他文件。  
&:表示正确错误混合输出

1.1 输出重定向 (覆盖,追加)

>   ----覆盖
>>  ----追加
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>
;
&&
||

1.1.1. 案例1:输出重定向(覆盖)

[root@qfedu.com ~]# date 1> date.txt  #正确输出--覆盖# 注意:如果 > 前面什么都不加默认为1,标准正确输出。

image-20240429174734550

1.1.2. 案例2:输出重定向(追加)

[root@qfedu.com ~]# date >> date.txt #正确输出--追加

image-20240429174810401

1.1.3.案例3:错误输出重定向

[root@localhost ~]# ls /home/  /aaaaaaaaa >list.txt
ls: cannot access /aaaaaaaaa: No such file or directory
[root@localhost ~]# ls /home/  /aaaaaaaaa >list.txt 2>error.txt #重定向到不同的位置
[root@localhost ~]# cat error.txt 
ls: cannot access /aaaaaaaaa: No such file or directory

image-20240429174922332

1.1.4. 正确和错误都输入到相同位置

[root@qfedu.com ~]# ls /home/  /aaaaaaaaa &>list.txt  #混合输出到相同文件

image-20240429175043801

1.1.5. 重定向到空设备/dev/null

[root@qfedu.com ~]# ls /home/  /aaaaaaaaa >list.txt 2>/dev/null #空设备,将错误的输出丢掉
[root@qfedu.com ~]# ls /home/  /aaaaaaaaa &>/dev/null #空设备,将正确与错误的输出丢掉

echo会将输入的内容送往标准输出(打印)
echo 内容 >> 文件名或脚本里面

ls / /hello 1>a.txt &>b.txt 2>c.txt

将正确的输出当成错误的输出使用,将错误的输出当成正确的输出使用

nginx -V 2>&1|  grep "version"

1>
2>
&>
1>&2
2>&1

脚本中使用重定向

实战一(没有使用重定向)

[root@qfedu.com ~]# vim ping1.sh
#!/bin/bash
ping -c1 10.18.40.100
if [ $? -eq 0 ];then
  echo "10.18.40.100 is up."
else
  echo "10.18.40.100 is down!" 
fi
[root@qfedu.com ~]# chmod +x ping1.sh 
[root@qfedu.com ~]# ./ping1.sh #执行文件(执行脚本)
PING 10.18.40.100 (10.18.40.100) 56(84) bytes of data.

--- 10.18.40.100 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

10.18.40.100 is down!

实战二(使用重定向)

[root@qfedu.com ~]# vim ping1.sh
#!/bin/bash
ping -c1 10.18.40.100 &>/dev/null
if [ $? -eq 0 ];then
  echo "10.18.40.100 is up." >>up.txt
else
  echo "10.18.40.100 is down!"
fi
[root@qfedu.com ~]# ./ping1.sh

1.2 输入重定向 <

标准输入: <   等价 0<

通过输入重定向创建文件

(cat > file <<EOF )是用来创建文件或者在脚本中使用,并向文件中输入信息输入的任何东西会被写入文件中,EOF命令结束。

语法:cat > file5 <<EOF  #可以写到脚本或者文件里面
EOF:开始和结束的标记。
成对使用
结尾的另一个必须定格写。
[root@qfedu.com ~]# cat >file4 <<EOF
> 111
> 222
> 333
> 444
> EOF
[root@qfedu.com ~]# cat file4 
111
222
333
444

利用重定向建立多行的文件 脚本创建多行文件==>打印菜单

[root@qfedu.com ~]# vim create_file.sh
#!/bin/bash
cat >file200.txt <<EOF
111
222
333
yyy
ccc
EOF
[root@qfedu.com ~]# chmod +x create_file.sh 
[root@qfedu.com ~]# ./create_file.sh 
[root@qfedu.com ~]# cat file200.txt 
111
222
333
yyy
ccc

2 管道

用法:command1 | command2 |command3 |...

image-20240429175635622

实战案例一

[root@qfedu.com ~]# rpm -qa  |grep 'httpd'  #查询所有安装的软件包,过滤包含httpd的包



[root@qfedu.com ~]# ps aux | grep 'sshd'

tee

实战案例二

[root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd  #以: 分隔,将第三列按字数升序
[root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd -r #以: 分隔,将第三列按字数降序
[root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd |head #以: 分隔,将第三列按字数升序看前十行
[root@qfedu.com ~]# sort -t":" -k3 -n /etc/passwd |tail #以: 分隔,将第三列按字数升序看后十行
参数详解:
sort 排序,默认升序
-t 指定分隔符
-k 指定列
-n 按数值
-r 降序
head 默认输出前十行
tail 默认输出后十行

实战案例三

[root@qfedu.com ~]# netstat -lntp | awk 'NR==3 {print $4}' | awk -F':' '{print $2}'
25

image-20240429175754617

3 xargs

对:ls cp rm 管道不能执行。所以通过xargs。

语法:
cat a.txt | xargs -i cp {} /目录

{}:前面传过来的内容
 -i :为了让大括号生效
 目录时  -r
 解释:前面传过来的东西交给大括号
 
 cat file.txt |xargs ls -l          
 前面是目录或者目录的路径。  ls - l  后面可以不加大括号,直接执行。

image-20240429175937821

实战案例一

[root@qfedu.com ~]# touch /home/file{1..5}
[root@qfedu.com ~]# vim files.txt
/home/file1
/home/file2
/home/file3 
/home/file4
/home/file5
[root@qfedu.com ~]# cat files.txt |ls -l #不加xargs传参,看输出结果
[root@qfedu.com ~]# cat files.txt |rm -rvf  #不加xargs传参,看输出结果
[root@qfedu.com ~]# cat files.txt |xargs ls -l
-rw-r--rwx. 1 root root 12 Nov  7 21:57 /home/file1
-rw-r--r--. 1 root root  0 Nov  7 21:57 /home/file2
-rw-r--r--. 1 root root  0 Nov  7 21:57 /home/file3
-rw-r--r--. 1 root root  0 Nov  7 21:57 /home/file4
-rw-r--r--. 1 root root  0 Nov  7 21:57 /home/file5
[root@qfedu.com ~]# cat files.txt |xargs rm -rvf
removed ‘/home/file1’
removed ‘/home/file2’
removed ‘/home/file3’
removed ‘/home/file4’
removed ‘/home/file5’

实战案例二

[root@qfedu.com ~]# touch /home/file{1..5}
[root@qfedu.com ~]# # cat files.txt | xargs -i cp -rvf {} /tmp/
‘/home/file1’ -> ‘/tmp/file1’
‘/home/file2’ -> ‘/tmp/file2’
‘/home/file3’ -> ‘/tmp/file3’
‘/home/file4’ -> ‘/tmp/file4’
‘/home/file5’ -> ‘/tmp/file5’

4 常用命令

[root@qfedu.com ~]# du -h /etc/   #查看目录及目录中的文件大小
[root@qfedu.com ~]# du -sh /etc/  #查看目录的总大小
du -h --max-depth=1 /usr/
[root@qfedu.com ~]# ls /etc/ | wc -l #查看目录中有多少个文件
							   wc -w

image-20240429180056294

image-20240429180108643

扩展–阅读

什么是CC攻击?

攻击者借助代理服务器生成指向受害主机的合法请求,实现DDOS和伪装就叫:CC(ChallengeCollapsar)。
CC主要是用来攻击页面的。大家都有这样的经历,就是在访问论坛时,如果这个论坛比较大,访问的人比较多,打开页面的速度会比较慢,
访问的人越多,论坛的页面越多,数据库压力就越大,被访问的频率也越高,占用的系统资源也就相当可观。

如何防御CC攻击

1.开启防火墙,过滤掉访问次数多的IP地址
2.拒绝代理服务器访问你服务器
怎么拒绝代理服务器访问呢?
代理服务器有固定的IP地址,将这些IP地址都加到防火墙下,全部drop掉

CC攻击危害是什么?

大量的流量不断冲击你的服务器,会让你的服务器负载及压力越来越大,直到服务器崩溃宕机

6.什么是DOS攻击

DoS是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务。
最常见的DoS攻击有计算机网络带宽攻击和连通性攻击。

相关推荐

  1. 【Linux】定向管道(十)

    2024-04-30 15:30:01       12 阅读
  2. Linux下管道定向深入了解

    2024-04-30 15:30:01       33 阅读
  3. Linux学习笔记:定向缓冲区

    2024-04-30 15:30:01       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-30 15:30:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-30 15:30:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-30 15:30:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-30 15:30:01       20 阅读

热门阅读

  1. 生成能够精确匹配原字符串的正则表达式

    2024-04-30 15:30:01       14 阅读
  2. zynq基础知识学习(1)

    2024-04-30 15:30:01       11 阅读
  3. 【软测学习笔记】Linux入门Day01

    2024-04-30 15:30:01       10 阅读
  4. 点云和去噪

    2024-04-30 15:30:01       19 阅读
  5. K8S集群安装

    2024-04-30 15:30:01       10 阅读
  6. Android APP转成launcher

    2024-04-30 15:30:01       13 阅读
  7. Linux第六章

    2024-04-30 15:30:01       16 阅读