How to Store Result of Diff in Linux

How to store result of diff in Linux

The diff utility produces its output on standard output (usually the console). Like any UNIX utility that does this, its output may very simply be redirected into a file like this:

diff A.txt B.txt >C.txt

This means "execute the command diff with two arguments (the files A.txt and B.txt) and put everything that would otherwise be displayed on the console into the file C.txt". Error messages will still go to the console.

To save the output of diff to a file and also send it to the terminal, use tee like so:

diff A.txt B.txt | tee C.txt

tee will duplicate the data to all named files (only C.txt here) and also to standard output (most likely the terminal).

Bash: using the result of a diff in a if statement

ls -lR $dir > a
ls -lR $dir > b

DIFF=$(diff a b)
if [ "$DIFF" != "" ]
then
echo "The directory was modified"
fi

How to output 'passed' if a diff command results in no difference in bash?

Get and check the exit code from the diff command. diff has an exit code of 0 if no differences were found.

diff ...
ret=$?

if [[ $ret -eq 0 ]]; then
echo "passed."
else
echo "failed."
fi

How to redirect output of command to diff

First, to your error, you need to redirect standard error:

diff $t.out <($parser_test `cat $t` 2>&1)

Second, to all the other problems you may not be aware of:

  • don't use ls with a for loop (it has numerous problems, such as unexpected behavior in filenames containing spaces); instead, use: for t in $TESTS_PATH1/cmd*.in; do
  • to support file names with spaces, quote your variable expansion: "$t" instead of $t
  • don't use backquotes; they are deprecated in favor of $(command)
  • don't use a subshell to cat one file; instead, just run: $parser_test <$t
  • use either [[ $? == 0 ]] (new syntax) or [ $? -eq 0 ] (old syntax)
  • if you use printf instead of echo, don't forget that you need to add \n at the end of the line manually
  • never use 1> $TMP_FILE 2> $TMP_FILE - this just overwrites stdout with stderr in a non-predictable manner. If you want to combine standard out and standard error, use: 1>$TMP_FILE 2>&1
  • by convention, ALL_CAPS names are used for/by environment variables. In-script variable names are recommended to be no_caps.
  • you don't need to use $? right after executing a command, it's redundant. Instead, you can directly run: if command; then ...

After fixing all that, your script would look like this:

for t in $tests_path1/cmd*.in; do
if diff "$t.out" <($parser_test <"$t" 2>&1); then
echo "$t ** TEST PASSED **"
else
echo "$t ** TEST FAILED **"
fi
done

If you don't care for the actual output of diff, you can add >/dev/null right after diff to silence it.

Third, if I understand correctly, your file names are of the form foo.in and foo.out, and not foo.in and foo.in.out (like the script above expects). If this is true, you need to change the diff line to this:

diff "${t/.in}.out" <($parser_test <"$t" 2>&1)

How to ignore output of diff in bash

Most probably the version of sh you are using doesn't understand the bash (deprecated/obsolete) extension &> that redirect both stdout and stderr at the same time. In posix shell the command &>/dev/null I think is parsed as { command & }; > /dev/null - it results in running the command in the background & and the > /dev/null part I think is ignored, as it just redirect output of a nonexistent command - it's valid syntax, but executes nothing. Because running the command in the background succeeds, the if always succeeds.

Prefer not to use &> - use >/dev/null 2>&1 instead. Use diff to pretty print the files comparison. Use cmp in batch scripts to compare files.

if cmp -s tmp ans.txt; then

How to get the output of git diff command in Shell Script

Since you are only interested in the cases "no diff", "diff", "error", I would run a

git diff --exit-code --quiet .....

--exit-code sets the exit code in the way the normal diff would do.

--quiet suppresses the output.

If the exit code is 0, you don't have differences.

If the exit code is 1, you have differences.

If the exit code is 2 or 128, you have fatal errors.

UPDATED As the OP pointed out in the comment, git-diff --exit-code produces status code 128, if the file to be compared does not exist. According to the man-page, it should produce the same exit code as the standard diff, which would be 2. Hence it is best to treat any exit code larger than 1 as standard error. This would also catch the case that git itself is not found (in which case the shell would likely report exit code 127).

unix diff side-to-side results?

From man diff, you can use -y to do side-by-side.

-y, --side-by-side
output in two columns

Hence, say:

diff -y /tmp/test1  /tmp/test2

Test

$ cat a                $ cat b
hello hello
my name my name
is me is you

Let's compare them:

$ diff -y a b
hello hello
my name my name
is me | is you


Related Topics



Leave a reply



Submit