简体   繁体   中英

Increment a variable by 1 bash

Hello I am a super newbie to bash and linux but I am trying to modify a script I have to be able to create a file and in that file print what I am requesting but have the port number increase by one each time.

This is what I have now but it does not do the job.

rm -rf /etc/squid_squid_user
touch /etc/squid_squid_user
IPADDRESS=$(wget ipinfo.io/ip -q -O -)
portid=10000
portid=(($portid+1))
clear
echo "Enter a username for your proxies."
read username
echo
echo "Enter a password for your proxies."
read password
htpasswd -cdb /etc/squid/squid_user $username $password
sudo tee -a proxies.txt <<EOF
$IPADDRESS:$portid:$username:$password
EOF

I will want the output to be like the example bellow

45.32.14.256:10001:test:run
45.32.14.256:10002:test:run
45.32.14.256:10003:test:run
45.32.14.256:10004:test:run
45.32.14.256:10005:test:run

I have commented some lines of your original code and introduced a loop.

# Original code: IPADDRESS=$(wget ipinfo.io/ip -q -O -)
IPADDRESS=45.32.14.256

# Original code: variable number set earlier
read -p "How many proxies? " number
portid=1000
((maxport=portid+number))
while (( portid < maxport)); do
   ((portid++))
   # original code: clear
   echo "Enter a username for your proxies."
   # original code: read username
   username=test
   echo
   echo "Enter a password for your proxies."
   # original code: read password
   password=run
   # Original code: htpasswd -cdb /etc/squid/squid_user $username $password
   sudo tee -a /tmp/proxies.txt <<EOF
$IPADDRESS:$portid:$username:$password
EOF
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM