1)
Don't Read Lines With For
2)
Don't parse ls for filenames and metadata.
3)
QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
4)
$(..) is highly recommended over `..`
5) When using
bash or
ksh, it's recommended to use
[[..]] for string/file tests, and
((..)) for numerical tests. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression
6)
Useless Use Of Grep.
7) Since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them. You should also generally try to use variable names that clearly relate to their function in the script, but avoid ones that conflict with existing command names like "
file".
8)
Scripting With Style
Congratulations! I think this one post has made me post more of my canned responses at once than any other.
Start by correcting the worst of the above, particularly the first three, then come back if you still have problems.