質問

Im trying to automate a download of multiple files from a server. There is a nested loop structure to pass through directories. The files in one directory are numbered and I'm just iterating through them using a counter. The indication that a directory is devoid of all required files is when a 404 is encountered. However, the break statement in the if statement seems to break out of both loops during this case. I tried giving break 1 to indicate that only one loop is to be stopped, but to no avail.

page=$1
chapStart=$2
chapEnd=$3
k=$2
for i in {$2..$3}
do
        j=1
        while true
        do
                if ((j < 10))
                then
                        pg=0"$j"
                else
                        pg=$j
                fi
                res=$(wget --timeout=10 http://www.somesite.com/content/content/"$page"/"$k"/0"$pg".file)
                if(($? != 0))
                then
                        break 1
                fi
                let ++j
        done
        let ++k
done

I need the break to exit just the while loop when encountered.

Edit As per chepner's correct answer, the problem was in the for loop structure rather than the break statement, which functions rather beautifully. Updated, working code is:

page=$1
chapStart=$2
chapEnd=$3
k=$2
for((i=$2;i<=$3;i++));
do
        j=1
        while true
        do
                if ((j < 10))
                then
                        pg=0"$j"
                else
                        pg=$j
                fi
                res=$(wget --timeout=10 http://www.somesite.com/content/content/"$page"/"$k"/0"$pg".file)
                if(($? != 0))
                then
                        break 1
                fi
                let ++j
        done
        let ++k
done
役に立ちましたか?

解決

You cannot use parameter expansion inside brace expansions. Your outer loop only has one iteration, with i set to the literal string {$2..$3}. You should use one of

for ((i=$2; i<=$3; i++)); do

or

for i in $(seq $2 $3); do

instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top