4

I want to run the following commands in one line:

$ curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O
$ python ./awslogs-agent-setup.py --region eu-west-2

How can I do this?

I tried the following

$ curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -s | python --region eu-west-2

But I get the error:

Unknown option: --
2
  • you forgot ./awslogs-agent-setup.py after python, but that's not all, it's looks like ./awslogs-agent-setup.py expect to read a specific file on filesystem - so you will have to update it so it can read curl's output directly as argument Commented Dec 20, 2017 at 16:54
  • 1
    @Arount awslogs-agent-setup.py is the file being downloaded, he wants to execute it directly from the curl output rather than put it into a local file to execute it. Commented Dec 20, 2017 at 17:03

2 Answers 2

6

You could let the python interpreter read from stdin using /dev/stdin (similar to python -) and pass the additional arguments alongside.

curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -s |\
     python /dev/stdin --region eu-west-2

As Barmar points out using /dev/stdin could be OS specific in which case using python - would be more standard

curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -s |\
     python - --region eu-west-2

As curl outputs the file content to stdout, you can pipe it over to the standard input of the interpreter.

Or use process-substitution feature in bash (<()), which lets you treat the output of a command as if it were a file to read from

python <(curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -s) --region eu-west-2
Sign up to request clarification or add additional context in comments.

1 Comment

You can use - as the filename. Python follows the standard convention of treating - as a filename to mean standard input, then you're not depending on the OS-specific /dev/stdin.
0

Why not just use &&

curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O && python ./awslogs-agent-setup.py --region eu-west-2

and if you want to do without creating a file just to delete later, do this:

curl -s https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py | python /dev/stdin --region eu-west-2

Execute bash script from URL

2 Comments

I don't want to create an uneeded file just to delete it
Your second solution seems the same as @Inian answer, except it is in more detail and has more options

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.