Skip to main content
added 225 characters in body
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

A few points:

  • process substitution (<( ... )) and the extended test syntax [[ ... ]] is not available in sh. You need more advanced shells like bash or ksh for those.
  • $(( ... )) is arithmetic substitution, not command substitution. You need $( ... ) here.
  • Instead of checking the output of diff, you can check the exit status of diff directly in the if condition. You can discard the unused output. (So you don't even need command substitution or [[ ... ]] here.)

And suggestions from the comments:

  • You can avoid negating the condition in if by swapping the if and else blocks
  • You can use the just-as-efficient cmp -s command if all you want to do is check if the files are different.

Combined:

#!/bin/bash

if ! diffcmp -qs <(sort 1.txt) <(sort 2.txt) > /dev/null
then
    echo ERRORPASS
else
    echo PASSERROR
fi

A few points:

  • process substitution (<( ... )) and the extended test syntax [[ ... ]] is not available in sh. You need more advanced shells like bash or ksh for those.
  • $(( ... )) is arithmetic substitution, not command substitution. You need $( ... ) here.
  • Instead of checking the output of diff, you can check the exit status of diff directly in the if condition. You can discard the unused output. (So you don't even need command substitution or [[ ... ]] here.)

Combined:

#!/bin/bash

if ! diff -q <(sort 1.txt) <(sort 2.txt) > /dev/null
then
    echo ERROR
else
    echo PASS
fi

A few points:

  • process substitution (<( ... )) and the extended test syntax [[ ... ]] is not available in sh. You need more advanced shells like bash or ksh for those.
  • $(( ... )) is arithmetic substitution, not command substitution. You need $( ... ) here.
  • Instead of checking the output of diff, you can check the exit status of diff directly in the if condition. You can discard the unused output. (So you don't even need command substitution or [[ ... ]] here.)

And suggestions from the comments:

  • You can avoid negating the condition in if by swapping the if and else blocks
  • You can use the just-as-efficient cmp -s command if all you want to do is check if the files are different.

Combined:

#!/bin/bash

if cmp -s <(sort 1.txt) <(sort 2.txt)
then
    echo PASS
else
    echo ERROR
fi
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

A few points:

  • process substitution (<( ... )) and the extended test syntax [[ ... ]] is not available in sh. You need more advanced shells like bash or ksh for those.
  • $(( ... )) is arithmetic substitution, not command substitution. You need $( ... ) here.
  • Instead of checking the output of diff, you can check the exit status of diff directly in the if condition. You can discard the unused output. (So you don't even need command substitution or [[ ... ]] here.)

Combined:

#!/bin/bash

if ! diff -q <(sort 1.txt) <(sort 2.txt) > /dev/null
then
    echo ERROR
else
    echo PASS
fi