rezizter rezizter - 1 month ago 3
Linux Question

Pass a large variable into the diff command via bash

I am writing a script which does a checksum (md5sum) on a forum web directory.

It is a bash script. With the idea being to do a checksum on all the files in the directory, and then compare it to a text file which has a list of checksums.

The script works if I pass it into a text file, and then do a diff command between the text file and my list of known checksums, but I would like to not have it write to a text file and then have to remove the text file at the end of the script, hence why I am using a variable

The script below fails with the error:
/usr/bin/diff: Argument list too long

cd /var/www/html/forum/

VAR1=$(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change")

/usr/bin/diff "${VAR1}" "/root/scripts/forum_checkum_original.txt"

How can I pass my variable along so that I can runn the diff command on it?

EDIT: with the help of the user devnull (thank you again) here is the completed and working script:

cd /var/www/html/forum/

MAIL=$(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files\|that\|change") /root/scripts/forum_checkum_original.txt)

if [[ -n $(/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "files that change") /root/scripts/forum_checkum_original.txt) ]]; then
echo "$MAIL" | mail -s "Forum Checksum"
echo "no files have been changed"


diff compares files, not variables. Use Process Substitution instead.

An equivalent of what you're trying to do would be:

/usr/bin/diff <(find . -type d \( -name store_sitemap \) -prune -o -type f -exec md5sum {} \; | grep -v "bidorbuy.log") /root/scripts/forum_checkum_original.txt