如何在命令行上将列表传递给 psql?

如何在命令行上将列表传递给 psql?

我想知道是否有办法这样做:

lab-2:/tmp# echo  46692,46694,46696,46704,46708,46709,46713,46730,46736,46574 > deletelist.txt
lab-2:/tmp# cat deletelist.txt
46692,46694,46696,46704,46708,46709,46713,46730,46736,46574

然后我想通过 psql 将列表传递到删除命令中。到目前为止,这是我尝试过的,但正如你所看到的,它失败了:

   lab-2:/tmp# psql -U test testdb -c "Delete from widgets where id in (" <deletelist.txt ")"
    psql: warning: extra command-line argument ")" ignored
    ERROR:  syntax error at end of input
    LINE 1: Delete from mist_cdr where id in (
                                          ^

编辑1

尝试建议的答案会得到以下结果:

lab-2:/tmp# psql -U test testdb -c "delete from widgets where id in ( $(</tmp/deletelist.txt ))"
ERROR:  syntax error at or near ")"
LINE 1: delete from widgets where id in ( )
                                           ^
lab-2:/tmp#

还尝试了不带路径的相同命令,如下所示:

lab-2:/tmp# psql -U test testdb -c "delete from widgets where id in ( $(<deletelist.txt ))"

但结果相同

答案1

尝试在命令替换中使用。

psql -U test testdb -c "Delete from widgets where id in ( $(<deletelist.txt) )"

相关内容