尝试创建shell函数以从命令行转储查找表。这是我的基本命令(标签已更改为隐藏标识):
me@mypc:~$ psql -h psql.example.com -U me mydb -c "SELECT usertype_id,name,slug FROM tbl_usertype ORDER BY name"
我认为这将是相应的shell函数:
me@mypc:~$ function slugs() { psql -h psql.example.com -U me mydb -c "SELECT $1_id,name,slug FROM tbl_$1 ORDER BY name"; }
然而,我的函数显然把我的参数放在命令的末尾,而不是$ 1代币:
me@mypc:~$ slugs usertype
psql: warning: extra command-line argument "usertype" ignored
ERROR: relation "tbl_" does not exist
LINE 1: SELECT _id,name,slug FROM tbl_ ORDER BY name;
如何正确编码?
答案 0 :(得分:0)
您的功能在我的系统上运行得非常好。
$ function slugs() { psql -U craig regress -c "SELECT $1_id,name,slug FROM tbl_$1 ORDER BY name"; }
$ slugs 1
ERROR: relation "tbl_1" does not exist
LINE 1: SELECT 1_id,name,slug FROM tbl_1 ORDER BY name
使用:
bash --version
GNU bash, version 4.2.45(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
所以要么你的旧版bash版本中有本地错误,要么更有可能真正的问题被函数的匿名隐藏。