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

5

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

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.