Score:0

How can I make a bash script that reads a line after every http request

ng flag

I have 700 lines (string) and I was wondering how I can make a bash script that sends an HTTP request using curl when it runs the first time such that it will send the first request with the first line, then when it sends the second request it will use the second line, 3rd request, it will use the 3rd line, ..., 700th request will use the 700th line then repeat.

Also what do you think is better? Using a .csv file to read the strings from, for example:

while IFS=, read -r example
do
   ...
done < filename.csv

Or using a for-loop inside the script, for example:

arr=( "string1" "string2" "string3" .."string700" )
for i in "${arr[@]}"
do
  echo $i
done
Tilman avatar
cn flag
Please clarify what your file looks like. Is it one URL per line as your text states, or is it comma separated as your script snippet seems to imply.
waltinator avatar
it flag
Ubuntu 14.04 has passed itself End-of-life date, and is no longer supported on AskUbuntu.
in flag
Why is PHP tagged? This question seems to want a bash script rather than a PHP script
Abdalaziz Alharthi avatar
ng flag
@Tilman it is user-agents list:https://gist.github.com/pzb/b4b6f57144aea7827ae4 but I am going to save the file with csv extension , Yes it is one URL.
Abdalaziz Alharthi avatar
ng flag
@bac0n so I should first use : while IFS=, read -r example do FileName="2021-03-13-data_export.csv" Lines=$(cat $FileName)
Tilman avatar
cn flag
So the text file does not contain URLs at all, but User-Agent strings. And it is not comma-separated at all even though you give it the `.csv` extension. `IFS=,` does not make sense if the file isn't really comma-separated. But I see you got a valid answer, so I'll stop here.
Abdalaziz Alharthi avatar
ng flag
Many thanks @Tilman
Score:2
cn flag

If you want to loop the list supplied in the comment, a newline-separated file will work fine as user-agents will not contain any newlines.

#!/bin/bash

mapfile -t < user-agents.txt
while :; do
    for agent in "${MAPFILE[@]}"; do
        curl -A "$agent" http://localhost/
        sleep 0.2
    done
done
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.