一、重定向
标准输入、标准正确输出、标准错误输出
进程在运行的过程中根据需要会打开多个文件,每打开一个文件会有一个数字标识。这个标识叫文件描述符。
进程使用文件描述符来管理打开的文件(FD----file descriptors).
文件描述符:每打开一个程序都会有文件描述
0,标准输入(键盘)
1,标准输出
2,标准错误,
3+,进程在执行过程中打开的其他文件。
&:表示正确错误混合输出
二、输出重定向 (覆盖,追加)
> ----覆盖
>> ----追加
正确输出: 1> 1>> 等价于 > >>
错误输出: 2> 2>>
2.1. 案例1:输出重定向(覆盖)
[root@qfedu.com ~]# ls /root 1> 1.txt #正确输出--覆盖
# 注意:如果 > 前面什么都不加默认为1,标准正确输出。(代表 ls 查看root的结果 重定向 到 1.txt文件中显示)
2.2.案例2:输出重定向(追加)
[root@qfedu.com ~]# ls /root /etc >> 1.txt #正确输出--追加 (代表 ls 查看 root 和etc下的内容 追加到 1.txt文件中显示)
2.3. 案例3:错误输出重定向
[root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt
ls: cannot access /aaaaaaaaa: No such file or directory (代表错误输入)
[root@qfedu.com ~]# ls /home/ /aaaaaaaaa >1.txt 2>2.txt #重定向到不同的位置
(代表着把正确输出的内容结果输出到1.txt 错误输出的结果显示到2.txt)
可以用 cat 1.txt 或者 cat 2.txt 查看结果
2.4.正确和错误都输入到相同位置
[root@qfedu.com ~]# ls /home/ /aaaaaaaaa &>1.txt #混合输出到相同文件
(代表着正确输出的结果和 错误输出的结果 都显示到1.txt)
2.5.重定向到空设备/dev/null
[root@qfedu.com ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null #空设备,将错误的输出丢掉
[root@qfedu.com ~]# ls /home/ /aaaaaaaaa &>/dev/null #空设备,将正确与错误的输出丢掉
(代表着dev/null 是一个空设备 或者叫做 黑洞, 输出的内容到黑洞不会在显示无论是正确或者错误输出都不显示)
2.6.脚本中使用重定向
[root@qfedu.com ~]# vim ping1.sh (shall脚本都是以.sh结尾)
#!/bin/bash (叫法 蛇帮 代表使用bash翻译命令)
ping -c1 10.18.40.100 (ping -c1代表 ping一次 10.18.40.100)
if [ $? -eq 0 ];then (代表如果 $? -eq =0的话)
echo "10.18.40.100 is up." (如果上条命令成功则显示10.18.40.100 is up到屏幕上)
else (代表着 否则的意思)
echo "10.18.40.100 is down!" (如果命令 没成功则显示10.18.40.100 is down!到屏幕上)
fi (代表着 结束 完成的意思)
[root@qfedu.com ~]# chmod +x ping1.sh (授予权限)
[root@qfedu.com ~]# ./ping1.sh #执行文件(执行脚本)
实战二(使用重定向)
[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 (如果成功则追加到up.txt这个文件)
else
echo "10.18.40.100 is down!" >>down.txt (如果失败追加到 down.txt这个文件)
fi
[root@qfedu.com ~]# ./ping1.sh (执行shall脚本)
三、输入重定向 <
标准输入: < 等价 0<
默认情况下,cat 命令会接受标准输入设备(键盘)的输入,并显示到控制台,但如果用文件代替键盘作为输入设备,那么该命令会以指定的文件作为输入设备,并将文件中的内容读取并显示到控制台。
通过输入重定向创建文件
(cat > 1.txt<< EOF )是用来创建文件或者在脚本中使用,并向文件中输入信息输入的任何东西会被写入文件中,EOF命令结束。
语法:cat >> file5 <<EOF #可以写到脚本或者文件里面
EOF:开始和结束的标记。(代表 开始和结束 结束必须使用EOF结尾 ,这个代表的EOF字体可以改变)
成对使用
结尾的另一个必须定格写。
[root@qfedu.com ~]# cat >1.txt<<EOF 输入(如果这么书写)
> 111
> 222
> 333
> 444
> EOF
[root@qfedu.com ~]# cat 1.txt 结果(则这么显示)
111
222
333
444
四、管道 |
[root@qfedu.com ~]# rpm -qa |grep 'httpd' #查询所有安装的软件包,过滤包含httpd的包
实战案例二 sort 排序的意思 默认以空格为分隔符
将/etc/passwd中的用户按UID大小排序
[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 默认输出后十行
实战案例三 stat状态的意思 后面跟文件名 可以查看文件的详细信息
stat 详细查看命令信息 比ll更详细
awk 剪切的意思
NR==3代表第几行 print$4 代表打印第几列
[root@qfedu.com ~]# netstat -lntp | awk 'NR==3 {print $4}' | awk -F':' '{print $2}'
22
五、参数传递:xargs
对:ls cp rm 管道不能执行。所以通过xargs。
语法:
cat a.txt | xargs -i cp {} /目录
{}:前面传过来的内容
-i :为了让大括号生效
目录时 -r
解释:前面传过来的东西交给大括号
cat file.txt |xargs ls -l
前面是目录或者目录的路径。 ls - l 后面可以不加大括号,直接执行。
[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’
常用小命令
[root@qfedu.com ~]# du -h /etc/ #查看目录及目录中的文件大小
[root@qfedu.com ~]# du -sh /etc/ #查看目录的总大小
[root@qfedu.com ~]# ls /etc/ | wc -l #查看目录中有多少个文件