Hello,
I am working through a test Linux script from a website, and am running into some issues. The point of the script is to find image files throughout the system, copy them to one centralized directory,ignore duplicate images with the same name, and output a text file of all the images copied.
The script is below.
#!/bin/sh
if [ ! -d $1 ] ; then
echo "Exiting due to non-existing $1"
exit #Check if source drive exists#
#
fi
if [ ! -d $2 ] ; then mkdir $2 #checking if destination directory exists #
else echo -e "Usage::# phar image_path archive_path" #outputs helpful error message #
find $1 -name "IMG_*.JPG" # find files #
fi
for i in $1; do #finds duplicate files and copies the non duplicate files.
find $i -type f -name 'IMG_*.jpg' -exec cp {} $2 \;
for j in $2; do
find $2 -type f -name $i | while read filename; do if test "$i"="$j"; then mv $i new_filename; fi done
done
done
#then
# mv -f "IMG_*.JPG.JPG"
#
# else if diff -q $file1 $file2 ; then echo "files are the same" ; exit # Checks if two files are the same.#
# fi
# output a list of the files copied and moved
# readlink -f.-name"iMG".JPG>files_moved.txt
The usage message comes if "$2" is passed (See 3.)
Anyway, the user passes a target dir, sees the usage message, and a list of files found in $1 - that list is not -- cannot be -- used -- later on again, --> unrequired/obsolete.
In addition:
2. Use more (often) quotes around variables (imagine file names with spaces, though not wanted, but might happen anyway.
3. [ -d "$1" ] Checks if $1 is a directory [ -z "$1" ] Checks if an argument has been passed at all
If no argument has been passed, say so, dont use the (unescaped) variable-name, as that part will be empty if no argument has been passed.
Likewise for the destination.
Example:
Place the results of find into variables (lists), as that could help to remove duplicates.
Some of the find commands, look (to my eyes) unused...
Last but not least, since not really relevant, try to stick to one coding style, i see like 3 or even 4 different styles.
Makes the reading easier (specialy without syntax highlighting).
But maybe you just need to figure your coding style first, so no worries.
And to repeat RudiC, since i dont feel that has been provided yet...
Elaborate, dont just say what you want, but what is, what you have tried to fix, where you stuck?