0

I am using Ubuntu (Precise 12.04) I am trying to write some bash script to see if file is changed via awk replace by using diff command.

I did a test with the following:

#! /bin/bash

diff fileA.htm fileB.htm &> /dev/null
if [ $? -ne 0 ]; then
  echo "changed";
else
  echo "not changed";
fi

It works, so I am expanding the script like below:

#! /bin/bash

find . \( -name '*.htm' -o -name '*.html' \) \
-exec sh -c \
"awk '/START_TAG/{f=1;print;while (getline < \"REPLACEMENT_FILE.tmp\"){print}}/END_TAG/{f=0}!f' \"{}\" > \"{}.awk\"; diff \"{}\" \"{}.awk\" &> /dev/null; \
if [ $? -ne 0 ]; then\
  echo \"{} changed\"; \
else \
  echo \"{} not changed\"; \
fi" \;

The awk is working, and the diff command is also working, but I couldn't get the output from diff &> /dev/null and capture in my if statement.

I am assuming when running that within the find -exec statement probably I need to change something there but not sure how to do it.

2
  • I don't know much about awk, but it seems to me that it would be possible to include a bit of code that would output something if the file was changed.
    – BenjiWiebe
    Commented Jul 11, 2014 at 3:30
  • I guess there are two separate issues here, one is if the script is written correctly for the intended purpose, the other being the question I am posting here. Commented Jul 15, 2014 at 8:09

1 Answer 1

0

I think I have found the problem, the fixed version below:

#! /bin/bash

find . \( -name '*.htm' -o -name '*.html' \) \
-exec sh -c \
$'awk \'/START_TAG/{f=1;print;while (getline < \"REPLACEMENT_FILE.tmp\"){print}}/END_TAG/{f=0}!f\' \"{}\" > \"{}.awk\"; diff \"{}\" \"{}.awk\" &> /dev/null;\n \
if [ $? -ne 0 ]; then\n \
  echo \"{} changed\";\n \
else\n \
  echo \"{} not changed\";\n \
fi'\n \;

I need to add line breaks for the if else statement

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .