I'm making a bash script in which I need to print a number while it's incremented like this:
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.
$ ~ #!/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:
for ((i=0; i<10; i++)); do
printf "%04d\n" $i
one
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 -w 0000 0010
(if you don't like the {0000..0010}
notation, which is more efficient but doesn't allow parameter substitution.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.