Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

freetds

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

like image 442
etangman Avatar asked Jul 17 '26 14:07

etangman


1 Answers

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
like image 137
Mikel Avatar answered Jul 19 '26 10:07

Mikel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!