Code:
cat ${input_config_file}|while IFS="," read rec1 _ _ _; do echo $rec1 ; done
^ ^ ^ ^ ^
1 2 3 4 5
1)
Useless Use Of Cat. The file can be directly fed into the
while loop. Saves one more process.
2) The full bracketed form of a variable is unnecessary unless you're doing a
parameter substitution or you need to concatenate it with another plain text string. In all other cases it does nothing but clutter up the code, which can even be detrimental by obscuring syntax mistakes. Leave them off when unneeded.
3) When
read is given multiple variable names, the input line is split according to the current IFS value and one word is placed into each. Leading and trailing whitespace is removed if IFS contains that character (this happens even with a single variable). If there are more variables than words, the extras remain empty, and if there are more words than variables the last one contains all the excess. This means you only need one instance of it to capture all the extra unwanted text on the line.
You should also generally use the "
-r" option with
read, to disable interpretation of backslashes in the input text.
4) "
_" is a special variable name in most shells. It generally contains the last argument of the most recently run command, and so is overwritten after every execution. This makes it perfect for use as a disposable variable.
And as mentioned, even if you used a regular variable, the amount of extra overhead is so small that it's usually better to use
read to split than an external command.
5)
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 any 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 when you need them.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
So the correct form of this should be:
Code:
while IFS="," read -r rec1 _; do echo "$rec1" ; done <"$input_config_file"