Suppressing errors messages that might occur when using cat command with EOF or EOD in bash script
You need to use redirection to /dev/null
like this:
function create()
{
cat 2>/dev/null << EOF > hello.txt/
Hello
EOF
}
You can also redirect all the errors from the create()
function:
function create()
{
cat << EOF > hello.txt/
Hello
EOF
}
createHook 2>/dev/null
How can I suppress error messages of a command?
Most Unix commands, including ls
, will write regular output to standard output and error messages to standard error, so you can use Bash redirection to throw away the error messages while leaving the regular output in place:
ls *.zip 2> /dev/null
Avoid error with cat command when src file doesn't exist
Test for file1
first.
[ -r file1 ] && cat ...
See help test
for details.
How to hide cat errors?
You need to redirect the stderr stream of the cat
command to /dev/null
. What you have done is redirected the stderr stream of wc
. Try this:
( find -print0 | xargs -0 cat 2>/dev/null ) | wc -l
Suppress error messages in Windows commandline
Redirect the output to nul
mkdir "C:\users\charqus\desktop\MyFolder" > nul
Depending on the command, you may also need to redirect errors too:
mkdir "C:\users\charqus\desktop\MyFolder" > nul 2> nul
Microsoft describes the options here, which is useful reading.
Bash ignoring error for a particular command
The solution:
particular_script || true
Example:
$ cat /tmp/1.sh
particular_script()
{
false
}
set -e
echo one
particular_script || true
echo two
particular_script
echo three
$ bash /tmp/1.sh
one
two
three
will be never printed.
Also, I want to add that when pipefail
is on,
it is enough for shell to think that the entire pipe has non-zero exit code
when one of commands in the pipe has non-zero exit code (with pipefail
off it must the last one).
$ set -o pipefail
$ false | true ; echo $?
1
$ set +o pipefail
$ false | true ; echo $?
0
How do I suppress shell script error messages?
You have two options:
Suppress rm
warnings
$ rm tempfl.txt 2> /dev/null
Redirect script output to /dev/null
$ ./myscript.sh 2> /dev/null
The latter has a drawback of missing all other warning messages produced by your script.
Read file into variable while suppressing No such file or directory error
Redirection applies to the output of a command line. When the cat
command produces an error, you can redirect the stderr as you've done.
But when you generate the error within the command line itself, through input redirection from a file that doesn't exist, THAT shell is the one producing the error, and you're not redirecting its output to anywhere special. You've successfully discovered the "ugly and cumbersome" workaround, which mimics a subshell using curly braces.
Personally, I'd use an if
construct, but if you really prefer shorter code over fewer calls to external programs, this is a perfectly valid use of cat
.
Related Topics
How to Add External References in Monodevelop
Host Multiple ASP.NET Core Web Application Under a Single Linux Server
How to Run a Bash Function() in a Remote Host? in Ubuntu
Behavior of Writing to Tcp Socket Before Reading All Data
Running a Program Through Ssh Fails with "Error Opening Terminal: Unknown."
Run Hydra (Mpiexec) Locally Gives Strange Ssh Error
Bluetooth Over Uart Using Hciattach
Is There Some Ansible Equivalent to "Failed_When" for Success
Ftrace: System Crash When Changing Current_Tracer from Function_Graph via Echo
Launching Sonar Scanner from a Gitlab Docker Runner
Phony Targets for Parallel Execution of Make
Debian Sources.List.D Versus Sources.List
Can't Run Dmidecode on Docker Container
Error While Building Pjsip in Linux
Cmake: How to Suppress "Entering Directory" Messages