当前位置:网站首页>管道重定向
管道重定向
2022-08-04 05:31:00 【anqiujiaduizhang】
1、重定向
标准输出 | 标准正确输出 | 标准错误输出 |
进程在运行的过程中根据需要会打开多个文件,每打开一个文件会有一个数字标识。这个标识叫文件描述符。
进程使用文件描述符来管理打开的文件(FD file descriptors)
文件描述符:每打开一个程序都会有文件描述
0 | 标准输入 |
1 | 标准正确 |
2 | 标准错误 |
3+ | 进程在执行过程中打开的其他文件 |
& | 表示正确错误混合输出 |
2、输出重定向(覆盖,追加)
覆盖 | > | |
追加 | >> | |
正确输出 | 1> 1>> | > >> |
错误输出 | 2> | 2>> |
2.1输出重定向覆盖
# date > date.txt
2.2输出重定向追加
#date +%X >> date.txt
2.3错误输出重定向
#ls /root /hhhhhjjjjjj (1)>list.txt
2.4错误输出重定向到不同的位置
#ls /root /hhhhhjjjjjj >list.txt 2>nolist.txt
2.5正确和错误都输入相同位置
#ls /root /hhhhhjjjjjj &>list.txt
2.6重定向到空设备/dev/null
#ls /root /hhhhhjjjjjj 2>/dev/null
#ls /root /hhhhhjjjjjj >/dev/null
#ls /root /hhhhhjjjjjj &>/dev/null
- 注: echo 会将输入的内容送往标准输出 echo 内容 >> 文件名或脚本里面 (等于w)
2.7脚本中使用重定向
# vim a.txt
#
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
#./a.txt (权限不够)
#chown +x a.txt
#./a.txt
[[email protected] ~]# ./a.txt
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!
使用重定向
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!" >>down.txt
fi
#./a.txt
# cat down.txt
3、输出重定向
标准输入 : < 等价 0<
默认情况下,cat命令会接受标准输入设备 (键盘)的输入,并显示到控制台,但如果用文件代替键盘作为输入设备,那么该命令会以指定的文件作为输入设备,并将文件中的内容读取并显示到控制台 |
#cat /etc/passwd
#cat < /etc/passwd
虽然执行结果相同,但第一行代表的是以键盘作为输入设备,而第二行代码是以/etc/passwd 文件作为输入设备
将/etc/passwd作为cat 的输入,读出/etc/passwd 的内容
3.1通过输入重定向创建文件
(cat > file << EOF)是用来创建文件或者在脚本中使用,并向文件中输入信息 输入的任何东西会被写入文件中 ,EOF 命令结束
cat >> file5 <<EOF #可以写到脚本里或者文件里面
EOF : 开始和结束的标记 成对使用 结尾的另一个必须定格写。
3.2利用重定向建立多行的文件 脚本创建多行文件
# vim b.txt
#cat >> c.sh <<EOF
#damifan
tangculiji
ludouya
EOF
#chown +x b.txt
#./b.txt
#cat c.sh
4、管道
#rpm -qa | grep 'httpd' #查询所有安装的软件包,过滤包含httpd 的包
4.1将/etc/passwd中的用户按UID 大小排序
# sort -t":" -k3 -n /etc/passwd
# sort -t":" -k3 -n /etc/passwd -r
# sort -t":" -k3 -n /etc/passwd | head -5
# sort -t":" -k3 -n /etc/passwd | tail -5
# sort -t":" -k3 -n /etc/passwd | tail -n +5
参数详解
- sort 排序,默认升序
- -t 指定分隔符
- -k 指定列
- -n 升序
- -r 降序
- | head
- | tail
5、参数传递:xargs
对 ls cp mv 管道不能执行,所以通过xargs
#vim a.txt
/home/c.sh
/home/d.sh
/home/e.sh
#touch /home/{c..e}.sh
# cat a.txt | xargs -i ls {}
#cat a.txt | xargs -i cp -rvf{} 目的地址
#cat a.txt | xargs -i mv {} 目的地址
#cat a.txt | xargs -i rm -rvf{}
边栏推荐
猜你喜欢
MNIST Handwritten Digit Recognition - Image Analysis Method for Binary Classification
Amazon Cloud Technology Build On 2022 - AIot Season 2 IoT Special Experiment Experience
DeblurGAN-v2: Deblurring (Orders-of-Magnitude) Faster and Better 图像去模糊
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)
arm-3-中断体系结构
【论文阅读】Multi-View Spectral Clustering with Optimal Neighborhood Laplacian Matrix
迅雷关闭自动更新
【论文阅读】Mining Cross-Image Semantics for Weakly Supervised Semantic Segmentation
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
随机推荐
CAS无锁队列的实现
【论文阅读】Further Non-local and Channel Attention Networks for Vehicle Re-identification
数据库的简述与常用操作指南
深度确定性策略梯度(DDPG)
强化学习中,Q-Learning与Sarsa的差别有多大?
CSDN spree -- college round table spree
Unity ML-agents 参数设置解明
PP-LiteSeg
sbl_init.asm-适合在编辑模式下看
典型CCN网络——efficientNet(2019-Google-已开源)
Copy攻城狮信手”粘“来 AI 对对联
腾讯、网易纷纷出手,火到出圈的元宇宙到底是个啥?
Endnote编辑参考文献
Deep Learning Theory - Initialization, Parameter Adjustment
MNIST handwritten digit recognition, sorted by from two to ten
arm交叉编译
tensorRT5.15 使用中的注意点
YOLOV4流程图(方便理解)
makefile基础学习
Deep Learning Theory - Overfitting, Underfitting, Regularization, Optimizers