1 2 3 4

linux重定向 null和zero

文件描述符

linux下一切皆文件

文件描述符,是内核为了高效管理已经被打开的文件所创建的索引,用于指向被打开的文件,所有执行I/O操作的系统调用都通过文件描述符;

文件描述符是一个简单的非负整数,用以标明每一个被进程打开的文件,程序刚刚启动时候,第一个打开的是0,第二个是1,以此类推,也可以理解为是一个文件的身份ID

用户通过操作系统处理信息的过程中,使用的交互设备文件(键盘,鼠标,显示器)

  • stdin 标准输入 文件描述符0
  • stdout 标准输出 文件描述符1
  • stderr 标准错误 文件描述符2

查看一个进程打开了那些文件

1获取进程号

[root@VM_0_15_centos ~]# vim /etc/passwd

[root@VM_0_15_centos ~]# ps -aux | grep passwd
root      1476  0.1  0.4 151164  4992 pts/2    S+   22:45   0:00 vim /etc/passwd
root      1534  0.0  0.0 112708   972 pts/1    R+   22:45   0:00 grep --color=auto passwd
[root@VM_0_15_centos ~]# ll /proc/1476/fd
total 0
lrwx------ 1 root root 64 Aug 24 22:46 0 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:46 1 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:45 2 -> /dev/pts/2
lrwx------ 1 root root 64 Aug 24 22:46 4 -> /etc/.passwd.swp

一个进程启动时,都会打开3个文件,标准输入,标准输出,标准出错处理,分别对应012

对文件描述做的操作就是对文件本身的操作,可以直接通过操作文件描述符来修改文件

一个进程可以打开的文件描述符的限制,或者说一个进程可以打开多少文件

[root@VM_0_15_centos ~]# ulimit -n
100001

虽然可以修改,但是只有服务器的内存越大,机器的性能越好时 , 可以修改的值更大

[root@VM_0_15_centos ~]# ulimit -n 100002
[root@VM_0_15_centos ~]# ulimit -n
100002

重定向

输出重定向

定义:

  • 将命令的正常输出结果保存到指定的文件中,而不是直接显示在显示器的屏幕上

语法

  • > 文件名 表示将标准输出的内容,写入到后面的文件中,如果文件名已经存在,则会覆盖源文件中的内容
  • >> 文件名 表示将标准输出的内容,追加到后面的文件中,不会清除文件原有内容

查看当前主机的cpu信息,写入到cpu.txt文件中

[root@VM_0_15_centos ~]# cat /proc/cpuinfo > cpu.txt
[root@VM_0_15_centos ~]# cat cpu.txt 
processor	: 0
vendor_id	: AuthenticAMD
cpu family	: 23
model		: 1
model name	: AMD EPYC Processor
...
bogomips	: 3992.46
TLB size	: 1024 4K pages
clflush size	: 64
cache_alignment	: 64
address sizes	: 48 bits physical, 48 bits virtual
power management:

将内核的版本信息追加到cpu.txt中

...
bogomips	: 3992.46
TLB size	: 1024 4K pages
clflush size	: 64
cache_alignment	: 64
address sizes	: 48 bits physical, 48 bits virtual
power management:

Linux VM_0_15_centos 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

清空一个文件,而不是删除

[root@VM_0_15_centos ~]# > cpu.txt 
[root@VM_0_15_centos ~]# cat cpu.txt 
[root@VM_0_15_centos ~]# 

输入重定向

定义

  • 将接收输入的途径由默认的键盘改为文件

搜索文件中的某个字符

[root@VM_0_15_centos ~]# uname -a > cpu.txt 
[root@VM_0_15_centos ~]# grep centos < ./cpu.txt 
Linux VM_0_15_centos 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

将xuegod.sql导入到mysql数据库中

mysql -uroot -p 123456 < xuegod.sql

EOF

分界符,形式不是固定的,EOF可以自定义,但是前后的EOF必须成对出现,且不能和shell命令冲突

1使用eof作为分界符时,再次输入eof字符时,会自动退出

[root@VM_0_15_centos ~]# cat >> cpu.txt << eof
> aaa
> bbb
> eof
[root@VM_0_15_centos ~]# 

2如果要在文件中输入eof时,可以自定义分界符

[root@VM_0_15_centos ~]# cat > cpu.txt << 123
> aaa
> bbb
> eof
> 123
[root@VM_0_15_centos ~]# 

3在shell脚本中可以输出一组字符,比如菜单

[root@VM_0_15_centos ~]# cat eof.sh 
#!/bin/bash
cat <<eof
======================
1,mysql
2,httpd
3,oracle
======================
eof
[root@VM_0_15_centos ~]# bash ./eof.sh 
======================
1,mysql
2,httpd
3,oracle
======================

错误重定向

将命令执行过程中出现的错误信息,(选项或者参数错误,)保持到指定的文件,而不是直接显示到显示器

  • 操作符: 2>

标准输出 1>,简写 >
标准输入 1<,简写 <
标准错误 2>,不能简写

  • 在实际应用中,错误重定向可以用来收集执行的错误信息,为排错提供依据
  • 对于shell脚本还可以将无关紧要的错误信息重定向到空文件/dev/null中,以保持脚本输出的简洁

1将错误显示的内容和正确显示的内容分开

[root@VM_0_15_centos ~]# ls /etc/passwd xxxx > a.txt 2> b.txt
[root@VM_0_15_centos ~]# cat a.txt b.txt 
/etc/passwd
ls: cannot access xxxx: No such file or directory

null黑洞和zero空文件

/dev/null

被看做黑洞,所有写入它的内容都会永久丢失.而尝试从它那儿读取内容则什么也读不到,
然而/dev/null对命令行和脚本都非常有用

[root@VM_0_15_centos ~]# echo aaa >> /dev/null 
[root@VM_0_15_centos ~]# cat /dev/null 
[root@VM_0_15_centos ~]# 

/dev/zero

在类UNIX操作系统中,/dev/zero是一个特殊的文件,当你读它的时候,她会提供无限的空间符
典型用法是用它来产生一个特定大小的空白文件

使用dd命令产生一个50M的文件

[root@VM_0_15_centos ~]# dd if=/dev/zero of=c.txt bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.0676305 s, 775 MB/s

[root@VM_0_15_centos ~]# ll -h c.txt 
-rw-r--r-- 1 root root 50M Aug 24 23:16 c.txt
  • if代表输入文件.如果不指定if,默认就会从stdin中读取输入
  • of代表输入文件.如果不指定of,默认就会将stdout作为默认输出
  • bs代表字节为单位的块大小
  • count代表被复制的块数

&>和>&符号

&表示等同于的意思

1把正确和错误的消息输入到相同的位置

  • 1>&2 把标准输出等同于标准错误
  • 2>&1 把标准错误等同于标准输出
[root@VM_0_15_centos ~]# ls /tmp/ xxx > 1.txt 2>&1
[root@VM_0_15_centos ~]# cat 1.txt 
ls: cannot access xxx: No such file or directory
/tmp/:
passwd.txt
systemd-private-bf711b8563ea4002ae117d6ff54122cc-ntpd.service-lWhZaP

2把正确和错误的消息输入到相同的位置,可以简写为

[root@VM_0_15_centos ~]# ls /tmp/ xxx &> 2.txt
[root@VM_0_15_centos ~]# cat 2.txt 
ls: cannot access xxx: No such file or directory
/tmp/:
passwd.txt
systemd-private-bf711b8563ea4002ae117d6ff54122cc-ntpd.service-lWhZaP

3工作中shell脚本中的, >/dev/null 2>&1

  • 解释,将标准错误等同于标准输出,将标准输出,输出到黑洞文件中
    将产生的信息丢弃

命令判断

三个特殊符号

; 分号

  • 不考虑命令的相关性,会连续执行
[root@VM_0_15_centos ~]# ls /aaa /bbb ;echo aaa
ls: cannot access /aaa: No such file or directory
ls: cannot access /bbb: No such file or directory
aaa

&& 逻辑与

  • 它只有在前面的命令执行成功后,后面的命令才会去执行

如果/opt目录存在,则在/opt下面新建一个文件a.txt

[root@VM_0_15_centos ~]# cd /opt/ && touch a.txt
[root@VM_0_15_centos opt]# ls /opt/a.txt 
/opt/a.txt

源码编译经典实用方法

./configure && make -j 4 && make install

|| 逻辑或

  • 如果前面的命令执行成功,后面的内容不会执行
  • 如果前面的命令执行失败,会执行后面的内容
[root@VM_0_15_centos ~]# ls xxx || cd /home/
ls: cannot access xxx: No such file or directory
[root@VM_0_15_centos home]# 
[root@VM_0_15_centos home]# cd /root/ || echo aaa
[root@VM_0_15_centos ~]# 

运算顺序

  • 从左到右,一个一个执行,从上到下执行
posted @ 2019-09-23 14:46  多走多看  阅读(1755)  评论(0编辑  收藏  举报