@lithos:
Seems a bit overly-complex and wasteful to me; it does exactly the same thing as c-style
for loop, only less cleanly and concisely.
Also, "
$[..]" is non-standard and deprecated. Use "
((..))" instead, or "
$((..))" if you need posix compatibility. You can also use the
let keyword.
Code:
(( i++ )) #bash or ksh form; uses ++ for simple value incrementing.
: $(( i++ )) #posix compatible, prefixing it with the ":" (true) command keeps it from producing output.
i=$(( i + 1 )) #using the original long syntax
let i++
let i=i+1
And if I may add a few more scripting points:
1)
Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be so a year or so down the line).
Many people also feel that it's more readable to place the "
do/then" keywords on the same line as the "
for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.
Code:
for var in fee fai foo fum ; do
if [[ "$var" == "foo" ]]; then
echo "Found 'foo'."
fi
done
2)
When using
bash or
ksh, it's recommended to use
((..)) for numerical tests, and
[[..]] for string/file tests and other complex expressions. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
3)
QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS! You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
4)
Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
It would probably also be more efficient to save the first
grep output into an array first, so that you only have to run it once. Then you could check that as needed for the values you want. I don't know what the contents of the
$CHECKfile looks like, though, or else I could post an updated version.