kimlew / bash-sort-images

Script that sorts photo files into year & month subdirectories, and if needed, day
0 stars 0 forks source link

Script breaks if you give it just one command-line argument #1

Closed kcharter closed 4 years ago

kcharter commented 4 years ago

I have found that if you give the directory as the first command-line argument, but don't give a second command line argument, the script won't move any files. In the code

https://github.com/kimlew/bash-sort-images/blob/16847406d5bedc500235cf93c761493c8a512b7d/sort_images.sh#L29-L32

you try to initialize variables from command-line arguments. You do this even if there is only one command-line argument, in which case you'll set day_subdir_also to an empty string.

Down in your loop, you check for an empty day_subdir_also, but if it's empty you never give a new value to new_dir_and_filename. The mv command at the bottom of the loop then fails for every file with a No such file or directory error message.

Just a suggestion: down in your loop you have an elaborate case statement that tests all the possible values for day_subdir_also. I suggest that you check for the all the possibilities outside your loop, and create a safe_day_subdir_also variable whose value is guaranteed to be either 'y' or 'n', and which you will use in your loop instead of day_subdir_also. The value should be

That way down in the loop the case statement on $safe_day_subdir_also will be simpler and you can remove the check for whether $day_subdir_also is empty.

kimlew commented 4 years ago

@kcharter As you suggested, I've added & moved code to address the 2nd parameter, day, when it is typed as command-line argument.

kcharter commented 4 years ago

OK, the new code in commit 230b5bfdbb767e52856e87f12a662e7ba412c139 looks good.

kcharter commented 4 years ago

Let me know if you would prefer me to close this or if you would like to.