|
| 1 | +mapfile |
| 2 | +=== |
| 3 | + |
| 4 | +从标准输入读取行并赋值到数组。 |
| 5 | + |
| 6 | +## 概要 |
| 7 | + |
| 8 | +```shell |
| 9 | +mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] |
| 10 | +``` |
| 11 | + |
| 12 | +## 主要用途 |
| 13 | + |
| 14 | +- 从标准输入或文件描述符读取行并赋值到数组。 |
| 15 | + |
| 16 | + |
| 17 | +## 选项 |
| 18 | + |
| 19 | +```shell |
| 20 | +-d delim 将delim设为行分隔符,代替默认的换行符。 |
| 21 | +-n count 从标准输入中获取最多count行,如果count为零那么获取全部。 |
| 22 | +-O origin 从数组下标为origin的位置开始赋值,默认的下标为0。 |
| 23 | +-s count 跳过对前count行的读取。 |
| 24 | +-t 读取时移除行分隔符delim(默认为换行符)。 |
| 25 | +-u fd 从文件描述符fd中读取。 |
| 26 | +-C callback 每当读取了quantum行时,调用callback语句。 |
| 27 | +-c quantum 设定读取的行数为quantum。 |
| 28 | + |
| 29 | +如果使用-C时没有同时使用-c指定quantum的值,那么quantum默认为5000。 |
| 30 | +当callback语句执行时,将数组下一个要赋值的下标以及读取的行作为额外的参数传递给callback语句。 |
| 31 | +如果使用-O时没有提供起始位置,那么mapfile会在实际赋值之前清空该数组。 |
| 32 | +``` |
| 33 | + |
| 34 | +## 参数 |
| 35 | + |
| 36 | +array(可选):用于输出的数组名称。如果没有指定数组名称,那么会默认写入到变量名为MAPFILE的数组中。 |
| 37 | + |
| 38 | +## 返回值 |
| 39 | + |
| 40 | +返回成功除非使用了非法选项、指定的数组是只读的、指定的数组不是下标数组。 |
| 41 | + |
| 42 | +## 例子 |
| 43 | + |
| 44 | +```shell |
| 45 | +# 常见的读取形式。 |
| 46 | +mapfile < source_file target_array |
| 47 | +cat source_file |mapfile target_array |
| 48 | +mapfile -u fd target_array |
| 49 | + |
| 50 | +# 只读取前5行。 |
| 51 | +mapfile < source_file -n 5 target_array |
| 52 | + |
| 53 | +# 跳过前5行。 |
| 54 | +mapfile < source_file -s 5 target_array |
| 55 | + |
| 56 | +# 在数组指定的下标开始赋值。 |
| 57 | +# 请注意:这样做不会清空该数组。 |
| 58 | +mapfile < source_file -O 2 target_array |
| 59 | + |
| 60 | +# 读取时设定行分隔符为tab。 |
| 61 | +# 注意,第二行的tab在终端需要用ctrl+v tab输入; |
| 62 | +mapfile < source_file -d $'\t' target_array |
| 63 | +mapfile < source_file -d ' ' target_array |
| 64 | + |
| 65 | +# 读取时移除行分隔符(tab)。 |
| 66 | +mapfile < source_file -d $'\t' -t target_array |
| 67 | +# 读取时移除行分隔符(换行符)。 |
| 68 | +mapfile < source_file -t target_array |
| 69 | + |
| 70 | +# 每读取2行,执行一次语句(在这里是echo)。 |
| 71 | +mapfile < source_file -C "echo CALLBACK:" -c 2 target_array |
| 72 | + |
| 73 | +# 遍历下标,依次显示数组的元素。 |
| 74 | +for i in ${!target_array[@]}; do |
| 75 | + printf "%s" ${target_array[i]} |
| 76 | +done |
| 77 | +``` |
| 78 | + |
| 79 | +### 注意 |
| 80 | + |
| 81 | +1. 该命令是bash内建命令,相关的帮助信息请查看`help`命令。 |
| 82 | +2. bash内建命令readarray是mapfile的同义词。 |
| 83 | + |
| 84 | +<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ --> |
0 commit comments