user2522055 user2522055 - 1 year ago 101
Linux Question

Add a delay while curl is downloading python script and then pipe to execute script?

I just created a rickroll prank to play on friends and family. I want to be able to download the file from github using a curl command which works. My issue is that when I use a pipe and try to execute the script it does it right after curl gets executed and before it downloads the file.

This is the command I am trying to run:

curl -L -O | python

I have tried to run it using the sleep command as well, but haven't had any luck.

(curl -L -O; sleep 10) | python

Answer Source

Expanding on my comment.

There are several ways to chain commands using most shell languages (here I assume sh / bash dialect).

  • The most basic: ; will just run each command sequentially, starting the next one as the previous one completes.
  • Conditional chaining, && works as ; but aborts the chain as soon as a command returns an error (any non-0 return code).
  • Conditional chaining, || works as && but aborts the chain as soon as a command succeeds (returns 0).

What you tried to do here is neither of those, it's piping. Triggered by |, it causes commands on its sides to be run at once, with the standard output of the left-hand one being fed into the standard input of the right-hand one.

Your second example doesn't work either, because it causes two sequences to be run in parallel:

  • First sequence is the curl, followed by a sleep once it finishes.
  • Second sequence is the python command, run simultaneously with anything written by the first sequence redirected as its input.

So fix it: command1 && command2, will run curl, wait for it to complete, and only run python if curl succeeded.

And again, you can use your example to show how harmful it can be to run commands one doesn't fully understand. Have your script write “All your files have been deleted” in red, it can be good for educating people on that subject.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download