Operating System - Linux
1755937 Members
4423 Online
108839 Solutions
New Discussion юеВ

Re: diff command without show differences ...

 
SOLVED
Go to solution
Manuales
Super Advisor

diff command without show differences ...

Hi ..
i'm making a script where i'm working with diff sentece, how can i make to not show diferences if there are?
i tryed to send with 2/dev/null but is not working?

NOW and LAST are files:

$ diff NOW LAST 2>/dev/null
1c1
< UUvxfsd root
---
> vxfsd root
Is different

i'd like appears how it looks like next withow diferences ...
$ diff NOW LAST 2>/dev/null
is different

how can i do it ?

Manuales.

7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: diff command without show differences ...

Hi Manuales:

Then do:

# diff NOW LAST > /dev/null || echo "are different!"

Regards!

...JRF...
Manuales
Super Advisor

Re: diff command without show differences ...

Hi James !!!
i tryied with 1>/dev/null instead of 2/dev/null and worked !!!

:D :D :D :D :D

thanks !!!
Rodney Hills
Honored Contributor

Re: diff command without show differences ...

Instead of "diff", use "cmp -s". This will work faster and less resources.

Example-
if cmp -s NOW LAST ; then
echo files are same
else
echo files are different
fi

HTH

Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: diff command without show differences ...

Hi (again) Manuales:

We could amend things thusly, too:

# diff NOW LAST > /dev/null && echo "are same" || echo "are different"

# cmp -s > /dev/null && echo "are same" || echo "are different"

Regards!

...JRF...
Manuales
Super Advisor

Re: diff command without show differences ...

Hi James and Rodni (and another too ...)

i know that diff with if only work with files :
"if diff NOW LAST
then ..."
but, how can i compare strings instead of files?

this is because in my script i'm redirecting string to a file, and for to make more short my script, how can i compare strings??

tempo2 file contains: process (ps -fea and user process, check only there are two fields separated by an space) tempo2 file was generated from a function and is a file:

.......................
.......................
.......................
/etc/opt/resmon/lbin/p_client root
/etc/opt/resmon/lbin/registrar root
/opt/dce/sbin/rpcd root
/opt/egb/lbin/egcd root
/opt/hpnp/bin/hpnpd root
/opt/itadm/it_sw/bin/itappssv -si00 -snsiddp -dbITP -t1 itadm
......
.......
.......

next function of script is a function wich verify how much processes are running with same name and then put the name of the user:
NOW=ACTUAL (ACTUAL in spanish is like now in english)
LAST=ULTIMO (ULTIMO in spanish is like last in english)

function obtiene_total
{
typeset -i x=0 totalproc=0
while read actual
do
if [ "$x" = 0 ]
then
echo $actual > ULTIMO
else
ultimo=`tail -1 ULTIMO`
echo $actual > ACTUAL
if diff ULTIMO ACTUAL 1>/dev/null
then
let " totalproc = $totalproc + 1 "
band=igual
else
case $band in
igual) echo $totalproc $ultimo >> procesos.txt
band=salta ;;
esac
typeset -i totalproc=0
case $band in
diferents) let " totalproc = $totalproc + 1 "
echo $totalproc $ultimo >> procesos.txt ;;
esac
band=diferents
fi
fi
echo $actual > ULTIMO
let " x = x + 1 "
done < tempo2
rm ACTUAL ULTIMO
}

The result will be:
..........
..........
16 /usr/sbin/biod 16 root
1 /usr/sbin/biod 16 root
1 /usr/sbin/cron root
1 /usr/sbin/envd root
1 /usr/sbin/getty console console root
1 /usr/sbin/guardAgntd root
1 /usr/sbin/hp_unixagt root
1 /usr/sbin/inetd root
1 /usr/sbin/lpsched lp
1 /usr/sbin/mib2agt root
1 /usr/sbin/netfmt -C -F -f /var/adm/nettl.LOG00 -c /var/adm/c root
20 /usr/sbin/nfsd 20 root
1 /usr/sbin/nfsd 20 root
1 /usr/sbin/ptydaemon root
1 /usr/sbin/pwgrd root
...................
...................
...................
...................

first field is number of same process and second field is the name of the process.

well question is:
how can i compare strings with diff and cpm?

Thanks !!!
James R. Ferguson
Acclaimed Contributor

Re: diff command without show differences ...

Hi Manuales:

Since you are dealing with ASCII data, redirect your output to a file and, again, use 'diff' but this time add some other options to ignore trailing blanks ('-b'); ignore whitespace and tabs during equality checking ('-w'); and ignore upper/lowercase differences ('-i'):

# diff -bwi file1 file2

By the way, I should have written:

# cmp -s NOW LAST && echo "are same" || echo "are different"

...in my last post. The '-s' of 'cmp' as Rodney used is designed for "silence" --- no output; hence no need to redirect anything to '/dev/null'/

Regards!

...JRF...
OldSchool
Honored Contributor

Re: diff command without show differences ...

Manuales

Does this give the desired result:

UNIX95= ps -A -oargs= -ouser= | sort | uniq -c | pg