How can I run `done < <(curl -sL file.txt);` in older versions (1993-12-28) of ksh or update to bash?

StackOverflow https://stackoverflow.com/questions/23433030

  •  14-07-2023
  •  | 
  •  

質問

I have a script that runs flawlessly on many of the servers required. But recently it's failed on servers with old ksh versions.

Can you help me fix the offending line:

#!/bin/ksh
SUCCESS=0
FAILURE=0
while read IP
do
    CURL=$(curl -s -m 2 -x http://$IP -L http://icanhazip.com)
    if  [[ "${IP%%:*}" == "${CURL%%:*}" ]] ; then
        SUCCESS=$[SUCCESS+1]
        echo "$IP   ✓"
    else
        FAILURE=$[FAILURE+1]
        echo "$IP   X"
    fi
done < <(curl -sL vpn-proxy-list.txt);
echo "✓: $SUCCESS   X: $FAILURE"

The final line returns:

line 3: syntax error at line 14: `<(' unexpected

Unfortunately I'm unable to update ksh.

Can you help me make the done < <(curl -sL vpn-proxy-list.txt); portion simply work in bash? Or compatible with older versions (1993) of ksh?

役に立ちましたか?

解決

You don't appear to be doing anything in the while body that cause trouble if it was run in a subshell, so I'd just stick with a plain pipline:

#!/bin/ksh

curl -sL vpn-proxy-list.txt | while read -r ip; do
    output=$(curl -s -m 2 -x "http://$ip" -L http://icanhazip.com)
    if  [[ "${ip%%:*}" == "${output%%:*}" ]]; then
        echo "$ip   Y"
    else
        echo "$ip   X"
    fi
done

Now you're asking something that breaks because you're making variable changes in a subshell, and those variables disappear when the subshell exits. A workaround: use grouping braces

curl -sL vpn-proxy-list.txt | {
    success=0
    failure=0
    while read -r ip; do
        output=$(curl -s -m 2 -x "http://$ip" -L http://icanhazip.com)
        if  [[ "${ip%%:*}" == "${output%%:*}" ]]; then
            echo "$ip   Y"
            let success+=1
        else
            echo "$ip   X"
            let failure+=1
        fi
    done
    echo there were $success successes
    echo there were $failure failures
}
# variables "success" and "failure" don't exist here.

他のヒント

You could make use of named pipes.

mkfifo foobar
curl -sL vpn-proxy-list.txt > foobar &
# Maybe sleep for a while here
while read -r IP; do
  # do something here
done < foobar
rm foobar
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top