BASH:语法错误附近意外的令牌`('。在.bashrc中

时间:2015-12-07 19:13:20

标签: bash postgresql

我想获得与postgresql DB的当前连接数。因此我正在使用这个脚本:

su - postgres -c "psql -c 'SELECT sum(numbackends) FROM pg_stat_database;'"

这就像一个魅力。 我的下一步是修改我的.bashrc以使用命令cntcon来显示连接数:

alias cntcon='su - postgres -c "psql -c 'SELECT sum(numbackends) FROM pg_stat_database;'"'

只要我想重新加载.bashrc,我就会发现以下错误:

-bash: /root/.bashrc: line 16: syntax error near unexpected token `('
-bash: /root/.bashrc: line 16: `alias cntcon='su - postgres -c "psql -c $'SELECT sum(numbackends) FROM pg_stat_database;$'"''

我已经在stackoverflow上找到了类似问题的以下解决方案:

-bash: syntax error near unexpected token `)'

然后我收到了这个错误:

-bash: alias: -c: not found
-bash: alias: SELECT sum(numbackends) FROM pg_stat_database;: not found

我也尝试过:

echo "alias cntcon='su - postgres -c "psql -c 'SELECT sum(numbackends) FROM pg_stat_database;'"'" | tee -a .bashrc.
alias cntcon='su - postgres -c psql -c SELECT sum(numbackends) FROM pg_stat_database;'

但是出现了以下错误:

-bash: alias: sum(numbackends): not found
-bash: alias: FROM: not found
-bash: alias: pg_stat_database: not found
-bash: ": command not found

我认为脚本中的几个转义存在问题,而且我还假设我犯了一个简单的错误。

提前谢谢!

0 个答案:

没有答案