Registered User
Registered User

Reputation: 265

zsh process file script

I'm attempting to have this process a number of files but I don't want it in a look so I don't have to monitor it.

#!/usr/local/bin/zsh
X=${1-20}
for (( N=1; N<=X; N++ )); do
        for p in *.xml; do
                    curl -X POST -H "Content-Type:application/xml"  -d "@${p}" "https://url /postAPI" > "post_${p}"
                        sleep 1
                done
        done

When doing ./work.sh 5 this loops forever!

What's causing infinate loop?

Edit Based on a comment below

/tmp/tmp.KeFYeM9Z % ls -l
total 4
-rwxr-xr-x  1 naes  wheel  218 Nov 20 14:42 work.sh

#!/usr/local/bin/zsh
X=${1-20}
for (( N=1; N<=X; N++ )); do
        for p in /tmp/tmp.u6RnKaJ3/*.xml; do
                    curl -X POST -H "Content-Type:application/xml"  -d "@${p}" "https://url /postAPI"
                        sleep 1
                done
        done

This still continues the infinite loop

This doesn't:

 % cat work1.sh
#!/usr/local/bin/zsh
X=${1-20}
for (( N=1; N<=X; N++ )); do
        date
        sleep 1
done



% ./work1.sh 5
Thu Nov 20 15:22:27 PST 2014
Thu Nov 20 15:22:28 PST 2014
Thu Nov 20 15:22:29 PST 2014
Thu Nov 20 15:22:30 PST 2014
Thu Nov 20 15:22:31 PST 2014

What in my loop causes the infinite?

Upvotes: 0

Views: 195

Answers (1)

Wolph
Wolph

Reputation: 80031

You are writing to the same directory you are reading from. So while reading the xml files you are writing xml files making it essentially loop forever. Although it's not really an infinite loop, it's a very large one.

Let's say you have 10 files, in that case you'll have this result:

N=1: |p| = 100
N=2: |p| = 200
N=3: |p| = 400
N=4: |p| = 800
N=5: |p| = 1600

So... it groes quite fast.

This should do the trick:

#!/usr/local/bin/zsh

X=${1-20}
OUTPUT_DIR=/tmp/output/
mkdir -p $OUTPUT_DIR

cd /tmp/tmp.u6RnKaJ3
for (( N=1; N<=X; N++ )); do
    echo "Attempt $N"

    for p in *.xml; do
        curl -X POST -H "Content-Type:application/xml"  -d "@${p}" "https://url /postAPI" > "${OUTPUT_DIR}post_${p}"
        sleep 1
    done
done

Upvotes: 3

Related Questions