Below is my bash script to setup a proxy, I am by no means an expert in bash. I suspect there is a better way to write this, any tips would be appreciated.
Adding more details to satisfy stackoverflows requirement. Is there a better way to do nested while-loops and if-statements?
#!/usr/bin/env bash
set -e
#Set Proxy
shopt -s nocasematch
while :
do
echo ""
read -r -p "Is a proxy required to access outbound http/https sites? [Yes/No] " input
case $input in
[y][e][s]|[y])
printf "\nPlease enter your proxy url"
while :
do
printf "\nExample: https://proxy.example.com:8080\n"
read -r -p "Proxy: " proxy
if [[ $proxy =~ ^(https?)://[a-zA-Z0-9][-a-zA-Z0-9.]{0,}[a-zA-Z0-9](.)[a-zA-Z]{2,}$ ]]; then
if [[ $proxy =~ ^(https):* ]]; then
proxyport=443
echo "Proxy Port: $proxyport"
else
proxyport=80
echo "Proxy Port: $proxyport"
fi
break;
elif [[ $proxy =~ ^(https?)://[a-zA-Z0-9][-a-zA-Z0-9.]{0,}[a-zA-Z0-9](.)[a-zA-Z]{2,}:[0-9]{1,5}$ ]]; then
break;
else
printf "\nERROR! Please enter a fully qualified domain name starting with \e[1m\e[31mhttp://\e[0m OR \e[1m\e[31mhttps://\e[0m";
fi
done
printf "\nIs the following Proxy information correct?\n"
if [[ -z "$proxyport" ]]; then
proxyurl=${proxy%:*}
proxyhost=${proxyurl##*/}
# proxyurl=$(echo $proxy | cut -f1 -f2 -d:)
proxyport=$(echo $proxy | cut -f3 -d:)
echo "Proxy: $proxy"
else
proxyhost=${proxy##*/}
echo "Proxy: $proxy:$proxyport"
fi
echo "Proxy Hostname: $proxyhost"
echo "Proxy Port: $proxyport"
while :
do
read -r -p "Is it right? [Yes/No] " yn
case $yn in
[y][e][s]|[y])
break;;
[n][o]|[n])
printf "\nStarting Over...\n"
break;;
*)
printf "\nERROR! Please enter Yes OR No.\n";;
esac
done
if [[ $yn = ^y ]]; then
break
else
printf "\nStarting Over...\n"
fi;;
[Nn][Oo]|[Nn])
printf "\nMoving on then\n"
break;;
*)
printf "\nERROR! Please enter Yes OR No.\n";;
esac
done
shopt -u nocasematch
Aucun commentaire:
Enregistrer un commentaire