How to ignore No such file or directory on the output?

Hello -
I have a script which looks for filesystem usage and its a common script for all servers I have . On few servers, we do not have a FS available and getting the output as

No such file or directory 

, which is expected. I am not in favor of having an if condition to have list of servers added and verify for that specific FS only on those servers.

wondering if there is a way I can ignore this error to show-up on output itself when someone run that script?
Noe - I write the output of the script to a temp txt file and use it for different purpose.

Hi
Try closing the stderr descriptor at the beginning of the script.

exec 2>&-

or redirect

exec 2> /dev/null
3 Likes

This helped. Thank you .

That globally suppresses all error messages.
You can suppress them for specific commands only, for example

df -kP 2> /dev/null

Suppress the expected.
If unexpected things happen then error messages are helpful, otherwise you might get wrong output silently.

Today I am using on that specific line , instead of whole script executing. But

this line making me to think , if for some reason that FS is not mounted during patching then I am missing that important message . Any way I can avoid ? I guess answer would be NO.

You could redirect error messages to a (log-)file, for a later review, or to parse for known issues after the 'main' commands.
So instead of >/dev/null you would be using >>/path/to/logfile .

Hope this helps

Try appending this to your command:

2> >(grep -v 'No such file or directory' >&2)

It should remove the above message from the STDERR but allow other messages to be printed.

Works for bash and I believe ksh, but not sh or dash.

Andrew

2 Likes

Hi @apmcd47

|& grep -v 'No such file or directory'

same thing only in left hand

I'm sorry, not the same thing. Standard output also falls under this filter.

The following "descriptor magic" should work with all shells

{
# Descriptor 2 goes where descriptor 1 currently goes to (the pipe!), then descriptor 1 goes to  where descriptor 3 currently goes to (set at the end of the braces)
# In short: 2 becomes a dup of 1 then 1 becomes a dup of 3
ls -ld / /notthere 2>&1 1>&3 | grep -v "No such file or directory"
# Restore to the original descriptors:
# 3 becomes a dup of 1 then 1 becomes a dup of 2 
} 3>&1 1>&2

The redirections of the ( code block } happens before the code block is run.

1 Like