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.
Just saying "it's not working" doesn't really explain your problem now, does it? Why not explain what it is doing that it shouldn't, and what it isn't doing that it should?
To start with, some general tips:
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 a year or so down the line).
Many people also think 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
The brackets on a function definition would also apply here.
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. You can learn about the exceptions later.
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
Now, as for this line...
Code:
sed -n '/WANT/,/END/p' zenon_start_release_rhel5_only.sh3 | awk '{print $1,$2,$3}' | cat >> ${ans}
To start from the end, the final
cat command is completely superfluous here. You can redirect the output directly into the file without it.
Second, there's usually no need to use both
sed (or
grep) and
awk together.
awk can do everything
sed can and more. Or it's likely in this case that even
sed can do it alone, depending on the exact nature of the file input and the desired output.
Third, it's probably a good idea to store the name of the input file in a variable too. File names and other external data points should usually not be hard-coded into a script.
Fourth, be sure to quote all variable substitutions, as mentioned. However, using the full
${var} form when you don't need it really only adds clutter to the script. Leave them off.
I believe this should do it for your first function, for example:
Code:
infile='zenon_start_release_rhel5_only.sh3'
catintofile() {
awk '/WANT/,/END/ { print $1,$2,$3 }' "$infile" >> "$ans"
}
Finally, I highly suggest setting up some kind of check on the input string to make sure you got a proper/desirable file name before actually trying to use it.