1、bash的的I/O重定向及管道
程序:指令+数据
读入数据:Input
输出数据:Output
打开的文件都有一个fd: file descriptor (文件描述符)
标准输入:keyborad, 0
标准输出:monitor, 1
标准错误输出:monitor, 2
I/O重定向:改变标准位置
输出重定向:COMMAND > NEW_POS, COMMAND >> NEW_POS
>:覆盖重定向,目标文件中的原有内容会被清除;
>>: 追加重定向,新内容会追加至目标文件尾部;
# set -C: 禁止将内容覆盖输出至已有文件中;
强制覆盖:>|
# set +C:
[[email protected] ~]# set +C
[[email protected] ~]# cat /etc/hosts > a.txt
[[email protected] ~]# set -C
[[email protected] ~]# cat /etc/hosts > a.txt -bash: a.txt: cannot overwrite existing file
2>: 覆盖重定向错误输出数据流;
2>>: 追加重定向错误输出数据流;
标准输出和错误输出各自定向至不同位置:
COMMAND > /path/to/file.out 2> /path/to/error.out
合并标准输出和错误输出为同一个数据流进行重定向:
&>:覆盖重定向
&>>:追加重定向
COMMAND > /path/to/file.out 2> &1
COMMAND >> /path/to/file.out 2 > &1
[[email protected] ~]# cat /etc/hosts >> /root/ddddd 2>>&1 //错误的 没有错误输出追加 -bash: syntax error near unexpected token `&‘
[[email protected]r ~]# cat /etc/hosts > /root/ddddd 2>&1
[[email protected] ~]# cat /etc/hostsddd >> /addd.txtbb 2>saaa [[email protected] ~]# cat saaa cat: /etc/hostsddd: No such file or directory
[[email protected] ~]# cat /etc/hostsddd &> saaahhh
[[email protected] ~]# cat saaahhh cat: /etc/hostsddd: No such file or directory
[[email protected] ~]# cat /etc/hosts &> saaahhhkkk
[[email protected] ~]# cat saaahhhkkk 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.107 master 192.168.1.108 slave
输入重定向:<
tr命令:转换或删除字符
tr [OPTION]... SET1 [SET2]
/etc/fstab 内容 用A代替a
[[email protected] ~]# tr a A < /etc/fstab # # /etc/fstAb # CreAted by AnAcondA on Tue JAn 27 14:36:31 2015 # # 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 # UUID=6f424695-992e-43fb-845d-25f6cbA558b6 / ext4 defAults 1 1 UUID=d2624A7b-cd31-4051-8f0e-2f08d03887b0 /boot ext4 defAults 1 2 UUID=3fAb6886-6f9A-484e-bc98-000f0f29e57c swAp swAp defAults 0 0 tmpfs /dev/shm tmpfs defAults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defAults 0 0 proc /proc proc defAults 0 0
小写字母编程大写字母
[[email protected] ~]# tr a-z ‘A-Z‘ < /etc/fstab # # /ETC/FSTAB # CREATED BY ANACONDA ON TUE JAN 27 14:36:31 2015 # # 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 # UUID=6F424695-992E-43FB-845D-25F6CBA558B6 / EXT4 DEFAULTS 1 1 UUID=D2624A7B-CD31-4051-8F0E-2F08D03887B0 /BOOT EXT4 DEFAULTS 1 2 UUID=3FAB6886-6F9A-484E-BC98-000F0F29E57C SWAP SWAP DEFAULTS 0 0 TMPFS /DEV/SHM TMPFS DEFAULTS 0 0 DEVPTS /DEV/PTS DEVPTS GID=5,MODE=620 0 0 SYSFS /SYS SYSFS DEFAULTS 0 0 PROC /PROC PROC DEFAULTS 0 0
HERE Documentation:<<
# cat << EOF
# cat > /path/to/somefile << EOF
2、管道:
COMMAND1 | COMMAND2 | COMMAND3 |...
Note:最后一个命令会在当前shell进程的子shell进程中执行;
tee命令:
tee [OPTION]... [FILE]...
将环境变量变成大写字母及保存到/tmp/a.tx及打印出来
[[email protected] ~]# echo $PATH | tr a-z A-Z | tee /tmp/a.txt /USR/LOCAL/SBIN:/USR/LOCAL/BIN:/SBIN:/BIN:/USR/SBIN:/USR/BIN:/USR/JAVA/JDK1.7.0_75/BIN:/USR/LOCAL/ZOOKEEPER-3.4.6/BIN:/USR/LOCAL/HADOOP-2.6.0/BIN:/ROOT/BIN [[email protected] ~]# cat /tmp/a.txt /USR/LOCAL/SBIN:/USR/LOCAL/BIN:/SBIN:/BIN:/USR/SBIN:/USR/BIN:/USR/JAVA/JDK1.7.0_75/BIN:/USR/LOCAL/ZOOKEEPER-3.4.6/BIN:/USR/LOCAL/HADOOP-2.6.0/BIN:/ROOT/BIN
练习:
1、将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中;
2、将登录至将前系统上用户信息中的后3行的信息转换为大写后保存至/tmp/who.out文件中;
who | tail -n 3 | tr ‘a-z‘ ‘A-Z‘ > /tmp/who.out