0 前言
在使用 for 循環(huán)語句時(shí),我們經(jīng)常使用到序列。比如:
for i in 1 2 3 4 5 6 7 8 9 10; do echo "$i * 2 = $(expr $i \* 2)"; ?done
其中的 1 2 3 4 5 6 7 8 9 10;就是一個(gè)整數(shù)序列 。
為了方便我們使用數(shù)字序列,Linux提供了seq命令,這個(gè)命令是取自單詞sequence的前3個(gè)字母。比如:
for i in $(seq 1 10) ; do
?更多信息請(qǐng)回顧:
Linux shell編程學(xué)習(xí)筆記17:for循環(huán)語句-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/134102934?spm=1001.2014.3001.5501
其實(shí),seq命令的用途和使用環(huán)境很廣闊?,F(xiàn)在我們就來探究一下。
1 seq命令的格式、功能
我們可以使用命令?seq --help ?來查看seq命令的幫助信息:
purpleEndurer @ bash ~ $ seq --help
Usage: seq [OPTION]... LAST
? or: ?seq [OPTION]... FIRST LAST
? or: ?seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.Mandatory arguments to long options are mandatory for short options too.
? -f, --format=FORMAT ? ? ?use printf style floating-point FORMAT
? -s, --separator=STRING ? use STRING to separate numbers (default: \n)
? -w, --equal-width ? ? ? ?equalize width by padding with leading zeroes
? ? ? --help ? ? display this help and exit
? ? ? --version ?output version information and exitIf FIRST or INCREMENT is omitted, it defaults to 1. ?That is, an
omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.
The sequence of numbers ends when the sum of the current number and
INCREMENT would become greater than LAST.
FIRST, INCREMENT, and LAST are interpreted as floating point values.
INCREMENT is usually positive if FIRST is smaller than LAST, and
INCREMENT is usually negative if FIRST is greater than LAST.
FORMAT must be suitable for printing one argument of type 'double';
it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point
decimal numbers with maximum precision PREC, and to %g otherwise.GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report seq translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'seq invocation'
purpleEndurer @ bash ~ $?
1.1 seq 命令的格式
seq [選項(xiàng)]... [序列起始數(shù)] [步長(zhǎng)] 序列結(jié)尾數(shù)
1.1.1 參數(shù):
- 序列起始數(shù):可以是0、正整數(shù) 或 負(fù)整數(shù),未指定時(shí)默認(rèn)為1
- 序列結(jié)尾數(shù):可以是0、正整數(shù) 或 負(fù)整數(shù)
- 步長(zhǎng)? ? ? ? ? ?:可以是 正整數(shù) 或 負(fù)整數(shù),未指定時(shí)默認(rèn)為1
1.1.2 選項(xiàng):
選項(xiàng) | 說明 | 備注 |
---|---|---|
-f格式字符串 -f?格式字符串 -f=格式字符串 --format=FORMAT |
使用printf命令中的格式字符串來格式化輸出,默認(rèn)為%g 數(shù)字位數(shù)不足部分默認(rèn)是空格 不能與 -w?或?--equal-width?同時(shí)使用 |
format |
-s分隔字符串 -s?分隔字符串 -s=分隔字符串 --separator 分隔字符串 --separator=分隔字符串 |
使用指定的字符串作為數(shù)字間的分隔符(默認(rèn)字符串是:\n) 指定作分隔符的字符串可以是空字符串,也可以1位字符,或者多位字符 |
separator |
-w? --equal-width |
通過用前導(dǎo)零填充來均衡寬度 不能與 -f 或?--format 同時(shí)使用 |
width |
--help | 顯示幫助信息并退出 | help |
--version | 輸出版本信息并退出 | version |
1.2 seq命令的功能
生成以超始數(shù)開始,逐一加上步長(zhǎng),直到結(jié)尾數(shù)的數(shù)列并以指定的格式輸出。
2 seq 命令用法實(shí)例
2.1 seq不帶參數(shù)會(huì)報(bào)錯(cuò),返加值為1
purpleEndurer @ bash ~ $ seq
seq: missing operand
Try 'seq --help' for more information.
purpleEndurer @ bash ~ $ echo $?
1
purpleEndurer @ bash ~ $?
2.2?生成遞增序列
例 2.2?生成從1開始,步長(zhǎng)為1,到10為止的序列
完整的命令是:seq 1 1 10
purpleEndurer @ bash ~ $ seq 1 1 10
1
2
3
4
5
6
7
8
9
10
purpleEndurer @ bash ~ $?
由于seq的?步長(zhǎng)未指定時(shí)默認(rèn)為1,所以我們可以將命令中代表步長(zhǎng)的第二個(gè)1省略,從而簡(jiǎn)寫為:seq 1?10
purpleEndurer @ bash ~ $ seq 1 10
1
2
3
4
5
6
7
8
9
10
purpleEndurer @ bash ~ $?
由于seq的?序列起始數(shù)未指定時(shí)默認(rèn)為1,所以我們可以將命令中的第一個(gè)1省略,進(jìn)一步簡(jiǎn)寫為:seq 10
purpleEndurer @ bash ~ $ seq 10
1
2
3
4
5
6
7
8
9
10
purpleEndurer @ bash ~ $?
2.3 生成遞減序列
例 2.3?生成從10開始,步長(zhǎng)為-2,到-10為止的序列
完整的命令是:seq 10 -2?-10
purpleEndurer @ bash ~ $ seq 10 -2 -10
10
8
6
4
2
0
-2
-4
-6
-8
-10
purpleEndurer @ bash ~ $?
?
2.4?指定格式字符串
2.4.1?系統(tǒng)默認(rèn)為%g
purpleEndurer @ bash ~ $ seq -f '%g' 10 -2 -10
10
8
6
4
2
0
-2
-4
-6
-8
-10
purpleEndurer @ bash ~ $
可以看到,當(dāng)我們指定格式字符串為%g時(shí),顯示結(jié)果與不指定是一樣的。
2.4.2? 自定義格式字符串
例如我們指定以>開頭,寬度為5來顯示
以下命令均可以實(shí)現(xiàn):
- seq -f=">%5g" 10 -2 -10
- seq -f">%5g" 10 -2 -10
- seq -f ">%5g" 10 -2 -10
- seq --format=">%5g" 10 -2 -10
- seq --format ">%5g" 10 -2 -10
?
purpleEndurer @ bash ~ $ seq -f '>%5g' 10 -2 -10
> ? 10
> ? ?8
> ? ?6
> ? ?4
> ? ?2
> ? ?0
> ? -2
> ? -4
> ? -6
> ? -8
> ?-10
purpleEndurer @ bash ~ $ seq -f ">%5g" 10 -2 -10
> ? 10
> ? ?8
> ? ?6
> ? ?4
> ? ?2
> ? ?0
> ? -2
> ? -4
> ? -6
> ? -8
> ?-10
purpleEndurer @ bash ~ $ seq --format=">%5g" 10 -2 -10
> ? 10
> ? ?8
> ? ?6
> ? ?4
> ? ?2
> ? ?0
> ? -2
> ? -4
> ? -6
> ? -8
> ?-10
purpleEndurer @ bash ~ $ seq --format ">%5g" 10 -2 -10
> ? 10
> ? ?8
> ? ?6
> ? ?4
> ? ?2
> ? ?0
> ? -2
> ? -4
> ? -6
> ? -8
> ?-10
purpleEndurer @ bash ~ $?
可以看到,seq命令默認(rèn)是用空格來補(bǔ)位,如果我們想用0來補(bǔ)位,可以使用命令:
seq -f">%05g" 10 -2 -10
purpleEndurer @ bash ~ $ seq -f">%05g" 10 -2 -10
>00010
>00008
>00006
>00004
>00002
>00000
>-0002
>-0004
>-0006
>-0008
>-0010
purpleEndurer @ bash ~ $
2.5?使用-w選項(xiàng)平衡寬度(不足時(shí)用0填充)
purpleEndurer @ bash ~ $ seq -w 10 -2 -10
010
008
006
004
002
000
-02
-04
-06
-08
-10
purpleEndurer @ bash ~ $?
雖然 -w?和 -f"%5g"都是以0來填充空位的,但是對(duì)比二者的顯示結(jié)果,可以發(fā)現(xiàn)二者存在一定的區(qū)別:
- -w是以生成的序列中位數(shù)最長(zhǎng)的位數(shù)(包括負(fù)號(hào)-)為最大位數(shù)
例如在命令?seq -w 10 -2 -10??中,生成的序列中 位數(shù)最長(zhǎng)的是-10,即3位數(shù),所以生成序列均為3位數(shù)。
- -f選項(xiàng)可以直接指定位數(shù)
例如在命令?seq -f">%05g" 10 -2 -10 中,生成的序列中 位數(shù)最長(zhǎng)的是-10,即3位數(shù),但由于我們指定寬度為5,所以顯示的序列均為5位數(shù)。
2.6?指定序列分隔符
seq命令默認(rèn)序列分隔符為換行符\n,我們可能使用-s或--separator指定其它分隔符,指定的分隔符字符串可以是空字符串,也可以1位字符,或者多位字符。
例如,生成從10開始,步長(zhǎng)為-2,到-10為止的序列,以兩個(gè)冒號(hào)作為分隔符
使用命令都可以實(shí)現(xiàn):
- seq -s '::' 10 -2?-10
- seq -s'::' 10 -2?-10
- seq -s='::' 10 -2?-10
- seq --separator '::' 10 -2?-10
- seq --separator='::' 10 -2?-10
purpleEndurer @ bash ~ $ seq -s '::' 10 -2 -10
10::8::6::4::2::0::-2::-4::-6::-8::-10
purpleEndurer @ bash ~ $ seq --separator '::' 10 -2 -10
10::8::6::4::2::0::-2::-4::-6::-8::-10
purpleEndurer @ bash ~ $ seq --separator='::' 10 -2 -10
10::8::6::4::2::0::-2::-4::-6::-8::-10
purpleEndurer @ bash ~ $ seq -s='::' 10 -2 -10
10=::8=::6=::4=::2=::0=::-2=::-4=::-6=::-8=::-10
purpleEndurer @ bash ~ $?
我們也可以使用空字符串作為分隔符
purpleEndurer @ bash ~ $ seq --separator '' 10 -2 -10
1086420-2-4-6-8-10
purpleEndurer @ bash ~ $?
3 seq?命令的類型 (is hashed?)
在
Linux shell編程學(xué)習(xí)筆記33:type 命令-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/134804451?spm=1001.2014.3001.5501中,我們知道type命令?可以顯示指定命令的信息,判斷給出的指令是內(nèi)部命令、外部命令(文件)、別名、函數(shù)、保留字 或者 不存在(找不到)。
但對(duì)于seq命令,type命令顯示的是?“seq is hashed (/usr/bin/seq)”
purpleEndurer @ bash ~ $ type seq
seq is hashed (/usr/bin/seq)
purpleEndurer @ bash ~ $?
這是什么意思呢?
bing了一下,在bash - What does "is hashed" mean when using the type command? - Unix & Linux Stack Exchangehttps://unix.stackexchange.com/questions/251731/what-does-is-hashed-mean-when-using-the-type-command
給出的解釋是:
What that means is that after finding the location of a command the first time (or when hash is invoked), its location is remembered (hashed).
翻譯過來就是:
這意味著,在第一次找到命令的位置(或調(diào)用哈希時(shí))后,會(huì)記住它的位置(散列)。
我們可以通過下面的命令序列來理解:
?purpleEndurer @ bash ~ $ type seq
seq is /usr/bin/seq
purpleEndurer @ bash ~ $ seq
seq: missing operand
Try 'seq --help' for more information.
purpleEndurer @ bash ~ $ type seq
seq is hashed (/usr/bin/seq)
purpleEndurer @ bash ~ $?
當(dāng)我們登錄系統(tǒng)后,還沒有執(zhí)行seq命令時(shí),使用type seq命令,命令顯示的信息是:seq is /usr/bin/seq
說明它是個(gè)外部命令。
當(dāng)我們執(zhí)行seq命令后,再使用type seq命令,命令顯示的信息是:seq is hashed (/usr/bin/seq)文章來源:http://www.zghlxwxcb.cn/news/detail-761796.html
即此時(shí)系統(tǒng)已記住了seq命令的位置。文章來源地址http://www.zghlxwxcb.cn/news/detail-761796.html
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記35:seq的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!