简体   繁体   中英

psql shell command execution with \!

Is there any way to capture shell output (in a psql variable) and/or the exit code of running a shell command from inside psql using \\! ? The documentation provides no details.

Seems to be impossible for \\! ,

but one can set a variable from external command output,

testdb=> \set content `cat my_file.txt`

and then using this var in sql expression like this:

testdb=> INSERT INTO my_table VALUES (:'content');

您可以使用\\o <filename>选项指定输出文件或使用COPY 命令来解决您的问题。

Using \\! with \\o ..

You can combine the two like this,

  • \\! echo "SELECT 1;" > bar \\! echo "SELECT 1;" > bar (runs the command echo "SELECT 1;" redirects output of SELECT 1; to bar )
  • \\o bar (runs the commands in bar )

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