繁体   English   中英

bash递增数组值脚本

[英]bash incrementing array values script

我需要生成数字值并在curl上使用该值。 我已经做到了,但是代码有一些错误,因为无法生成数字。

#!/bin/bash

for a in $(seq 0 9)
do
pass[0]="$a"
   for b in $(seq 0 9)
   do
   pass[1]="$b"
        for c in $(seq 0 9)
        do
        pass[2]="$c"
            for d in $(seq 0 9)
            do
            pass[3]="$d"
                for e in $(seq 0 9)
                do
                pass[4]="$e"
                    for f in $(seq 0 9)
                    do
                    pass[5]="$f"
                   curl -d "$a$b$c$d$e$f" somesite.php
                    done
                done
             done
         done    
     done
done

我得到这个输出:

   echo ${pass[*}
>                     done
> 
Display all 289 possibilities? (y or n)
> e
>              done
>          done    
>      done
> done
> ^C

更简单(但仍然很糟糕):

for a in $(seq 0 9)
do
    for b in $(seq 0 9)
    do
        for c in $(seq 0 9)
        do
            for d in $(seq 0 9)
            do
                for e in $(seq 0 9)
                do
                    for f in $(seq 0 9)
                    do
                        curl -d "$a$b$c$d$e$f" somesite.php
                    done
                done
            done
        done    
    done
done

我不认为您复制到问题的脚本就是您执行的脚本。 当我运行脚本时,它会执行以下命令:

curl -d 0 2 0 8 7 9 somesite.php

注意空格; 我怀疑它们是(a)不必要的,并且(b)在不使用IFS的情况下是不可避免的,这比修复我所展示的代码更糟糕。 数组很棒; 他们不是一无所有。

顺便说一句,(到目前为止)使用起来会更简单:

for number in $(seq -f '%06.0f' 0 1000000)
do
    curl -d "$number" somesite.php
done

要么:

seq -f '%06.0f' 0 1000000 |
while read number
do
    curl -d "$number" somesite.php
done

或...还有其他方法可以做到(请参阅其他答案)。 seq | while read seq | while read循环的优点是不需要超过7 MiB的内存来存储数字。

curl一百万页需要一段时间。

为什么不这样做呢?

#!/bin/bash
for ((i=0;i<1000000;++i)); do
    printf -v a "%.6d" "$i"
    curl -d "$a" somesite.php
done

编辑。 由于您想使用“更快”的方法,因此这是我能想到的最快的方法(顺便说一句,这是所有方法中最短的一种):

#!/bin/bash
for i in {000000..999999}; do
    curl -d "$i" somesite.php
done

我认为这要好得多:

for (( i=0; i<1000000; i++ )); do 
    VAL=$(printf "%06d" $i)
    curl -d "$VAL" somesite.php
done

试试这个

for i in `seq -f '%06.f' 0 999999`; do
    curl -d "$i" somesite.php
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM