N
N
NO_GLITCH2015-10-28 19:50:16
linux
NO_GLITCH, 2015-10-28 19:50:16

BASH How to create a dynamic variable?

Example
Using curl, a request is sent to the server, the title is extracted from the response and written to a file.
The list of URLs is taken from the file
Part of the script:

while read -r line
  do
  count_progress=$((++count_progress))
  export tmp_var1=$(curl -s $line/ | grep '<title>' )
  if [ "$tmp_var1" == "String" ]; then echo $line >> save_as; fi
        printf "$count_progress checked\r"
 done < $input

I would like not to wait for the completion of the previous curl and run them at the same time.
Non-working example with & added
while read -r line
  do
  count_progress=$((++count_progress))
  export tmp_var1=$(curl -s $line/ | grep '<title>' )&
  if [ "$tmp_var1" == "String" ]; then echo $line >> save_as; fi&
        printf "$count_progress checked\r"
 done < $input

It seems to me that a parallel launch can be done if each time you set different variables in the place where tmp_var1 is indicated, because each time this value is interrupted by a new request that has not yet received a response.
The question is exactly how to substitute a new variable each time, for example, based on a line from a file
echo $line | awk '$0="tmp_"$0'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vlad Zhivotnev, 2015-10-28
@inkvizitor68sl

man gnu-parallel

J
jcmvbkbc, 2015-10-28
@jcmvbkbc

This should work, but the idea of ​​running an uncontrolled number of parallel processes is also not particularly good:

while read -r line
  do
  count_progress=$((++count_progress))
  (
  export tmp_var1=$(curl -s $line/ | grep '<title>' )
  if [ "$tmp_var1" == "String" ]; then echo $line >> save_as; fi
        printf "$count_progress checked\r"
  ) &
 done < $input

A
Alexey Shumkin, 2015-10-29
@ashumkin

Vlad Zhivotnev has already given the correct answer :)
You have taken an inefficient path and are trying to make inefficient decisions instead of taking the right path ( parallel)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question