It looks like
gives advice on how to set proxy using a script instead of using the options menu. I'm not going to try to use a proxy because "reasons".
Also, the /help description of the proxy command indicates that "userid" and "password" are REQUIRED fields,
Do you know which proxy you're using by having your $ip changed to match the proxy IP?
i.e. when you use: //echo -a $ip
does that change to match the proxy you're using? If so, it should be easy to modify your script to compare the active proxy against the list, so it won't retry the same one.
Is using socks4/socks5/proxy supposed to be your choice, or does the proxy server require you to use one of those 3 settings which they prefer?