简体   繁体   中英

Linux print file and use it in for example a curl request

This might be a complete noob question, but I would like to know a simple oneliner to solve my problem.

Imagine following: I have 5 files, could be .txt, .sql .anything.

root@DESKTOP:/$ ls
test.sql test1.sql test2.sql test3.sql

Imagine I would like to use for example test.sql in a curl request as part of a json parameter.

root@DESKTOP:/$ cat test.sql | 
curl -H "Accept: application/json" 
-H "Content-type: application/json" 
-X POST -d "{ \"sqlcommand\" :\"??????" }" 
http://localhost:9999/api/testsql

How can I put the output of the cat command in this request at the place of the questionmark? $0, $1 etc are not sufficient. I know how to do it with a for loop. And I could write a shell script that takes an input parameter that I could paste in the command. But. I would like to do it in a simple oneliner and moreover I'd like to learn how I can get the output of the previous command when I need to use it combined with other data OR when it is bad practice i'd like to know the best practice.

Thank you in advance!

This should do what you need:

curl -X POST -d "{ \"sqlcommand\" :\"$(cat test.sql)\" }" 

$(cmd) substitutes the result of cmd as a string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM