Bash script makes connection using FreeTDS, interacts, doesn't exit (just hangs)
I'm using FreeTDS in a script to insert records into a MSSQL database. TheUSE
andINSERT
commands work, but theexit
command doesn't and it hangs. I've tried redirectingstdout
butcat
complains. 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 - | ts开发者_运维百科ql -H 10.10.10.10 -p 1433 -U user -P pass
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
精彩评论