0

I got values in a file that needs to read and added to a command. This is my failing solution...

for arrayValue in "${fileArray[@]}"
do
    curl -s --user "${arrayValue[0]}:${arrayValue[1]}" "https://dyndns.loopia.se?hostname=$arrayValue
done

The file is like this:

username
password
domain
domain
...

Reading from the file is no problem, the problem is how to add them to the curl command.

4
  • You are filling fileArray=( < yourfile )? Show the output of declare -p fileArray. (before the loop). And, what is the end your curl command, e.g. "https://dyndns.loopia.se?hostname=$arrayValue..."? Commented Feb 13, 2019 at 7:20
  • Is the user name and the password in the same array as the other data? Commented Feb 13, 2019 at 7:22
  • I didn't ask about reading the file, I asked (1) "How are you filling your array?" and (2) "What is the content of your array immediately prior to your loop?" Commented Feb 13, 2019 at 7:24
  • curl -s --user "${arrayValue[0]}:${arrayValue[1]}" should be curl -s --user "${fileArray[0]}:${fileArray[1]}" Commented Feb 13, 2019 at 7:25

2 Answers 2

4

If your array contains the user name and password in the first two elements, and you want to avoid looping over those, try

for arrayValue in "${fileArray[@]:2}"
do
    curl -s --user "${fileArray[0]}:${fileArray[1]}" "https://dyndns.loopia.se?hostname=$arrayValue"
done

Notice how arrayValue refers to the current element out of fileArray. It is not an array, so the references to ${arrayValue[0]} and ${arrayValue[1]} in your attempt were not valid.

Also notice how we loop over the array elements starting from the third with the :2 suffix. (Array indexing is zero-based, so the index 2 refers to the third element.)

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

Comments

0

Assuming there is no blank character (space, CR...) in the hostname, you could also get rid of the for loop by using printf:

printf -v url "https://dyndns.loopia.se?hostname=${fileArray[@]:2}"

Note the :2 that gets all array elements except the 2 first ones.

Since curl accepts several URL, you can simple use:

curl -s --user "${fileArray[0]}:${fileArray[1]}" $url

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.