11.1 I/O以及I/O重定向
11.1.1 默认输入输出设备及程序运行流程简介
管道和重定向:> < >> <<
计算机5大部件:
运算器、控制器: CPU
存储器:RAM
输入设备/输出设备
程序:指令和数据(放于存储器中)
控制器:读取指令,并读取需要的运算
运算器:在控制器的控制线进行运算
存储器:运算结果存储在存储器中
三大总线:
地址总线:内存寻址
数据总线:传输数据
控制总线:控制指令
寄存器:CPU暂时存储器
I/O设备(输入输出设备): 硬盘,键盘,鼠标
程序运行时会使用默认的输入输出设备:INPUT设备,OUTPUT设备
系统设定的默认输入输出设备
默认输出设备:标准输出,STDOUT, 1
默认输入设备:标准输入, STDIN, 0
标准错误输出:STDERR, 2
对于计算机来说:
标准输入:键盘
标准输出和错误输出:显示器(因为廉价,可以输出无限的东西)
passwd --stdin(获取标准输入为密码)
11.1.2 I/O重定向
I/O重定向:将默认的输入输出来源改变为自己设定的内容
[root@Daniel-R480 ~]# ls /var
adm crash empty gopher lib lock mail opt run tmp
cache db games kerberos local log nis preserve spool yp
[root@Daniel-R480 ~]# ls /var >/var/var.out
[root@Daniel-R480 ~]# vim /var/var.out
[root@Daniel-R480 ~]# cat /var/var.out
adm
cache
crash
db
empty
1、输出重定向
Linux:
(1)标准输出重定向
>: 覆盖输出,覆盖原有的文件
>>:追加输出
set -C: 禁止对已经存在文件使用覆盖重定向;
强制覆盖输出,则使用 >|
set +C: 关闭上述功能
(2)错误输出重定向
2>: 重定向错误输出
2>>: 追加方式
[root@Daniel-R480 ~]# ls /varr > /tmp/var.out 2> /tmp/err.out
[root@Daniel-R480 ~]# cat /tmp/err.out
ls: cannot access /varr: No such file or directory
[root@Daniel-R480 ~]#
(3)标准和错误输出同时重定向
&>: 重定向标准输出或错误输出至同一个文件
[root@Daniel-R480 ~]# ls /varr &> /tmp/var.out
[root@Daniel-R480 ~]# cat /tmp/var.out
ls: cannot access /varr: No such file or directory
[root@Daniel-R480 ~]#
2、输入重定向
<:输入重定向
<<:Here Document,在此处生成文档
[root@Daniel-R480 ~]# cat << END
> 123
> 234
> END
123
234
[root@Daniel-R480 ~]#
[root@Daniel-R480 ~]# cat << EOF
> 123
> EOF
123
[root@Daniel-R480 ~]# cat >> './test.txt' << EOF
> 123
> 234
> EOF
[root@Daniel-R480 ~]# cat ./test.txt
123
234
[root@Daniel-R480 ~]#
3、管道
管道:前一个命令的输出,作为后一个命令的输入
命令1 | 命令2 | 命令3 | …
tee :既显示输入的内容,又保存到文件中
[root@Daniel-R480 ~]# echo "hello world"| tee /tmp/hello.out
hello world
[root@Daniel-R480 ~]# cat /tmp/hello.out
hello world
[root@Daniel-R480 ~]#
[root@Daniel-R480 ~]# wc -l /etc/passwd
32 /etc/passwd
[root@Daniel-R480 ~]# wc -l /etc/passwd | cut -d' ' -f1
32
[root@Daniel-R480 ~]#
练习:
1、统计/usr/bin/目录下的文件个数;
# ls /usr/bin | wc -l
[root@Daniel-R480 ~]# ls -l /usr/bin | wc -l
790
[root@Daniel-R480 ~]# ls -l /usr/bin | head -1
total 64504
[root@Daniel-R480 ~]# ls -l /usr/bin | head -2
total 64504
-rwxr-xr-x 1 root root 41544 Oct 31 2018 [
[root@Daniel-R480 ~]# ls -l /usr/bin | head -3
total 64504
-rwxr-xr-x 1 root root 41544 Oct 31 2018 [
-rwxr-xr-x 1 root root 107848 Feb 3 00:33 a2p
[root@Daniel-R480 ~]# ls -l /usr/bin | head -4
total 64504
-rwxr-xr-x 1 root root 41544 Oct 31 2018 [
-rwxr-xr-x 1 root root 107848 Feb 3 00:33 a2p
-rwxr-xr-x 1 root root 29200 Oct 30 2018 addr2line
[root@Daniel-R480 ~]# ls -lh /usr/bin | head -1
total 64M
[root@Daniel-R480 ~]#
2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;
[root@Daniel-R480 ~]# cut -d: -f7 /etc/passwd | sort -u
/bin/bash
/bin/sh
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
[root@Daniel-R480 ~]#
3、思考:如何显示/var/log目录下每个文件的内容类型?
[root@Daniel-R480 ~]# file /var/log/*
/var/log/anaconda: directory
/var/log/audit: directory
/var/log/boot.log: empty
/var/log/btmp: empty
/var/log/chrony: directory
/var/log/cron: empty
/var/log/grubby_prune_debug: ASCII text
/var/log/lastlog: 8086 relocatable (Microsoft)
/var/log/maillog: empty
/var/log/messages: empty
/var/log/qemu-ga: directory
/var/log/rhsm: directory
/var/log/secure: empty
/var/log/spooler: empty
/var/log/tallylog: empty
/var/log/tuned: directory
/var/log/wtmp: empty
/var/log/yum.log: ASCII text
[root@Daniel-R480 ~]# file `ls /var/log`
anaconda: cannot open (No such file or directory)
audit: cannot open (No such file or directory)
boot.log: cannot open (No such file or directory)
btmp: cannot open (No such file or directory)
chrony: cannot open (No such file or directory)
cron: cannot open (No such file or directory)
grubby_prune_debug: cannot open (No such file or directory)
lastlog: cannot open (No such file or directory)
maillog: cannot open (No such file or directory)
messages: cannot open (No such file or directory)
qemu-ga: cannot open (No such file or directory)
rhsm: cannot open (No such file or directory)
secure: cannot open (No such file or directory)
spooler: cannot open (No such file or directory)
tallylog: cannot open (No such file or directory)
tuned: cannot open (No such file or directory)
wtmp: cannot open (No such file or directory)
yum.log: cannot open (No such file or directory)
[root@Daniel-R480 ~]# cd /var/log
[root@Daniel-R480 log]# file `ls /var/log`
anaconda: directory
audit: directory
boot.log: empty
btmp: empty
chrony: directory
cron: empty
grubby_prune_debug: ASCII text
lastlog: 8086 relocatable (Microsoft)
maillog: empty
messages: empty
qemu-ga: directory
rhsm: directory
secure: empty
spooler: empty
tallylog: empty
tuned: directory
wtmp: empty
yum.log: ASCII text
[root@Daniel-R480 log]#
4、取出/etc/inittab文件的第6行;
[root@Daniel-R480 ~]# head -6 /etc/inittab | tail -1
#
5、取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中;
[root@Daniel-R480 ~]# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/users
user9:/bin/bash
6、显示/etc目录下所有以pa开头的文件,并统计其个数;
[root@Daniel-R480 ~]# ls -d /etc/pa* | wc -l
3
[root@Daniel-R480 ~]#
7、不使用文本编辑器,将alias cls=clear一行内容添加至当前用户的.bashrc文件中;
[root@Daniel-R480 ~]# echo "alias cls=clear" >> ~/.bashrc
[root@Daniel-R480 ~]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias cls=clear
[root@Daniel-R480 ~]#
Comments Closed.