Linux基础入门 --6 DAY
标准I/O重定向和管道
标准输入和输出
程序=指令+数据
输入数据:input
输出数据:output
打开的文件都有一个fd:file descriptor (文件描述符)
Linux给程序提供了三种I/O设备:
1. 标准输入(STDIN) 0 默认接受来自终端的输入
2. 标准输出(STDOUT)1 默认输出到终端窗口
3. 标准错误(STDERR)2 默认输出到终端窗口
范例:
[root@localhost proc]# ps aux | grep tail
root 2979 0.0 0.0 108092 612 pts/0 S+ 05:32 0:00 tail -f passwd
root 2991 0.0 0.0 112808 960 pts/1 S+ 05:32 0:00 grep --color=auto tail
[root@localhost proc]# cd 2979
[root@localhost 2979]# ls
attr cgroup comm cwd fd io map_files mountinfo net oom_adj pagemap projid_map schedstat smaps statm task wchan
autogroup clear_refs coredump_filter environ fdinfo limits maps mounts ns oom_score patch_state root sessionid stack status timers
auxv cmdline cpuset exe gid_map loginuid mem mountstats numa_maps oom_score_adj personality sched setgroups stat syscall uid_map
[root@localhost 2979]#
[root@localhost 2979]#
[root@localhost 2979]# ls
attr cgroup comm cwd fd io map_files mountinfo net oom_adj pagemap projid_map schedstat smaps statm task wchan
autogroup clear_refs coredump_filter environ fdinfo limits maps mounts ns oom_score patch_state root sessionid stack status timers
auxv cmdline cpuset exe gid_map loginuid mem mountstats numa_maps oom_score_adj personality sched setgroups stat syscall uid_map
[root@localhost 2979]#
[root@localhost 2979]#
[root@localhost 2979]# ll exe
lrwxrwxrwx. 1 root root 0 Sep 4 05:32 exe -> /usr/bin/tail
[root@localhost 2979]# cd fd
[root@localhost fd]#
[root@localhost fd]#
[root@localhost fd]# ls
0 1 2 3 4
[root@localhost fd]#
[root@localhost fd]# ll
total 0
lrwx------. 1 root root 64 Sep 4 05:32 0 -> /dev/pts/0
lrwx------. 1 root root 64 Sep 4 05:32 1 -> /dev/pts/0
lrwx------. 1 root root 64 Sep 4 05:32 2 -> /dev/pts/0
lr-x------. 1 root root 64 Sep 4 05:32 3 -> /root/passwd
lr-x------. 1 root root 64 Sep 4 05:32 4 -> anon_inode:inotify[root@localhost proc]# ps aux | grep bash
root 1036 0.0 0.0 115408 944 ? S 05:12 0:00 /bin/bash /usr/sbin/ksmtuned
root 2498 0.0 0.1 116564 3168 pts/0 Ss 05:14 0:00 -bash
wtj 2728 0.0 0.1 116460 3080 pts/1 Ss 05:25 0:00 -bash
root 2885 0.0 0.1 116560 3148 pts/0 S 05:31 0:00 -bash
root 2940 0.0 0.1 116560 3160 pts/1 S 05:31 0:00 -bash
root 3034 0.0 0.0 112812 960 pts/1 S+ 05:36 0:00 grep --color=auto bash
[root@localhost proc]# echo $$ //查看当前shell进程编号
2940[root@localhost 2885]# ll fd
total 0
lrwx------. 1 root root 64 Sep 4 05:39 0 -> /dev/pts/0
lrwx------. 1 root root 64 Sep 4 05:39 1 -> /dev/pts/0
lrwx------. 1 root root 64 Sep 4 05:32 2 -> /dev/pts/0
lrwx------. 1 root root 64 Sep 4 05:39 255 -> /dev/pts/0
[root@localhost 2885]# ll /dev/std*
lrwxrwxrwx. 1 root root 15 Sep 4 05:12 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 Sep 4 05:12 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 Sep 4 05:12 /dev/stdout -> /proc/self/fd/1[root@localhost ~]# alias rm='DIR=/data/backup/`date +%F_%T `;mkdir $DIR;mv -t $DIR'
I/O重定向redirect
标准输出和错误重定向
STDOUT和STDERR可以被重定向到指定文件,而非默认当前终端
格式:
命令 操作符号 文件名
支持操作符号包括:
1> 或 > 把STDOUT重定向到文件
>| 强制覆盖
2> 把STDERR重定向到文件
&> 把所有输出重定向到文件
以上如果已存在文件,文件内容会被覆盖.
set -C 禁止将内容覆盖已有的文件,但可以追加
set +C 允许覆盖,默认
示例:
[root@localhost ~]# ls
-f passwd
[root@localhost ~]# ls >/dev/pts/1
[root@localhost ~]# tty
/dev/pts/1
[root@localhost ~]# -f passwd[root@localhost ~]# { ls;pwd;} > test.log // 合并2个命令结果进行重定向
[root@localhost ~]# (ls;pwd) > test.log
[root@localhost ~]# cat test.log
backuo
backup
-f
f1.log
test.log
/root
[root@localhost ~]# ls /etc/err > err.log 2>&1
[root@localhost ~]# cat err.log
ls: cannot access /etc/err: No such file or directory
>> 追加
在Linux中,
>>
是一个重定向操作符,用于将命令的输出追加到指定文件的末尾,而不是覆盖文件内容。
标准输入重定向
<
[root@localhost ~]# cat bc.sh
1+1
2*2
[root@localhost ~]# bc < bc.sh
2
4[root@localhost ~]# seq -s+ 1 100 | bc
5050
tr命令
tr转换和删除字符
tr [OPTION]... SET1[SET2]
SET1
是要被转换或删除的字符集合。SET2
是转换后的字符集合,长度应与SET1
相同(如果执行替换操作)。如果省略SET2
,则tr
会删除SET1
中的字符。
选项:
-c
或--complement
:取反字符集,即操作不在SET1
中的字符。-d
或--delete
:删除SET1
中的字符,而不是转换。-s
或--squeeze-repeats
:压缩连续重复的字符,只保留一个。-t
或--truncate-set1
:将SET1
的字符长度截断为与SET2
相同。如果SET2
较短,则SET1
的额外字符会被忽略。
示例:
[wtj@localhost ~]$ tr "123" "abc"
123
abc[wtj@localhost ~]$ tr -dc abc
abc
dqdqdqwgeqabc
abcabc[wtj@localhost ~]$[root@localhost ~]# echo {1..100} | tr " " + | bc
5050
多行重定向 <<
基本用法:
command <<DELIMITER line1 line2 ... DELIMITER
带有变量替换的Here-Document
默认情况下,here-document中的变量不会被替换为它们的值。但是,你可以通过在分隔符前加上
-
来启用变量替换:
cat <<'EOF' $HOME is not expanded. EOF cat <<"EOF" $HOME is expanded to $(echo ~). EOF
在第一个例子中,
$HOME
没有被替换,因为我们在EOF
前加了一个单引号,这禁用了变量替换。在第二个例子中,$HOME
被替换成了当前用户的主目录的路径。注意事项
- 分隔符可以是任何字符串,但通常选择像
EOF
、END
这样的简单字符串,以避免与输入内容冲突。- 在某些情况下,你可能需要转义或引用分隔符,以避免与输入内容冲突。
- Here-document是shell脚本中非常强大的特性,它允许你在脚本中直接嵌入复杂的文本数据。
管道
在Linux中,管道符(
|
)是一个非常强大的工具,它允许你将一个命令的输出作为另一个命令的输入。这种机制极大地增强了命令行工具的灵活性和功能。使用管道符,你可以将多个命令串联起来,形成一个处理数据的流水线。基本用法
管道符的基本语法如下:
command1 | command2
这里,
command1
的输出会被直接传递给command2
作为其输入。示例
1. 查看当前目录下所有文件,并通过
grep
搜索包含特定文本的文件ls -l | grep ".txt"
这个命令会列出当前目录下的所有文件和目录,然后通过管道符将输出传递给
grep
命令,grep
命令会搜索包含“.txt”的行,即文件名以“.txt”结尾的文件。2. 使用
cat
查看文件内容,并通过grep
搜索特定文本cat file.txt | grep "特定文本"
这个命令会先使用
cat
命令查看file.txt
的内容,然后将这些内容传递给grep
命令,grep
命令会搜索并显示包含“特定文本”的行。3. 统计当前目录下文件和目录的数量
ls -l | wc -l
这个命令首先使用
ls -l
列出当前目录下的所有文件和目录的详细信息,然后通过管道符传递给wc -l
命令,wc -l
命令会计算并显示接收到的行数,即文件和目录的数量(注意,这包括了一行总结信息,因此实际数量需要减一)。更准确的做法是使用find
命令结合wc
:这个命令使用
find
命令列出当前目录下(不包括子目录)的所有文件和目录,然后通过wc -l
计算数量。find . -maxdepth 1 | wc -l
注意事项
- 管道符只能处理文本数据。如果命令输出的是非文本数据(如二进制文件),则管道符可能无法正常工作。
- 管道符右侧的命令会启动一个子shell来执行,这意味着在管道符右侧定义的变量或别名可能不会在左侧的命令中生效。
- 管道符是Linux和Unix系统中非常基础且强大的特性之一,通过结合使用不同的命令和工具,可以实现复杂的文本处理和数据操作任务。
tee命令
利用tee命令可以重定向到多个目标
格式:
命令1 | tee [ -a ] 文件名 | 命令2
可以将命令1的标准输出,作为命令2的标准输入
选项:
-a 追加
示例:
[root@localhost ~]# echo 1+1 | tee bc | bc
2
[root@localhost ~]# cat bc
1+1[root@localhost ~]# cat << EOF | tee bc
> love
> wtj
> root
> EOF
love
wtj
root
[root@localhost ~]# cat bc
love
wtj
root
tr命令
[root@localhost ~]# echo i love you | rev
uoy evol i