Linux基础命令—重定向与管道符

本文详细介绍了Linuxshell中的重定向、输出覆盖、追加、文件描述符以及各种输出重定向方式的应用。此外,还涉及了输入重定向、shell脚本中的应用、变量使用、for循环并发执行和管道操作的基本概念和实例。
摘要由CSDN通过智能技术生成

基础知识

什么是重定向
将要输出在屏幕上的信息重新定向写入到其他的文件中

为何要使用重定向
1)当屏幕输出重要的信息是,将内容保存至一个重要的文件中
2)后台运行的程序输出的结果不去干扰我们正常的使用
3)计划任务,将系统中定时任务的结果保存至文件中以便于查看
4)一些例行执行的命令,我们已之该命令会错误,不想看错误信息,将其忽略
5)错误日志与正确日志分开保存

通过一个程序的运行会打开三个文件 标准输入 标准输出 错误输出
名称                文件描述符   作用
标准输入(stdin)        0        键盘输入 
标准输出(stdout)       1        显示器输出
标准输出(stderr)       2        显示器输出
文件名称(filename)     3+        

输出重定向

标准覆盖输出重定向
1>
错误覆盖输出重定向
2>
标准追加输出重定向
1>>
错误追加输出重定向 
2>>
混合覆盖输出重定向
&>
混合追加输出重定向
&>>
各输出各的
1> a 2> b

准备一个环境
touch a.txt
echo 123 > a.txt
echo 456 > test.txt
标准覆盖输出重定向
[root@localhost ~]# cat a.txt b.txt > stdout.txt
cat: b.txt: No such file or directory

[root@localhost ~]# cat stdout.txt 
123

覆盖
cat test.txt b.txt > stdout.txt 

[root@localhost ~]# cat stdout.txt 
456

标准追加输出重定向
[root@localhost ~]# cat a.txt b.txt >> stdout.txt 
cat: b.txt: No such file or directory

[root@localhost ~]# cat stdout.txt 
123
123

错误覆盖输出重定向
[root@localhost ~]# cat a.txt b.txt 2> stderr.txt
123
[root@localhost ~]# cat stderr.txt 
cat: b.txt: No such file or directory

覆盖
[root@localhost ~]# cat test.txt aaa.txt 2> stderr.txt 
456
[root@localhost ~]# cat stderr.txt 
cat: aaa.txt: No such file or directory

错误追加输出重定向
[root@localhost ~]# cat a.txt b.txt 2>> stderr.txt 
123
[root@localhost ~]# cat stderr.txt 
cat: b.txt: No such file or directory
cat: b.txt: No such file or directory
[root@localhost ~]# cat test.txt aaa.txt 2>> stderr.txt 
456
[root@localhost ~]# 
[root@localhost ~]# cat stderr.txt 
cat: b.txt: No such file or directory
cat: b.txt: No such file or directory
cat: aaa.txt: No such file or directory

问题如何清空文件内容呢?
>stderr.txt

混合覆盖输出重定向
[root@localhost ~]# cat a.txt b.txt &>all.txt
[root@localhost ~]# cat all.txt 
123
cat: b.txt: No such file or directory

混合追加输出重定向 
[root@localhost ~]# cat a.txt b.txt &>>all.txt
[root@localhost ~]# cat all.txt 
123
cat: b.txt: No such file or directory
123
cat: b.txt: No such file or directory

各输出各的
[root@localhost ~]# cat a.txt b.txt 1> stdout.txt 2> stderr.txt
[root@localhost ~]# cat stdout.txt 
123
[root@localhost ~]# cat stderr.txt 
cat: b.txt: No such file or directory

输入重定向

1.通过输入重定向输入多个内容
cat > test.txt <<EOF
a
b
c
d
e
f
g
EOF

2.邮件中的案例
yum install -y mailx
mail root 
邮件保存的目录
/var/mail/用户名称
/var/spool/mail/用户名称
mail root < test.txt

重定向在shell脚本中的应用

判断10.0.0.0/24网段的机器是否存活
shell 写法
1.文件全部以.sh结尾 
vim test.sh
#!/bin/bash #指定命令解释器

ping -c2 -i0.01 10.0.0.200 &>/dev/null 

判断:
vim test.sh
#!/bin/bash
if ping -c2 -i0.01 10.0.0.200 &>/dev/null 
then
    echo "10.0.0.200"主机存活
else
    echo "10.0.0.200"主机未存活
fi

shell变量的定义
无需指定变量的数据类型 
变量名=值
name=nrj 

如何调用变量
$name 

for循环中的并发执行 
循环体内的内容用{}&括起来 未来面试中可能会问到该问题

循环:
#!/bin/bash
for i in {1..254}; do
{
    if ping -c2 -i0.01 10.0.0.$i &>/dev/null 
    then
        echo 10.0.0.$i active
    else
        echo 10.0.0.$i dead 
    fi
}&
done 

管道符

1.什么是管道
管道操作符号 "|" ,主要用来连接左右两个命令, 将左侧的命令的标准输出, 交给右侧命令的标准输入
PS: 无法传递标准错误输出至后者命令
2.管道流程示意图
格式: cmd1 | cmd2 [...|cmdn]
3.管道使用案例
案例1: 将/etc/passwd 中的用户按 UID 大小排序
# sort -t':' -k3 -n /etc/passwd 
# sort -t':' -k3 -nr /etc/passwd
# sort -t':' -k3 -n /etc/passwd |head
案例2: 统计当前/etc/passwd 中用户使用的 shell 类型
# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
案例3: 统计出最占CPU的5个进程
# ps aux --sort=-%cpu |head -6

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值