为什么 vagrant ssh -c 忽略命令中的单引号?
Why does vagrant ssh -c ignore single quotes in command?
我想用主机的 IP 更新我的数据库 运行 中的一列(使用 Vagrant
)。
为此,我想使用 bash 脚本:
LOCAL_IP=$(ipconfig getifaddr en0)
SQL="vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c \"update mytable set mycolumn = '$LOCAL_IP';\"'"
eval $SQL
但是我得到这个错误:
ERROR: syntax error at or near ".123"
LINE 1: update mytable set mycolumn = 123.123.123.123;
SQL变量的输出:
echo $SQL
vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c "update mytable set mycolumn = '123.123.123.123';"'
当我在 vm 中从 echo $SQL
调用 psql 命令时一切正常,但我没有主机 ip。
vagrant ssh
psql -U user -d mydatabase -h localhost -c "update mytable set mycolumn = '123.123.123.123';"
看起来 vagrant ssh -c
命令会删除 IP 周围的单引号。有什么想法吗?
更新
重现我的问题的最简单方法:
$ vagrant ssh -c 'echo "update table set column = 'test';" > bla.sql'
$ vagrant ssh
$ cat bla.sql
update table set column = test;
我在这里找到了解决方案:
现在看起来像这样:
SQL="vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c \"update mytable set mycolumn = '\''$LOCAL_IP'\'';\"'"
我想用主机的 IP 更新我的数据库 运行 中的一列(使用 Vagrant
)。
为此,我想使用 bash 脚本:
LOCAL_IP=$(ipconfig getifaddr en0)
SQL="vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c \"update mytable set mycolumn = '$LOCAL_IP';\"'"
eval $SQL
但是我得到这个错误:
ERROR: syntax error at or near ".123"
LINE 1: update mytable set mycolumn = 123.123.123.123;
SQL变量的输出:
echo $SQL
vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c "update mytable set mycolumn = '123.123.123.123';"'
当我在 vm 中从 echo $SQL
调用 psql 命令时一切正常,但我没有主机 ip。
vagrant ssh
psql -U user -d mydatabase -h localhost -c "update mytable set mycolumn = '123.123.123.123';"
看起来 vagrant ssh -c
命令会删除 IP 周围的单引号。有什么想法吗?
更新
重现我的问题的最简单方法:
$ vagrant ssh -c 'echo "update table set column = 'test';" > bla.sql'
$ vagrant ssh
$ cat bla.sql
update table set column = test;
我在这里找到了解决方案:
现在看起来像这样:
SQL="vagrant ssh -c 'psql -U user -d mydatabase -h localhost -c \"update mytable set mycolumn = '\''$LOCAL_IP'\'';\"'"