Operating System - HP-UX
1846563 Members
1702 Online
110256 Solutions
New Discussion

Remove errors at command line

 
SOLVED
Go to solution
Vanquish
Occasional Advisor

Remove errors at command line

i am doing a diff between 2 files if the file does not exists it gives error message on command line i want to remove it
diff: No such file or directory
Thanks
6 REPLIES 6
Marvin Strong
Honored Contributor
Solution

Re: Remove errors at command line

add 2>/dev/null to the end of your diff command.

example

diff file1 file2 2>/dev/null

Juergen Tappe
Valued Contributor

Re: Remove errors at command line

Easy answer :
diff file1 file2 2>/dev/null

and the error messages going to "trashcan".

Juergen
Working together
A. Clay Stephenson
Acclaimed Contributor

Re: Remove errors at command line

That error message goers to stderr (like good UNIX programs should) while the normal output goes to stdout. The solution is simple:

diff file1 file2 2>/dev/null
If it ain't broke, I can fix that.
Hazem Mahmoud_3
Respected Contributor

Re: Remove errors at command line

# diff file1 file2 2 > /dev/null

-Hazem
A. Clay Stephenson
Acclaimed Contributor

Re: Remove errors at command line

I should add that there is a danger to ignoring the error message. It now is not possible to distinguish between two files which match exactly (ie no diff) and a missing file. Both cases will result in no output to stdout. You need to now check the status of the diff command as well as examine stdout. Diff will return a non-zero exit status on failure.

e.g.
diff file1 file2 2>/dev/null > difffile
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Diff failed. Status = ${STAT}"
fi
If it ain't broke, I can fix that.
Rory R Hammond
Trusted Contributor

Re: Remove errors at command line


From a Shell Script:

if
[ -f file1 -a -f file2 ]
then
diff file1 file2
fi

Suggest if you want from a command line:
1. make a function in .profile

difff()
{
if
[ -f $1 -a -f $2 ]
then
diff $1 $2
fi
}

2. command line
difff file1 file2

There are a 100 ways to do things and 97 of them are right