Bash - STDOUT Append to Filename with $VAR has no Contents [VirtualBox VM Linux Guest of Win8 Host] [closed]

StackOverflow https://stackoverflow.com/questions/23144259

  •  05-07-2023
  •  | 
  •  

Question

When I put a $VAR in the STDOUT filename, it succeeds in creating the (non-existent) file but it fails in placing contents into it. Meanwhile, filenames without $VAR succeed.

#!/bin/bash

nodelist=$1

func_append_log() {
  log_file=$1
  echo "iterate $log_file" >> ./log/func.log
  echo "iterate $log_file" >> ./log/func.${log_file}.log
}

for node in `cat ${nodelist}` 
do 
  func_append_log $node 
  echo "iterate $node"     >> ./log/main.log
  echo "iterate $node"     >> ./log/main.${node}.log
done

As you can see, the files created with a variable in them are empty, while the non-variable filenames have contents. Any clues?

-rwxrwxrwx. 1 root root  377 Apr 17  2014 func.log*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.pl1.rcc.uottawa.ca*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.pl2.cs.montana.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab03.cs.washington.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab1.csee.usf.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab-2.cs.colostate.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab2.cs.ubc.ca*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab3.csee.usf.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab3.wail.wisc.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.planetlab4.wail.wisc.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 func.ricepl-1.cs.rice.edu*
-rwxrwxrwx. 1 root root  377 Apr 17  2014 main.log*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.pl1.rcc.uottawa.ca*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.pl2.cs.montana.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab03.cs.washington.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab1.csee.usf.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab-2.cs.colostate.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab2.cs.ubc.ca*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab3.csee.usf.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab3.wail.wisc.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.planetlab4.wail.wisc.edu*
-rwxrwxrwx. 1 root root    0 Apr 17  2014 main.ricepl-1.cs.rice.edu*

Thank you,

Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top