标准输入/出

  • 每个打开的文件都有一个文件描述符fd(file descriptor)

  • Linux给程序提供三种I/O设备: 1. 标准输入STDIN : 0,默认接收来自键盘的输入 2. 标准输出STDOUT : 1,默认输出到终端窗口 3. 标准错误STDERR : 2,默认输出到终端窗口

输出重定向

  • 使用说明 STDOUT 和 STDERR 可以被重定向到文件: 命令 操作符号 文件名 支持的操作符号包括: > : 把STDOUT重定向到文件,覆盖 >> : 把STDOUT重定向到文件,在原有基础上追加内容 2> : 把STDERR重定向到文件,覆盖 2>> : 把STDERR重定向到文件,追加 &> : 把所有输出重定向到文件,覆盖 &>> : 把所有输出重定向到文件,追加覆盖 cmd > /path/to/file.out 2>&1 : 把所有输出重定向到文件,覆盖(顺序很重要) set -C : 禁止将内容覆盖已有文件,但可追加强制覆盖 >| set +C : 允许覆盖 (cmd1;cmd2……) : 合并多个程序到STDOUT

  • 示例

#-----------------------set -C 禁止覆盖,但可强制覆盖 >|----------------[root@centos7 testdir]# cat aa/root
[root@centos7 testdir]# echo "come on baby">aa-bash: aa: cannot overwrite existing file
[root@centos7 testdir]# echo "come on baby">|aa[root@centos7 testdir]# cat aacome on baby#-----------------------把STDERR重定向到文件,覆盖----------------------[root@centos7 testdir]# find /etc -name passwd 2> /dev/null/etc/passwd
/etc/pam.d/passwd

[root@centos7 testdir]# tree /testdir/ 2>&1 > /testdir/tree.log-bash: /testdir/tree.log: cannot overwrite existing file
[root@centos7 testdir]# tree /testdir/ > /testdir/tree.log 2>&1-bash: /testdir/tree.log: cannot overwrite existing file#------------------------合并多个程序到STDOUT---------------------------[root@centos7 testdir]# (echo "[`date`]";tree -L 1 /testdir) >| aa[root@centos7 testdir]# cat aa[Sun Jul 31 22:41:25 CST 2016]
/testdir
├── aa
├── dd -> aa
├── test
└── testlink -> test2 directories, 2 files#------------------合并文件---------------------------------------------[root@centos7 testdir]# cat aa tree.log > combined

输入重定向

  • 使用说明 < : 重定向标准输入

  • 示例

[root@centos7 testdir]# echo `date` >| aa[root@centos7 testdir]# cat aaSun Jul 31 23:02:24 CST 2016[root@centos7 testdir]# cat > filea < aa[root@centos7 testdir]# cat fileaSun Jul 31 23:02:24 CST 2016#------------------------读取文件发邮件-----------------------------[root@centos7 testdir]# mail -s subject liang < aa[liang@centos7 ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help."/var/spool/mail/liang": 1 message 1 new>N  1 root                  Sun Jul 31 23:10  18/617   "subject"& 1Message  1:
From root@centos7.localdomain  Sun Jul 31 23:10:02 2016Return-Path: <root@centos7.localdomain>
X-Original-To: liang
Delivered-To: liang@centos7.localdomain
Date: Sun, 31 Jul 2016 23:10:02 +0800To: liang@centos7.localdomain
Subject: subject
User-Agent: Heirloom mailx 12.5 7/5/10Content-Type: text/plain; charset=us-ascii
From: root@centos7.localdomain (root)
Status: R

tr 转换/删除字符(不会修改源文件,仅输出到终端)

  • 用法 tr [OPTION...] SET1 [SET2]

  • 参数说明

-c或--complerment取字符集的补集
-d或--delete删除所有属于第一字符集的字符;
-s或--squeeze-repeats把连续重复的字符以单独一个字符表示
-t或--truncate-set1先删除第一字符集较第二字符集多出的字符
  • 示例

[root@centos7 testdir]# cp /etc/issue /testdir/issue[root@centos7 testdir]# tr 'a-z' 'A-Z' < /testdir/issue
\S
KERNEL \R ON AN \M[root@centos7 testdir]# cat /etc/issue
\S
Kernel \r on an \m[root@centos7 testdir]# tr -d abc < /testdir/fstab
# Aessile filesystems, y referene, re mintined under '/dev/disk'
# See mn pges fst(5), findfs(8), mount(8) nd/or lkid(8) for more info[root@centos7 testdir]# cat fstab 
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info