下面是我设置代理的bash脚本,我绝不是bash的专家。我怀疑有更好的方式来写这个,任何提示都会受到赞赏。
添加更多详细信息以满足stackoverflows要求。有没有更好的方法来执行嵌套的while循环和if语句?
#!/bin/bash
set -e
#functions
function yesno() {
while read -n1 -r -p "[y/n/QUIT] > "; do
case $REPLY in
y|n) return;;
q) echo; echo; exit 1;;
*) printf "\nERROR - Invalid response, please enter Yes OR No.\n";;
esac
done
}
function startover() {
printf "\n\n\e[1m\e[31mStarting Over...\e[0m\n"
sleep 2
unset proxyport proxyhost
}
shopt -s nocasematch
while true; do
printf "\nIs a proxy required to access outbound \e[1m\e[7mHTTP/HTTPS\e[0m sites?\n"
yesno
if [[ $REPLY == "y" ]]; then
printf "\n\nPlease enter your proxy url\nExample - \e[1m\e[7mproxy.example.com:8080\e[0m\n"
while read -r -p "Proxy > " proxy; do
if [[ $proxy =~ ^https:// && $proxy =~ :[0-9]{1,5}$ ]]; then
printf "\nERROR - HTTPS with a port is not supported, please re-enter without '\e[1m\e[97m\e[41mHTTPS://\e[0m' prefix\n"
elif [[ $proxy =~ ^(https?://)?([a-z0-9][-a-z0-9\.]{0,61}[a-z0-9]\.[a-z]{2,5})?.((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))?(:[0-9]{1,5})?$ ]]; then
if [[ $proxy =~ :[0-9]{1,5}$ ]]; then
proxyport=${proxy##*:}
proxy=${proxy%:*}
echo "Port entered $proxyport"
elif [[ $proxy =~ ^https:// && ! $proxy =~ :[0-9]{1,5}$ ]]; then
proxyport=443
echo "URL starts with HTTPS, assuming $proxyport"
else
proxyport=80
echo "No port entered, assuming $proxyport"
fi
proxyhost=${proxy##*/}
break
else
printf "\nERROR - \e[1m\e[97m\e[41m$proxy\e[0m is not supported please try again\n"
fi
done
printf "\nCreating Proxy Settings\n"
sleep 2
printf "Proxy Hostname: \e[1m\e[97m\e[44m$proxyhost\e[0m\nProxy Port: \e[1m\e[97m\e[44m$proxyport\e[0m\nDoes the information above look correct?\n"
yesno
if [[ $REPLY == "n" ]]; then
startover
fi
fi
break
done
shopt -u nocasematch