3

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

etangman
  • 45
  • 1
  • 8

1 Answers1

2

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
Mikel
  • 24,855
  • 8
  • 65
  • 66
  • Awesome. No, I didn't mean to leave the `cat tempfile -` it was in there from something else I was trying. The EOF is much cleaner though. Thanks again. – etangman Mar 27 '11 at 11:08