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

4
  • What's wrong with using a heredoc? Commented Mar 26, 2011 at 21:16
  • Like this? It's exiting, but not inserting the record. tsql -H 10.10.10.10 -p 1433 -U user -P pass <<< tempfile Perhaps too many commands too quickly? Commented Mar 26, 2011 at 21:24
  • No, a heredoc is two less thans. <<< is a here string, which is something different. Commented Mar 26, 2011 at 21:46
  • And in that case, what you really want is just < tempfile, which is standard input redirection. Commented Mar 26, 2011 at 21:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.