0

I am working on a bash script and I got a list of IP's that I wanted to add one by one in a CURL command. For example given list on a file named list.txt

8.8.8.8
10.10.10.10
136.34.24.22
192.168.10.32

I wanted to add each value on curl command

curl -k -u $user:$password "https://logservice/jobs" --data-urlencode 'search=search index=test $ARRAYVALUE | head 1' > output.txt

Where $ARRAYVALUE is the IP address to be used on the command.

I will appreciate any hint.

Thanks

rodlozarg
  • 57
  • 6
  • 1
    Have you considered trying to look up how to [read a file into an array](https://stackoverflow.com/questions/30988586/creating-an-array-from-a-text-file-in-bash) in bash? – that other guy Mar 18 '19 at 18:52
  • Also, you should read about [the difference between single and double quotes in bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash). – Gordon Davisson Mar 18 '19 at 19:20
  • Why do you want an array? Just to `while read ip; do ... ; done < list.txt` – William Pursell Mar 18 '19 at 21:22
  • I believe all you need is simple `xargs -I{} curl ... {}`. – liborm Mar 18 '19 at 22:10
  • Possible duplicate of [Shell script using curl to loop through urls](https://stackoverflow.com/q/16131321/608639), [How to execute the same curl request on a sequence of server IPs](https://stackoverflow.com/q/47075974/608639), etc. – jww Mar 18 '19 at 23:17

2 Answers2

1

If I understood correctly, you want to:

  • map each line of a "list.txt" to an item of an array
  • loop over the newly created array inserting items one by one into your command invocation

Consider this, heavily commented, snippet. Look especially at mapfile and how variable is used in curl invocation, surrounded by double quotes.

#!/bin/bash
# declare a (non-associative) array 
# each item is indexed numerically, starting from 0
declare -a ips
#put proper values here
user="userName"
password="password"
# put file into array, one line per array item
mapfile -t ips < list.txt

# counter used to access items with given index in an array
ii=0
# ${#ips[@]} returns array length 
# -lt makes "less than" check 
# while loops as long as condition is true
while [ ${ii} -lt ${#ips[@]} ] ; do 
  # ${ips[$ii]} accesses array item with the given (${ii}) index
  # be sure to use __double__ quotes around variable, otherwise it will not be expanded (value will not be inserted) but treated as a string
  curl -k -u $user:$password "https://logservice/jobs" --data-urlencode "search=search index=test ${ips[$ii]} | head -1" > output.txt
  # increase counter to avoid infinite loop
  # and access the next item in an array
  ((ii++))
done

You may read about mapfile in GNU Bash reference: Built-ins.

You may read about creating and accessing arrays in GNU Bash reference: Arrays

Check this great post about quotes in bash.

I hope you found this answer helpful.

0

I believe you need something like this :

#!/bin/bash
function FN()
{
  filename=$1

  declare -a IPs_ARRAY
  i=0
  user=$2
  password=$3

  while read ip
  do
        IPs_ARRAY[$i]=$ip
        echo ${IPs_ARRAY[$i]}
        # Uncomment for your actions ::
        #curl -k -u $user:$password "https://logservice/jobs" --data-urlencode 'search=search index=test ${IPs_ARRAY[$i]} | head 1' > output.txt
        (( i++ ))

  done < $filename
}

#############
### MAIN ###
###########
read -p "Enter username: " username
read -p "Enter password: " password
# Call your function 
filename="list.txt"
FN $filename $username $password
dimkatsi91
  • 21
  • 10