1

As part of my bash shell script I am running the following code:

sql -umyuser mydb <<-EOSQL 
MODIFY $MYTABLECLEAR TO TRUNCATED;\g
EOSQL

This works perfectly, but I get output in the terminal window, that I would like to avoid. Normally I would simply add a >/dev/null statement to the end of a command that I wanted to suppress output on, however I can't do this without screwing up the EOSQL text block.

I have tried putting the following:

`sql -umyuser mydb <<-EOSQL 
MODIFY $MYTABLECLEAR TO TRUNCATED;\g
EOSQL` >/dev/null

But here I get a message in the terminal window saying TERMINAL: command not found.

Interestingly the sql command still runs despite this message.

Any help would be much appreciated.

Thanks,

Paul.

2
  • 1
    Have you tried enclosing it with parenthesis? (sql ... ) &> /dev/null Commented Jan 16, 2018 at 13:12
  • 3
    Your attempted workaround tries to run the output from sql as a shell command. Whatever or whoever convinced you to use that particular syntax should probably be abandoned as a source for shell scripting advice. Commented Jan 16, 2018 at 13:43

2 Answers 2

7

Just add the output redirection immediately after the here-document redirection; the here document itself doesn't begin until the next line.

sql -umyuser mydb <<-EOSQL > /dev/null
MODIFY $MYTABLECLEAR TO TRUNCATED;\g
EOSQL

As a mnemonic, you might (mis)parse <<-EOSQL as < <-EOSQL, in which case a filename starting with < or <- indicates an "in-script" file for redirection instead of a regular named disk file. Understand, though, that << and <<- really are separate operators from <. Either way, the here-doc operator can be mixed with the other redirection operators as desired as the comments are pointing out.

Sign up to request clarification or add additional context in comments.

3 Comments

Might also be worthwhile to mention that it doesn't matter in what order redirections are provided. sql -umyuser mydb >/dev/null <<-EOSQL should work just as well, and might reduce confusion.
If you need to suppress error output as well as standard output, you can use <<-EOSQL >/dev/null 2>&1. Note that >/dev/null must come before 2>&1, but <<-EOSQL can be anywhere in the sequence.
@sjsam With the switch to {...}, I have no problem with yours remaining the accepted answer. Mine might be less correct, but your looks less "surprising", even to me :)
3

As mentioned in this comment, you may use the curly braces to avoid the wastage that occurred in the previous answers.

{sql -umyuser mydb <<-EOSQL 
MODIFY $MYTABLECLEAR TO TRUNCATED;\g
EOSQL
} &>/dev/null
# Well, this is a bit neat too, isn't it? 

Looks all good now :-) courtesy @ghoti

4 Comments

The first unnecessarily buffers the output in memory; the second unnecessarily forks a new process.
Ah, sjsam, you've fallen into the XyProblem trap! :-) You've answered "how do I make this workaround work" rather than "how do I fix my underlying problem"! Kudos for pointing to the other answer.
@ghoti : Indeed, seeing the heredoc, i never thought it would be possible to use the redirection operator anywhere after that. But as you pointed out in the other answer, it could've been used before the heredoc at least. Aww. Oh my ;-)
Actually, you could just use curly braces instead of parentheses. Check the man page for your shell, but at least on my systems, redirections work on command lists surrounded by { .. }, which are executed in the current shell rather than a subshell, thus avoiding the "wastage". (The POSIX docs aren't as explicit as the man pages on the systems I use.) See tests here.

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.