David Parks
David Parks

Reputation: 32071

BASH: How to send params & data to a process on STDIN

I'm scripting a call to curl, you can enter the password & parameters via STDIN (keep password off the cmd line).

I also need to send POST data on STDIN (large amount of data that won't fit on the cmd line).

So, from a command line I can successfully do this by:

> curl -K --data-binary @- -other_non-pw_params
> -u "username:password"
> <User types ctrl-d>
> lots_of_post_data
> lots_of_post_data
> <User types ctrl-d>
> <User types ctrl-d>

Now... I'm trying to do that in a BASH script...

Wishful-thinking Psudo-code:

{ echo '-u "username:password"'
  echo <ctrl-d>    |   cat dev/null   |   ^D
  echo lots_of_post_data
  echo lots_of_post_data
} | curl -K --data-binary @- -other_non-pw_params

Upvotes: 3

Views: 2855

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328594

Use a "here document":

curl --config - <<EOF
--basic
...
EOF

Upvotes: 2

David Parks
David Parks

Reputation: 32071

Aha! There's a curl specific solution to this.

You pass all of the parameters on STDIN, and leave --data-binary @- (or it's equivalent) to the end, then everything after it is accepted as data input. Example script:

#!/bin/bash
{ echo '--basic'
  echo '--compress'
  echo '--url "https://your_website"'
  echo '-u "username:password"'
  echo '--data-binary @-'
  echo 'lots_of_post_data'
  echo 'lots_of_post_data'
} | curl --config -

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798616

There is no way to simulate a EOF as in Ctrl-D in the terminal save to stop sending data to the stream altogether. You will need to find a different way of doing this, perhaps by writing a script in a more capable language.

Upvotes: 0

Related Questions