将变量增加1 bash

时间:2017-08-12 22:19:00

标签: linux bash sh

您好我是bash和linux的超级新手,但我正在尝试修改脚本,我必须能够创建一个文件,并在该文件中打印我要求的但每次都增加一个端口号。

这就是我现在所拥有的,但它不起作用。

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

我希望输出类似于下面的示例

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

1 个答案:

答案 0 :(得分:0)

我已经评论了原始代码的一些行并引入了一个循环。

# 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