Python全栈工程师

3.3 IO重定向

2019-06-27  本文已影响1人  BeautifulSoulpy

来自rulongjun的博客

image

1.标准IO(Standard Input/Output)

可用于做输入的设备:

键盘设备、文件系统上的常规文件、网卡等。

可用于做输出的设备:

显示器、文件系统上的常规文件、网卡等。

程序的数据流有三种:

fd:file descriptor,文件描述符
标准输入:0
标准输出:1
标准错误:2

echo $?(bash脚本编程中,很多判断基于$?这个来决定)

image

2. IO重定向(Input/Output Redirection)

输入本来默认是键盘,我们改成其他输入,就是输入重定向 :例如从文本文件里输入。
本来输出的位置是显示器,我们改成其他输出,就是输出重定向:例如输出到文件。

set -C
禁止覆盖输出重定向到已存在的文件(在这种模式下,如果你非要覆盖,可以使用>|

set +C
关闭上述特性

/dev/null
特殊设备,空设备,也叫黑洞,数据进去都没了。


2.1 输出重定向(Output Redirection)的几种方法

1. 正常输出重定向:>(覆盖输出)、>>(追加输出)
例子:

>cat /etc/issue > /tmp/issue.out
#开两个ssh连接,我们先看第二个的终端所在的虚拟设备文件,然后在第一个输入

#第二个ssh:
>>> tty
/dev/pts/1

#第一个ssh输入:
>>> cat /etc/issue > /dev/pts/1
# 我们看到第二个ssh输出了信息

#追加输出
ls /var > /tmp/test.out   #完全清空test内容,并用前面的内容覆盖
cat /etc/fstab >> /tmp/test.out   #追加内容

|

2. 错误输出重定向:2>(覆盖输出)、2>>(追加输出)")

ls /etc/issue1111 2>> /tmp/error.out
catt /etc/issue 2>> /dev/null

3. 正确和错误的都进行输出重定向

ls /boot /err &> /tmp/all1.log # 新写法
ls /boot /err &>> /tmp/all2.log # 新写法
ls /boot /err > /tmp/all3.log 2>&1 # 老写法
ls /boot /err >> /tmp/all4.log 2>&1 # 老写法   

echo "new line" > issue  #覆盖重定向,将前面内容写到为空的issue中;
echo "new line" >> issue  #追加重定向,将内容写在文件最底部;

输入重定向(Input Redirection)的方法

3.tr命令

tr [OPTION]... SET1 [SET2]
把输入的数据当中的字符,凡是在SET1定义范围内出现的,通通对位转换为SET2出现的字符

  • tr SET1 SET2 < /path/from/somefile对位转化SET1中的字符为SET2中的字符
  • tr -d SET1 < /path/from/somefile删除指定集合里出现的字符
  • tr -s "\n" /path/from/somefile把指定的连续的字符以一个字符表示,压缩。
  • tr -cComplement ,取字符集的补集,通常与其他参数结合使用,例如-dc
[root❄centos7 ~]☭ tr 'a-z' 'A-Z'
aabbcc33
AABBCC33
^C

[root❄centos7 ~]☭ tr 'a-z' 'A-Z' </etc/issue
\S
KERNEL \R ON AN \M
[root❄centos7 ~]☭ tr 'a-z' 'A-Z' < /etc/issue > /app/issue2
[root❄centos7 ~]☭ cat /app/issue2
\S

KERNEL \R ON AN \M

tips:如果是输入后再输出到同一个文件,就会清空这个文件,所以最好不要这么用,下面是一个错误示范:

[root❄centos7 ~]☭ cd /app/
[root❄centos7 app]☭ cp issue2 issue3
[root❄centos7 app]☭ tr 'a-z' 'A-Z' < issue3 >issue3
[root❄centos7 app]☭ cat issue3
[root❄centos7 app]☭

追加是可以的,在原有文件基础上再追加一段:

[root❄centos7 app]☭ cat issue2
\S
KERNEL \R ON AN \M
[root❄centos7 app]☭ tr 'A-Z' 'a-z' < issue2 >> issue2
[root❄centos7 app]☭ cat issue2
\S
KERNEL \R ON AN \M
\s
kernel \r on an \m

dc结合使用


[root❄centos7 app]☭ echo {a..z} >f1
[root❄centos7 app]☭ cat f1
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root❄centos7 app]☭ tr -d 'f-n' <f1
a b c d e          o p q r s t u v w x y z
[root❄centos7 app]☭ cat f1
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root❄centos7 app]☭ tr -dc 'f-n' < f1
fghijklmn[root❄centos7 app]☭

Here documents

输出到屏幕,或创建多行文档):<<终止词
例子:

[root❄centos7 app]☭ cat <<EOF
> yulongjun
> zhaoweiqiang
> EOF
yulongjun
zhaoweiqiang

[root❄centos7 app]☭ cat >cat.out <<END
> yulongjun
> zhaoweiqiang
> hanjinze
> songda
> END

[root❄centos7 app]☭ cat /tmp/cat.out 
yulongjun
zhaoweiqiang
hanjinze
songda

-代表输出流

搭配cat
cat -:如果指定cat的文件为-,表示从标准输入读取(和直接使用cat,好像没什么区别)

搭配|
echo 123 | cat -:表示把管道符前面的输出流,在交给cat执行一遍(这就很牛逼了)

例子:
如果操作系统没有scp命令,只有ssh,那么是不是就不能远程拷贝了(前提:没有openssh-clients软件包)

利用-,就可以实现:

cat jdk.tar.gz | ssh 192.168.56.101  'cat - > /tmp/jdk.tar.gz'

cat jdk.tar.gz 产生输出流, 在管道后面的 - ,则可以接受输出流,并重定向到 /tmp/jdk.tar.gz

上一篇 下一篇

猜你喜欢

热点阅读