-2

I need to make an if statement that:

  • compares the files
  • finds the differences between them
  • gets a word count of both files
  • pastes the information from one file to another

But I do not know if I'm doing it correctly. Here is my code:

If [ "$DECISION" == "a" ] || [ "$DECISION” == "A" ]; then
  Cmp workfile1.txt workfile2.txt  
elif [ "$DECISION" == "b" ] || [ "$DECISION” == "B" ]; then
  diff workfile1.txt workfile2.txt then
  wc workfile1.txt workfile2.txt then
  paste workfile1.txt workfile2.txt
fi
  • 3
    Please don't SHOUT! It is not an emergency... – jasonwryan Dec 12 '14 at 21:17
  • I take it it's supposed to do one of those things, based on $DECISION? – Michael Mrozek Dec 12 '14 at 21:20
  • 4
    FYI: Unix is, in general, case sensitive. The command can't be If, it must be if. Similarly, its cmp, not Cmp. – derobert Dec 12 '14 at 21:28
1

A few things:

  1. As derobert notes, the language and the Unix platform are case-sensitive.
  2. The then keyword is used only with an accompanying if. To simply run on command after another, either:

    • Keep them in different lines:

      diff workfile1.txt workfile2.txt
      wc workfile1.txt workfile2.txt
      
    • Or separate with semicolons:

      diff workfile1.txt workfile2.txt; wc workfile1.txt workfile2.txt
      
  3. The quotes matter. On some things you have used pretty quotes instead of normal ones ("). Don't use a word processor such as MS Word or LibreOffice Writer to edit scripts. Use plain editors (like Notepad++, gedit, etc.) or IDEs.
  4. wc will list word, line and character count if it is not specified which to output. Use -w to restrict to word counts: wc -w workfile1.txt workfile2.txt
  5. I believe you think "comparing two files" and "finding the differences between two files" are two unconnected things. No, they aren't. You can use the same command to do both. Use either cmp or diff. There's no need to use both.
  6. Merging in the differing sections of two files is best handled by patch, and to get information for patch, you can use diff. So skip cmp for this. Relevant reading:

I think you should start with a beginner's guide to scripting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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