Your question doesn't have enough information to reproduce the problem -- which is to say, running the program exactly as you give it (after producing a reasonable nodelist
) works perfectly. In the future, you should check that your cut-down test cases actually reproduce the bug you intend them to demonstrate. :)
Well, "perfectly". You have a bunch of bugs around word splitting -- node names with wildcard characters or characters in IFS
would result in unexpected behavior. There are also a bunch of inefficiencies -- places where you reopen the same file more than once unnecessarily, which creates substantial overhead.
The below fixes these bugs -- opening func.log
and main.log
exactly once each, rather than reopening them every time you want to write another line, and working with unusual node names or IFS values.
#!/bin/bash
exec 4>./log/func.log ## open this file only once
exec 5>./log/main.log ## this one too
nodelist=$1
func_append_log() {
echo "iterate $log_file" >&4 ## FD previously opened as func.log
echo "iterate $log_file" >>"./log/func.${log_file}.log"
}
while read -r node; do
func_append_log "$node"
echo "iterate $node" >&5 ## FD previously opened as main.log
echo "iterate $node" >>"./log/main.${node}.log"
done <nodelist
References:
- BashFAQ #1 - How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
- Don't Read Lines With For