kevin
kevin

Reputation: 33

bash script containing function doesn't do anything at all when run

I'd like to remove some duplicate files from a directory. In a directory in my $PATH, I've written a script remove_duplicates containing the following:

$:~/local/bin cat remove_duplicates
#!/bin/bash

remove_duplicates() {
  for i in *
  do
#  echo ${i}
    if [ -e ${i}.~1~ ]
    then
      hash1=$(md5sum ${i} | awk '{print $1}')
      hash2=$(md5sum ${i}.~1~ | awk '{print $1}')
      if [ "$hash1" = "$hash2" ]
      then
        rm -v ${i}.~1~
      fi
    fi
  done
}

When I execute the script in a directory containing a duplicated file, it doesn't delete the duplicate.

$:~/.vim/colors md5sum candycode*
8b115e7b2ed03eb949c9c0b2cf049012  candycode.vim
8b115e7b2ed03eb949c9c0b2cf049012  candycode.vim.~1~

$:~/.vim/colors which remove_duplicates
~/local/bin/remove_duplicates

$:~/.vim/colors remove_duplicates

$:~/.vim/colors md5sum candycode*
8b115e7b2ed03eb949c9c0b2cf049012  candycode.vim
8b115e7b2ed03eb949c9c0b2cf049012  candycode.vim.~1~

I think the problem might be in the expansion of the * in my for loop, but I'm not sure how to get it to expand correctly (i.e. expand to include all files in the directory from which the script is called).
The reason that I think this is where the problem lies is that when I uncomment the echo ${i} line in the script, I don't see anything echoed to stdout.
I've also tried to replace * with $PWD/*, but this doesn't seem to do anything.

Upvotes: 0

Views: 137

Answers (1)

James K
James K

Reputation: 617

You're defining a function, but not running it.

That is, when you run this script, it goes through and executes it, line by line. What this accomplishes is defining a function called remove_duplicates.. then it reaches the end of the file and is done.

You can either add a line at the end which calls remove_duplicates, or you can just remove the declaration that makes it a function in the first place:

#!/bin/bash

  for i in *
  do
#  echo "${i}"
    if [ -e "${i}.~1~" ]
    then
      hash1=$(md5sum "${i}" | awk '{print $1}')
      hash2=$(md5sum "${i}.~1~" | awk '{print $1}')
      if [ "$hash1" = "$hash2" ]
      then
        rm -v "${i}.~1~"
      fi
    fi
  done

Upvotes: 2

Related Questions