开发者

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
&g开发者_运维问答t; <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


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 -


Use a "here document":

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜