?
目錄?
- 0 前言
- 1? readarray命令的格式和功能
-
- 1.1 命令格式
- 1.2?命令功能
- 1.3?注意事項
- 2?命令應(yīng)用實例
-
- 2.1 從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù)時不指定數(shù)組名,則數(shù)據(jù)會保存到MAPFILE數(shù)組中
- 2.2 從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù)并存儲到指定的數(shù)組
- 2.3?使用 -O?選項指定起始下標(biāo)
- 2.4?用-n指定有效行數(shù)
- 2.5?用-s來路過部分?jǐn)?shù)據(jù)
- 2.6?用-c和-C選項使用回調(diào)程序
- 2.7?使用輸出重定向和-t選項從磁盤文件中讀取數(shù)據(jù)
- 3 mapfile命令
?
0 前言
在交互式編程中,數(shù)組元素的值有時是需要從程序外部輸入的。比如由用戶通過鍵盤輸入的,這時我們可以使用read -a命令來實現(xiàn),但需要重復(fù)輸入的數(shù)據(jù)比較多時,用read -a命令就不太方便,效率也不夠高。而且對于有些經(jīng)常使用的固定數(shù)據(jù),我們可以把這些數(shù)據(jù)存放在一個文件里,然后在使用這些數(shù)據(jù)的時候,再從文件里把數(shù)據(jù)讀出來。
為此,Linux專門提供了 readarray命令。
?1? readarray命令的格式和功能
我們 可以使用命令 help readarray 來查看?readarray 命令的幫助信息。
purleEndurer @ bash ~ $ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
? ? Read lines from a file into an array variable.
? ??
? ? A synonym for `mapfile'.purleEndurer @ bash ~ $ readarray --help
bash: readarray: --: invalid option
readarray: usage: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
purleEndurer @ bash ~ $?
?可惜help readarray命令 顯示的幫助信息不多。我們 又嘗試??readarray --help 命令,但是readarray 命令不支持?--help 選項。
1.1 命令格式
readarray [-n 最大行數(shù)] [-O 起始下標(biāo)] [-s 跳過行數(shù)] [-t] [u 文件描述符] [-C 回調(diào)程序] [-c 行數(shù)] [數(shù)組名]
選項 | 說明 | 備注 |
---|---|---|
-c 行數(shù) | 每讀取指定行數(shù)就調(diào)用一次"-C 回調(diào)程序"選項指定的回調(diào)程序 默認(rèn)為每5000行調(diào)用一次回調(diào)程序 |
count |
-C 回調(diào)程序 | 每讀取"-c 行數(shù)"選項指定的行數(shù)就執(zhí)行一次回調(diào)程序 | callback |
-n 最大行數(shù) | 最多只拷貝指定的最大行數(shù)的數(shù)據(jù)到數(shù)組中 默認(rèn)為0,即拷貝所有行。 |
number |
-O 起始下標(biāo) | 指定從哪個下標(biāo)開始存儲數(shù)據(jù),默認(rèn)為0。 對于二維數(shù)組來說,指定的是起始行數(shù)。 |
origin |
-s 跳過行數(shù) | 忽略指定的跳過行數(shù)中的數(shù)據(jù),從跳過行數(shù)之后開始 | skip |
-t | 移除尾隨行分隔符,默認(rèn)是換行符 主要配合 -u選項使用 |
trim |
-u 文件描述符 | 指定從文件描述符而非標(biāo)準(zhǔn)輸入中讀取數(shù)據(jù) | use |
1.2?命令功能
從標(biāo)準(zhǔn)輸入或指定文件讀取數(shù)據(jù)并存儲到指定的數(shù)組中。
1.3?注意事項
- 在標(biāo)準(zhǔn)輸入數(shù)據(jù)時,按Enter鍵換行,輸完所有數(shù)據(jù)后,要按Ctrl+D來結(jié)束輸入(Ctrl+D在屏幕上無顯示)。
- 如果指定的數(shù)組變量原來已儲存有數(shù)值,在使用readarray命令時沒有-O選項,那么數(shù)組變量中原有的數(shù)據(jù)會先被清空,然后再存儲新讀取的數(shù)據(jù)。
- 如果不指定數(shù)組名,則數(shù)據(jù)會保存到MAPFILE數(shù)組中。
2?命令應(yīng)用實例
2.1 從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù)時不指定數(shù)組名,則數(shù)據(jù)會保存到MAPFILE數(shù)組中
例 2.1??
purpleEndurer @ bash ~ $ readarray
1 1 1
2 2 2
purpleEndurer @ bash ~ $ echo $REPLYpurpleEndurer @ bash ~ $ echo $MAPFILE
1 1 1
purpleEndurer @ bash ~ $ echo ${MAPFILE[*]}
1 1 1 2 2 2
purpleEndurer @ bash ~ $ echo ${MAPFILE[0]}
1 1 1
purpleEndurer @ bash ~ $ echo ${MAPFILE[1]}
2 2 2
purpleEndurer @ bash ~ $?
我們輸入了1 1 1和2 2 2兩行數(shù)據(jù)后,按Ctrl+D結(jié)束輸入。
對于read命令,如果不指定用來存儲數(shù)據(jù)的變量名,數(shù)據(jù)將保存在變量REPLY中。
但對于readarray命令,如果不指定用來存儲數(shù)據(jù)的數(shù)組變量名,數(shù)據(jù)將保存到存儲到MAPFILE數(shù)組中。
2.2 從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù)并存儲到指定的數(shù)組
?例2.2?從標(biāo)準(zhǔn)輸入讀取兩行數(shù)據(jù)并存儲到指定的數(shù)組變量a
purpleEndurer @ bash ~ $ readarray a
1 2 3
4 5 6
purpleEndurer @ bash ~ $ echo $a
1 2 3
purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3 4 5 6
purpleEndurer @ bash ~ $ echo ${a[0][*]}
1 2 3purpleEndurer @ bash ~ $ echo ${a[1][*]}
4 5 6
purpleEndurer @ bash ~ $?
我們輸入了 1 2 3和4 5 6兩行數(shù)據(jù),可以看到數(shù)據(jù)存儲到數(shù)組變量a中。
系統(tǒng)默認(rèn)從數(shù)組下標(biāo)0開始存儲,所以命令執(zhí)行的結(jié)果如下:
a[0][0]=1??a[0][1]=2??a[0][2]=3
a[1][0]=4? a[1][1]=5? a[1][2]=6
2.3?使用 -O?選項指定起始下標(biāo)
例 2.3.1?在例2.2的基礎(chǔ)上,我們繼續(xù)從標(biāo)準(zhǔn)輸入讀取兩行數(shù)據(jù)并存儲到指定的數(shù)組a,起始下標(biāo)為1
purpleEndurer @ bash ~ $ readarray -O1 a
a b c
d e f
purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3 a b c d e f
purpleEndurer @ bash ~ $ echo ${a[1][*]}
a b c
purpleEndurer @ bash ~ $ echo ${a[2][*]}
d e f
purpleEndurer @ bash ~ $???
我們輸入了a b c和d e f 兩行數(shù)據(jù)。由于我們指定從下標(biāo)1開始,
所以二維數(shù)組a的第一行數(shù)據(jù)沒有變化
二維數(shù)組a的第二行數(shù)據(jù)變成 [a b c]
[d e f]則變成了二維數(shù)組a的第三行的數(shù)據(jù)。
這時的二維數(shù)組a的值為:
a[0][0]=1??a[0][1]=2??a[0][2]=3
a[1][0]=a? a[1][1]=b? a[1][2]=c
a[2][0]=d? a[2][1]=e? a[2][2]=f
可見,對于二維數(shù)組來說,-O指定的是起始行數(shù)。
那么,對于一維數(shù)組呢?-O指定的是什么呢?
我們通過下面的例子來看一下。
例2.3.2?先定義一維數(shù)組a并初始化其值為1 2 3,然后用readarray命令讀取數(shù)據(jù) a b c,并指定從數(shù)組a的下標(biāo)2開始存儲。
purpleEndurer @ bash ~ $ a=( 1 2 3)
purpleEndurer @ bash ~ $ echo $a
1
purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3
purpleEndurer @ bash ~ $ readarray -O2 a
a b cpurpleEndurer @ bash ~ $ echo ${a[*]}
1 2 a b c
purpleEndurer @ bash ~ $?
注意:
在輸入a b c后要按Ctrl+D兩次,這樣可以讓數(shù)組a保持為一維數(shù)組。
如果按下了Enter鍵,數(shù)組a將變成二維數(shù)組。
可以看到,對于一維數(shù)組來說,-O選項指定的是元素的下標(biāo)。
例2.3.3?不使用-O選項,指定數(shù)組名中原有數(shù)據(jù)會先被清空
purpleEndurer @ bash ~ $ readarray a
1
2
3
4purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3 4
purpleEndurer @ bash ~ $ readarray a
a
b
purpleEndurer @ bash ~ $ echo ${a[*]}
a b
purpleEndurer @ bash ~ $?
在第一次執(zhí)行?readarray a?命令時,我們輸入的數(shù)據(jù)1、2、3、4被存儲到數(shù)據(jù)變量a中。
在第二次執(zhí)行?readarray a?命令時,我們輸入的數(shù)據(jù)a、b被存儲到數(shù)據(jù)變量a中,原來的數(shù)據(jù)1、2、3、4被清空了。
2.4?用-n指定有效行數(shù)
例 2.4?從標(biāo)準(zhǔn)輸入讀取2行數(shù)據(jù),儲存到數(shù)組變量a。
purpleEndurer @ bash ~ $ echo $a
purpleEndurer @ bash ~ $ readarray -n 2 a
1 1 1
2 2 2
purpleEndurer @ bash ~ $ echo ${a[*]}
1 1 1 2 2 2
purpleEndurer @ bash ~ $ echo ${a[1]}
2 2 2
purpleEndurer @ bash ~ $ echo ${a[0]}
1 1 1
purpleEndurer @ bash ~ $?
可以看到,我們輸入兩行數(shù)據(jù)后,readarray命令就自動停止輸入,并將我們輸入的數(shù)據(jù)存儲到數(shù)組變量a中。
2.5?用-s來路過部分?jǐn)?shù)據(jù)
例 2.5?跳過標(biāo)準(zhǔn)輸入中的前2行數(shù)據(jù),將后續(xù)的數(shù)據(jù)存儲到數(shù)組變量a中。
purpleEndurer @ bash ~ $ echo $a
purpleEndurer @ bash ~ $ readarray -s 2 a
1 1 1
2 2 2
3 3 3
4 4 4?
purpleEndurer @ bash ~ $ echo ${a[*]}
3 3 3 4 4 4
purpleEndurer @ bash ~ $ echo ${a[1]}
4 4 4
purpleEndurer @ bash ~ $ echo ${a[0]}
3 3 3
purpleEndurer @ bash ~ $?
我們輸入了1 1 1 、2 2 2、3 3 3、4 4 4四行數(shù)據(jù),由于-s 2?選項,前兩行數(shù)據(jù)1 1 1 、2 2 2被跳過,數(shù)組變量a存儲的數(shù)據(jù)是3 3 3、4 4 4,即:
a[0][0]=3?a[0][1]=3??a[0][2]=3
a[1][0]=4? a[1][1]=4? a[1][2]=4
2.6?用-c和-C選項使用回調(diào)程序
例 2.6?從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),每讀入2行數(shù)據(jù)就調(diào)用echo命令顯示字符串---
purpleEndurer @ bash ~ $ readarray -c 2 -C "echo ---"
a
b
--- 1 bc
d
--- 3 de
f
--- 5 fpurpleEndurer @ bash ~ $ echo ${MAPFILE[*]}
a b c d e f
purpleEndurer @ bash ~ $?
2.7?使用輸出重定向和-t選項從磁盤文件中讀取數(shù)據(jù)
例2.7.1?利用seq命令創(chuàng)建數(shù)據(jù)文件d.txt,然后利用readarray和輸入重定向?qū)?shù)據(jù)文件d.txt的內(nèi)容存儲到數(shù)組變量a
purpleEndurer @ bash ~ $ seq 5 > d.log
purpleEndurer @ bash ~ $ cat d.log
1
2
3
4
5
purpleEndurer @ bash ~ $ readarray a < d.log
purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3 4 5
purpleEndurer @ bash ~ $ echo ${#a[*]}
5
purpleEndurer @ bash ~ $ echo ${#a[1]}
2
purpleEndurer @ bash ~ $ echo ${#a[1][1]}
2
purpleEndurer @ bash ~ $ echo ${#a[1][2]}
2
例2.7.2?在使用輸入重定向和readarray -t?命令從例2.7.1創(chuàng)建的d.txt文件讀取數(shù)據(jù)存儲到數(shù)組變量a
purpleEndurer @ bash ~ $ readarray -t a < d.log
purpleEndurer @ bash ~ $ echo ${a[*]}
1 2 3 4 5
purpleEndurer @ bash ~ $ echo ${#a[*]}
5
purpleEndurer @ bash ~ $ echo ${#a[1][1]}
1
purpleEndurer @ bash ~ $ echo ${#a[1][2]}
1purpleEndurer @ bash ~ $ echo ${#a[1]}
1
purpleEndurer @ bash ~ $?
從 echo ${a[*]}? 和??echo ${#a[*]}?的命令執(zhí)行結(jié)果來看,readarray a < d.log?和?readarray -t a < d.log?執(zhí)行的結(jié)果似乎是一樣的。
但從echo ${#a[1]}、echo ${#a[1][1]}、echo ${#a[1][2]}命令的執(zhí)行結(jié)果看,readarray a < d.log?和?readarray -t a < d.log?執(zhí)行的結(jié)果是不一樣的。
這是因為readarray a < d.log?沒有過濾換行符。
3 mapfile命令
mapfile命令不僅在功能上和readarray命令相同,而且在命令格式上也和readarray命令相同。
但是mapfile命令的幫助信息比readarray命令要詳細(xì)得多。
purpleEndurer @ bash ~ $ help mapfile
mapfile: mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
? ? Read lines from the standard input into an indexed array variable.
? ??
? ? Read lines from the standard input into the indexed array variable ARRAY, or
? ? from file descriptor FD if the -u option is supplied. ?The variable MAPFILE
? ? is the default ARRAY.
? ??
? ? Options:
? ? ? -n count ?Copy at most COUNT lines. ?If COUNT is 0, all lines are copied.
? ? ? -O origin Begin assigning to ARRAY at index ORIGIN. ?The default index is 0.
? ? ? -s count ?Discard the first COUNT lines read.
? ? ? -t ? ? ? ? ? ? ? ?Remove a trailing newline from each line read.
? ? ? -u fd ? ? ? ? ? ? Read lines from file descriptor FD instead of the standard input.
? ? ? -C callback ? ? ? Evaluate CALLBACK each time QUANTUM lines are read.
? ? ? -c quantum ? ? ? ?Specify the number of lines read between each call to CALLBACK.
? ??
? ? Arguments:
? ? ? ARRAY ? ? ? ? ? ? Array variable name to use for file data.
? ??
? ? If -C is supplied without -c, the default quantum is 5000. ?When
? ? CALLBACK is evaluated, it is supplied the index of the next array
? ? element to be assigned and the line to be assigned to that element
? ? as additional arguments.
? ??
? ? If not supplied with an explicit origin, mapfile will clear ARRAY before
? ? assigning to it.
? ??
? ? Exit Status:
? ? Returns success unless an invalid option is given or ARRAY is readonly or
? ? not an indexed array.
purpleEndurer @ bash ~ $?文章來源:http://www.zghlxwxcb.cn/news/detail-775110.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-775110.html
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記37:readarray命令和mapfile命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!