Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Let me give you an idea of what I am doing.

I am running a script that is doing basic OS processes for two systems Linux and SunOS. Well the first directory it runs is called sol0. The script puts the output of sol0 into a temp file.

It looks like this:

-----------------------
proc    1 response time =     67
proc    2 response time =    114
-----------------------
average response time   =     90
completion_time         =    170
total idle time         =     32
percent idle time       =     18%
-----------------------

I am editing the script to make a file in the current directory that stores sol0's output. I want that file to compare to the output of every directory after that.

My cmp command says none of the outputs match even when I made a copy of sol0 with a different name. They both have the same output!

Here is how I am using the command ,but first some details about my script.

My Variables:

STUID=$1
PATH=${PATH}\:.
TMPFILE=tempfile
GDIR=`pwd`
OSNAME=`uname -s`
LOG=${GDIR}/results/${STUID}-${OSNAME}-X.log
DATE=`date`
TT=/dev/tty
file="./"

How the script is calling the function to run:

(  if [ ${OSNAME} = SunOS ] ; then
    ulimit 65
    ./sim >>${TMPFILE} 2>${TMPFILE}2 &

   else
    ulimit 35
    ./sim >>${TMPFILE} 2>${TMPFILE}2 &
   fi
  )  

The cmd command:

if  [ ${STUID} = sol0 ]; then
    if  [ ${OSNAME} = SunOS ]; then 
          echo " Making SunOS compare file. " > ${TT}
          echo > ${GDIR}/compares
          echo " Created file. " > ${TT}
          cp ${TMPFILE} ${GDIR}/compares
    else
        echo " Making Linux compare file. " > ${TT}
    echo > ${GDIR}/comparel
    echo " Created file. " > ${TT}
    cp ${TMPFILE} ${GDIR}/comparel
    fi
  else
    echo " This is not sol0. " > ${TT}
  fi

  if [ ${OSNAME} = Linux ]; then
    if [ ${STUID} != sol0 ]; then
      if [ cmp ${TMPFILE} ${GDIR}/comparel ]; then
        echo "${STUID} Linux output matches the Linux output of sol0." > ${TT}
      else
        echo "${STUID} Linux output does not match the Linux output of sol0." >${TT}
      fi  
   else
      echo "This is sol0, no comparing will be done." >${TT}
   fi
  else
   echo "This is running on SunOS. " >${TT}
  fi
share|improve this question

1 Answer 1

up vote 3 down vote accepted

Looking specifically at your cmp command, replace:

if [ cmp ${TMPFILE} ${GDIR}/comparel ]; then

With:

if cmp "${TMPFILE}" "${GDIR}/comparel"; then

One uses the [ command (also known as test) when one wants to set an exit code according to one of various conditions (a file exists, a string is empty, etc.). Here, you want to set an exit code based on the result of the cmp command and you have no use for test. Therefore, the square brackets need to be removed.

share|improve this answer
    
Thank you for that knowledge, I did not know that. Are there any other reasons why it would not compare? My cmp specifically is still saying it wouldn't match, so I must be doing something wrong still. –  user2419571 2 days ago
1  
@user2419571 Since, as I understand it, your files are text, my first suggestion is try to diff on the files instead of cmp. The advantage of diff is that, if it thinks the files differ, it will show you exactly why it thinks so. –  John1024 2 days ago
    
@Jogn1024 You're awesome, thank you! –  user2419571 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.