I have a bash script that invokes psql and executes SQL code:
sudo -i -u postgres psql <<EOF
SQL COMMANDS;
EOF
The SQL part generates a lot of output and I'd like to redirect it to /dev/null. How does one redirect a heredoc to /dev/null ?
I have a bash script that invokes psql and executes SQL code:
sudo -i -u postgres psql <<EOF
SQL COMMANDS;
EOF
The SQL part generates a lot of output and I'd like to redirect it to /dev/null. How does one redirect a heredoc to /dev/null ?
Trivially
postgres psql <<EOF >/dev/null
SQL COMMANDS;
EOF
The <<
token heredoc delimiter is just another redirection operator; so you can do stuff like
postgres psql <<EOF 2>/dev/null |
SQL COMMANDS;
EOF
while IFS= read -r output; do
case $output in
*error*) printf '%s%s%s\n' "$red" "$output" "$plain";;
*) echo "$output";;
esac
done
where the pipeline which starts on the first line continues after the EOF
token. (This requires you to have defined screen control codes for red and uncolored text separately; man tput
.)