当前位置:网站首页>Pipe redirection
Pipe redirection
2022-08-04 06:33:00 【anqiujiaduizhang】
1、重定向
标准输出 | 标准正确输出 | 标准错误输出 |
A process will open as many files as needed while it is running,Each open file will have a numerical identification.This identifier is called a file descriptor.
进程使用文件描述符来管理打开的文件(FD file descriptors)
文件描述符:Every program you open has a file description
0 | 标准输入 |
1 | 标准正确 |
2 | 标准错误 |
3+ | Other files opened by the process during execution |
& | Indicates correct and incorrect mixed output |
2、输出重定向(覆盖,追加)
覆盖 | > | |
追加 | >> | |
正确输出 | 1> 1>> | > >> |
错误输出 | 2> | 2>> |
2.1Output redirection override
# date > date.txt
2.2输出重定向追加
#date +%X >> date.txt
2.3错误输出重定向
#ls /root /hhhhhjjjjjj (1)>list.txt
2.4Error output redirected to a different location
#ls /root /hhhhhjjjjjj >list.txt 2>nolist.txt
2.5Both correct and incorrect are entered in the same location
#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 The input will be sent to standard output echo 内容 >> filename or script (等于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
虽然执行结果相同,But the first line represents the keyboard as the input device,而第二行代码是以/etc/passwd 文件作为输入设备
将/etc/passwd作为cat 的输入,读出/etc/passwd 的内容
3.1Create a file with input redirection
(cat > file << EOF)is used to create files or to use in scripts,and enter information into the file Anything entered will be written to the file ,EOF 命令结束
cat >> file5 <<EOF #It can be written in a script or in a file
EOF : start and end markers 成对使用 The other one at the end must be written in freeze frame.
3.2Use redirection to create multi-line files The script creates a multiline file
# 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 pipeline cannot be executed,所以通过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{}
边栏推荐
猜你喜欢
随机推荐
深度确定性策略梯度(DDPG)
MNIST手写数字识别 —— Lenet-5首个商用级别卷积神经网络
LeetCode_Nov_2nd_Week
[CV-Learning] Linear Classifier (SVM Basics)
C语言结构体(必须掌握版)
通用解决端口占用问题
CSDN spree -- college round table spree
Copy攻城狮5分钟在线体验 MindIR 格式模型生成
Deep learning, "grain and grass" first--On the way to obtain data sets
MNIST手写数字识别 —— 图像分析法实现二分类
makefile基础学习
arm learning-1-development board
MNIST手写数字识别 —— 从零构建感知机实现二分类
卷积神经网络入门详解
LeetCode_Dec_3rd_Week
Fabric v1.1 环境搭建
LeetCode_22_Apr_2nd_Week
arm-2-基础阶段
FAREWARE ADDRESS
MNIST手写数字识别 —— 基于Mindspore快速构建感知机实现十分类