Bash script makes connection using FreeTDS, interacts, doesn't exit (just hangs)

£可爱£侵袭症+ 提交于 2020-01-04 01:57:05

问题


I'm using FreeTDS in a script to insert records into a MSSQL database. TheUSEandINSERTcommands work, but theexitcommand doesn't and it hangs. I've tried redirectingstdoutbutcatcomplains. I suppose I will use Expect otherwise. Meh. Thanks.

echo -e "USE db\nGO\nINSERT INTO db_table (id, data, meta)\nVALUES (1, 'data', 'meta')\nGO\nexit" > tempfile cat tempfile - | tsql -H 10.10.10.10 -p 1433 -U user -P pass


回答1:


Did you mean to do this: cat tempfile -? It means that it will wait for you to press Ctrl+D, because it is trying to read from standard input as well.

If not, remove the -.

Also, as Ignacio suggests, you could write it more cleanly as a heredoc:

tsql -H 10.10.10.10 -p 1433 -U user -P pass <<EOF
USE db
GO
INSERT INTO db_table (id, data, meta)
VALUES (1, 'data', 'meta')
GO
exit
EOF

Or just do the echo with literal newlines rather than \n:

echo "
USE db
GO
INSERT INTO db_table (id, data, meta)
VALUES (1, 'data', 'meta')
GO
exit
" > tempfile

and then run it by using standard input redirection (<) like this:

tsql -H 10.10.10.10 -p 1433 -U user -P pass < tempfile


来源:https://stackoverflow.com/questions/5445374/bash-script-makes-connection-using-freetds-interacts-doesnt-exit-just-hangs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!