[英]bash printing and incrementing array values
I'm making a bash script in which I need to print a number while it's incremented like this: 我正在制作一个bash脚本,在这个脚本中我需要打印一个数字,而它的增量如下:
0000
0001
0002
0003
0004
I have made this but is not working: 我做了这个,但没有工作:
#!/bin/bash
i=0
pass[0]=0
pass[1]=0
pass[2]=0
pass[3]=0
for i in $(seq 1 9)
pass[3]="$i"
echo ${pass[*]}
done
I paste the script on cli and i get this. 我将脚本粘贴在cli上,我得到了这个。
$ ~ #!/bin/bash
$ ~ i=0
$ ~ pass[0]=0
$ ~ pass[1]=0
$ ~ pass[2]=0
$ ~ pass[3]=0
$ ~ for i in $(seq 1 9)
> pass[3]="$i"
bash: error sintáctico cerca del elemento inesperado `pass[3]="$i"'
$ ~ echo ${pass[*]}
0 0 0 0
$ ~ done
bash: error sintáctico cerca del elemento inesperado `done'
$ ~
Use this pure bash script: 使用这个纯粹的bash脚本:
for ((i=0; i<10; i++)); do
printf "%04d\n" $i
one
OUTPUT: OUTPUT:
0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
#!/bin/bash
i=0
pass[0]=0
pass[1]=0
pass[2]=0
pass[3]=0
for i in $(seq 1 9)
do
pass[3]="$i"
echo ${pass[*]}
done
did you forget 'do' 你忘了'做'吗?
For those of you who like expansions, you can also do: 对于那些喜欢扩展的人,你也可以这样做:
printf "%s\n" {0001..0009}
or 要么
printf "%.4d\n" {1..9}
No loop! 没有循环!
You can store in an array thus: 您可以存储在数组中:
$ myarray=( {0001..0009} )
$ printf "%s\n" "${myarray[@]}"
0001
0002
0003
0004
0005
0006
0007
0008
0009
$ echo "${myarray[3]}"
0004
You can do the formatting with seq
: 您可以使用seq
进行格式化:
seq -w 0000 0010
(if you don't like the {0000..0010}
notation, which is more efficient but doesn't allow parameter substitution.) (如果您不喜欢{0000..0010}
表示法,它更有效但不允许参数替换。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.