Home Shell 中的输入重定向相关
Post
Cancel

Shell 中的输入重定向相关

输入重定向 ‘<’

1
$ command < /path/to/file

注意:输出重定向是大于号(>),输入重定向是小于号(<),这样,本来需要从键盘获取输入的命令会转移到文件读取内容。 注意 “<”只能从文件中读取输入。

example:
测试脚本,和输入文件

1
2
3
4
5
6
#!/bin/bash

while read line
do
    echo "input is $line"
done
1
2
3
4
5
6
qwe
adminsx
zxc

123

效果:实现输入重定向,由标准输入重定向到文件:(read 需要读取到换行)

1
2
3
4
5
6
$ ./test.sh < testfile.txt 
input is qwe
input is adminsx
input is zxc
input is 
input is 123

Here Document ‘<<’

”«” 在BASH文档中,称之为 “Here Documents”。

Here Documents 是 shell 中的一种特殊的重定向方式,用来将输入重定向到一个交互式 Shell 脚本或程序。

1
2
3
command << delimiter
    document
delimiter

它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

实际使用中,一般 delimiter 常使用EOF,也可以使用其他的自定义的。

结尾的delimiter 一定要顶格写,前面不能有任何字符,后面也不能有任何字符,包括空格和 tab 缩进。 开始的delimiter前后的空格会被忽略掉。

该功能可以将一段输入内容序列传递给一个交互式程序或命令,可以在shell或shell脚本中使用。 主要它可以支持多行的输入内容,相当于预先准备好一行一行的待输入内容(包含换行),然后程序需要输入内容时,填一行进去。

example: 在命令行中使用:

1
2
3
4
5
6
7
8
9
10
11
12
$ cat
qwe
qwe
asd
asd

$ cat << EOF
> qwe
> asd
> EOF
qwe
asd

在脚本中使用,在脚本中这样写,相当于对xsdb交互程序依次输入哪些内容,一次一行:

1
2
3
4
5
6
7
8
9
10
11
    echo "reseting the card... "
xsdb << EOF
after 1000
connect -host ${HW_SERVER} -port 3121
after 500
targets -set -filter {name =~ "*APU*"}
after 200
rst
after 400
disconnect
EOF

Here Strings ‘<<<’

”«<” 在BASH文档中,称之为 “Here Strings”。

Here String是Here Documents 的一种简化方式,它由操作符”«<”和作为标准输入的字符串构成,here-string是一个用于输入重定向的普通字符串。

1
2
$ cat <<< "1234"
1234

Here String 是支持使用命令输出的,这是它非常有用的一个地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ cat <<< "`ls -l /`"
total 72
lrwxrwxrwx   1 root root     7 Dec  1  2021 bin -> usr/bin
drwxr-xr-x   3 root root  4096 Feb  6 22:57 boot
drwxr-xr-x   2 root root  4096 Sep  9 20:08 data
drwxr-xr-x  17 root root  3020 Feb 15 20:55 dev
drwxr-xr-x 131 root root 12288 Mar  3 10:40 etc
drwxr-xr-x   4 root root  4096 Feb  6 22:23 home
lrwxrwxrwx   1 root root    31 Oct 27 14:52 initrd.img -> boot/initrd.img-5.10.0-19-amd64
lrwxrwxrwx   1 root root    31 Oct 27 14:59 initrd.img.old -> boot/initrd.img-5.10.0-19-amd64
lrwxrwxrwx   1 root root     7 Dec  1  2021 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Dec  1  2021 lib32 -> usr/lib32
lrwxrwxrwx   1 root root     9 Dec  1  2021 lib64 -> usr/lib64
lrwxrwxrwx   1 root root    10 Dec  1  2021 libx32 -> usr/libx32
drwx------   2 root root 16384 Dec  1  2021 lost+found
drwxr-xr-x   4 root root  4096 Feb  6 23:15 media
drwxr-xr-x   2 root root  4096 Dec  1  2021 mnt
drwxr-xr-x   2 root root  4096 Dec  1  2021 opt
dr-xr-xr-x 187 root root     0 Feb 15 20:55 proc
drwx------   8 root root  4096 Mar  3 10:40 root
drwxr-xr-x  29 root root   920 Mar  6 14:09 run
lrwxrwxrwx   1 root root     8 Dec  1  2021 sbin -> usr/sbin
drwxr-xr-x   2 root root  4096 Dec  1  2021 srv
dr-xr-xr-x  13 root root     0 Feb 15 20:55 sys
drwxrwxrwt  14 root root  4096 Mar  6 15:26 tmp
drwxr-xr-x  14 root root  4096 Dec  1  2021 usr
drwxr-xr-x  13 root root  4096 Feb  7 20:45 var
lrwxrwxrwx   1 root root    28 Oct 27 14:52 vmlinuz -> boot/vmlinuz-5.10.0-19-amd64
lrwxrwxrwx   1 root root    28 Oct 27 14:59 vmlinuz.old -> boot/vmlinuz-5.10.0-19-amd64

比较有用的一个示例:在shell 脚本中使用while 读取另一个命令的输出,一行一行处理。也是一个容易犯的错误。

简单的统计根目录的文件数

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
num_files=0

ls / |  while read LINE
do
    echo "this item is $LINE"
    num_files=$((num_files+1))
done

echo "there are ${num_files} files."

如果如此执行,会一直显示0个,因为中间使用了管道,导致 while 这部分的处理其实是在一个子shell中运行,完毕后,是在父shell中显式,而父shell中 的该统计变量其实没有收到影响,所以一直是0。如果对while使用管道输入内容, 并在内部使用了全局变量,会发现循环退出后全局变量时钟没有改变, 是一个常见bug。而修改方式就是不让其在子shell中执行即可,使用here string可以很好解决。

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
num_files=0

while read LINE
do
    echo "this item is $LINE"
    num_files=$((num_files+1))
done <<< `ls /`

echo "there are ${num_files} files."

关于 “< <”

这也是一种特殊的使用方式,左边 < 代表左边接受从右边输入,右边 <(command) 代表右边shell(子shell)命令的输出,将输出输出到左边。

1
#   command  <     <(command)

注意:右边的<和(之间不能有空格。

参考

https://www.jianshu.com/p/70136d731ca0

https://blog.csdn.net/liurizhou/article/details/100078917

This post is licensed under CC BY 4.0 by the author.