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
Advertisement
Answer
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.