Bash 脚本使用 FreeTDS 建立连接,交互,不退出(只是挂起)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5445374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:40:56  来源:igfitidea点击:

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

bashfreetds

提问by etangman

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 Expectotherwise. Meh. Thanks.

我在脚本中使用 FreeTDS 将记录插入到 MSSQL 数据库中。该USEINSERT命令的工作,但该exit命令没有和它挂起。我试过重定向stdoutcat抱怨。我想否则我会使用Expect。嗯。谢谢。

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

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

采纳答案by Mikel

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.

你的意思是这样做:cat tempfile -?这意味着它将等待您按Ctrl+ D,因为它也在尝试从标准输入中读取。

If not, remove the -.

如果没有,请删除-.

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

此外,正如 Ignacio 所建议的,您可以将其写为更简洁的 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:

或者只是用文字换行符而不是\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