0

I am using for loops in bash to loop over an array of IPs from file. There are around 38 IPs and in one file and I want to select any range of IP like for example I should be able to loop over if a user asks to go for IP address from range 10 to 20. But with the for loop I am not able to find out how to use seq or give a range. Below is an example:

mapfile -t ip < SippIPs.txt
echo
read -p 'How many Sipp's are required: ' choice
echo

for ((j=1; j<=$choice; j++)); do
        sipps=${j[@]}
        ips=(${ip[sipps-1]})
        echo "$sipps"
        echo "$ipList"

        echo "    ----  Launching SIPp $sipps ----  "
        sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@$ips <<EOF1
        screen -S sipp -d -m bash -c 'cd /usr/local/src/sipp-3.3; ulimit -Hn 65535; ulimit -Sn 65535; ./sipp -i $ips -mi $ips -sf HA_demo.xml -inf HA_demo.csv 10.171.0.231:5060 -p 5060 -r 1 -rp 1s -l 1 -m 1 -watchdog_minor_threshold 1500 -watchdog_major_threshold 4000 -watchdog_major_maxtriggers 30 -trace_err -aa -d 350s -oocsn ooc_default -t u1 -trace_screen -skip_rlimit && exec bash'
        exit
EOF1
done

Below is how my ips text file looks like:

[root@megahost23 MasterScript]# cat SippIPs.txt
10.171.0.201
10.171.0.202
10.171.0.203
10.171.0.204
10.171.0.205
10.171.0.206
10.171.0.207
10.171.0.208
10.171.0.209
10.171.0.210
10.171.0.211
10.171.0.212
10.171.0.213
10.171.0.214
10.171.0.215

Can someone guide please how can I give a range with the abovementioned for loop I am using?

12
  • 1
    I don't understand sipps=${j[@]}. $j is a number, not an array. Commented Nov 10, 2021 at 15:08
  • Just storing it in a variable. The code is working fine. Commented Nov 10, 2021 at 15:09
  • 2
    for ((j = 10; j <= 20; j++)) Commented Nov 10, 2021 at 15:12
  • 2
    More generally, for ((j = $start; j <= $end; j++)) Commented Nov 10, 2021 at 15:13
  • 1
    read -p 'Start from' start; read -p 'End at' end then use the above loop. Commented Nov 10, 2021 at 15:16

1 Answer 1

1

How about something like this:

$ read -p 'min: ' min
min: 3

$ read -p 'max: ' max
max: 10

$ mapfile -t zumba < /tmp/range
$ for (( i=min-1; i <= max-1 ; i++ )) ; do echo ${zumba[i]}; done
c
d
e
f
g
h
i
j
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.