1

my code is:

echo 'url' | parallel "if curl -ss "$url" | grep yes; then vara+="$(echo {})"; else varb+="$(echo {})"; fi"

But variables are not being set. If then statement works perfectly eg . if curl $url | grep yes; then echo done; else echo undone; fi ---> such thing works but variables are not being set

Can anyone help me find my mistake.

Desired Output: is if if curl url and grep certain word in source then that url will be appended to a variable using += and if word not found then it will be apended in another variable . Finally after script is over i want to have to variables: vara contains all url list having specific word and varb contains list of urls not having that word in source. that variables are furthet used in scripts

3
  • 1
    They're being set, but in different processes. parallel doesn't provide the sort of interprocess communication you are looking for. Commented Nov 27, 2021 at 18:18
  • 1
    Other problems include: quotes don't nest, so ... "if curl -ss "$url" ... is parsed as a double-quoted string if curl -ss followed by a completely unquoted variable reference, then the start of another double-quoted string. Also, everything like $url and $(echo {}) gets interpreted and turned into fixed strings by the shell before they're passed to the parallel command as arguments. Put the set -x command before this, and you can see how bash interprets this as it runs. Commented Nov 27, 2021 at 18:38
  • What are you actually trying to achieve please? I mean, what is your expected output? Commented Nov 27, 2021 at 21:59

1 Answer 1

1

TLDR; Accummulate all results in a single array, in the calling process, and sort them out afterwards.

For each URL, output either SUCCESS:URL or FAIL:URL

Accumulate that into a single array:

Results=( $(parallel 'status="FAIL"; curl something && status="SUCCESS"; printf "$status:%s" {}') )

which will give Results like:

SUCCESS:www.ibm.com
SUCCESS:www.google.com
FAIL:www.chocolate-teapots.com

Now select/sort the elements using this answer.

Agreed that sorting afterwards is not exactly what you were looking for, but I assume the reason for wanting to do it in parallel was the latency of curl which is still being avoided, and selecting/sorting even thousands of array elements will only take milliseconds.


Explanation

Your script is setting array elements, but within the new process that GNU Parallel starts for each job... and which exits when that job is complete.


You could use GNU Parallel parset like this to fill an array:

parset Results 'sleep 3; seq {}' ::: {1..10}
echo "${Results[@]}"

And you'd set it up with:

env_parallel --install
<<start a new shell>>

but that wouldn't be my choice.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.