As mentioned, please use [code][/code]
tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Now for a few comments on the script in the OP. There are a couple of poorly-written points about it.
1) First, and foremost, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS
. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
2) Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
is used for interpreting scripts in posix-compliant lowest-common-denominator mode. This is mostly recommended for system startup scripts and other cases where portability and standardization are more important than convenience. When this isn't required, you should probably use #!/bin/bash
or another shell that has more modern, advanced features available, such as ksh or zsh.
The final two points assume that your shell is bash or ksh.
4) It's recommended to use ((..))
for numerical tests, and [[..]]
for string tests and other complex expressions. Don't use the old [..]/test
unless you specifically need POSIX-style portability.
5) brace expansion
can be used to generate a sequence of numbers. You can also use a c-style for loop
Here are a few useful bash scripting references:
I particularly recommend the first one for general concepts, and the two following it for overcoming problems with your code.