That's certainly better. Good work!
There are a couple of things you should try to ingrain into your coding behavior from the outset, however.
1) Learn to always quote your variable expansions, especially when working with filenames. I know you set IFS to newline this time, so this script is safe from word-splitting, but you should still get into the habit of doing it by default. It's just that important.
Longer strings like this can and should be quoted as a single unit:
echo "Processing" $FILE
echo "Processing $FILE"
Environment variables are generally all upper-case, so it's a good idea to give your own script variables lowercase or mixed case names to differentiate them. Not really an important point, but it is a traditional practice.
Oh, and you really don't need to back-up the old IFS setting in a script unless you have a need to revert back to it later on.
2) If you haven't seen it yet, the last page of the Guide has a section on formatting and readability. Properly indenting and blocking your code makes for code that's more readable and easily debuggable.
Good commenting is important too. What your script is doing may seem obvious to you now, but I guarantee that in a few years you'll look back on some of your old code and wonder what the heck that
's supposed to be doing (and possibly what you were high on when you wrote it
). Learn to comment as you code.
And while not absolutely necessary, a lot of us think the "do/then
" keywords read better when placed on the same line as the "for/while/until/if
" keywords they're paired with.
So here's how I would do your main loop from above:
for file in *; do # I thought you wanted to use *.avi here?
echo "Processing $file"
echo "Creating Directory $dir"
mkdir -p "$dir"
echo "Moving $file into $dir"
mv -t "$dir" "$file"
echo "Processing next file..."
" has the side-effect of it not erroring if the dirname already exists, and explicitly defining the target directory with "mv -t
" is safer than depending on it simply using the last argument. Also, in this case the echo commands act pretty well as comments, so we don't really need to explain it any further. You could however just use the -v
option in the above commands instead, and skip the echos. )