mapfile命令用于从标准输入或文件描述符读取行并赋值到数组
语法格式 mapfile [参数]
常用参数: -n count从标准输入中获取最多count行,如果count为零那么获取全部
-O origin从数组下标为origin的位置开始赋值,默认的下标为0
-s count跳过对前count行的读取
参考实例
先创建一个示例用的文件alpha.log,每行一个小写字母,共26行: [root@linux265 ~]# echo {a..z} | tr " " "\n" >alpha.log [root@linux265 ~]# cat alpha.log a b c d e f g h i j k l m n o p q r s t u v w x y z
读取alpha.log文件并将每一行存储到数组myarr中(如果不指定,则存储到默认的MAPFILE数组中): [root@linux265 ~]# mapfile myarr
从标准输入中获取最多6行数据: [root@linux265 ~]# mapfile -n 6 myarr
从数组下标为6的位置开始赋值: [root@linux265 ~]# mapfile -O 6 myarr
跳过对前6行的读取: [root@linux265 ~]# mapfile -s 6 myarr