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: --
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
  • 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 – Arount Dec 20 '17 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. – Barmar Dec 20 '17 at 17:03

2 Answers2

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
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 3
    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`. – Barmar Dec 20 '17 at 17:05
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