prabhakar
prabhakar

Reputation: 55

Writing to file shell script

I have been to trying to write some output to a CSV file using the method below in a shell script:

writeToResultFile()
{   
     resultFile="ShakeoutResult.csv"
     msg=" $*"
     echo "writing to resultFile..$msg"
     echo $msg >> $resultFile
}

When I tried to call this method:

writeToResultFile "column1,column2,column3"

It works fine and was written to output file. But when I tried to call this method from another method such as:

doProcess()
{ 
  writeToResultFile "data1,data2,data3"
}

Nothing is written to the output file. Stepping through, I know that writeToResultFile() is getting invoked and the param also is echoed in the console, but not getting appended to the output file.

Upvotes: 0

Views: 3195

Answers (1)

Gergely Bacso
Gergely Bacso

Reputation: 14651

Just to make sure: what do you use? Bash? Because it's working:

#!/bin/bash
writeToResultFile() {
    msg=" $*"
    echo "messaage: $msg"
    echo $msg >> output.txt
}
doProcess()
{ 
  writeToResultFile "function1,function2,function3"
}
writeToResultFile "main1,main2,main3"
doProcess

The output will be (cat output.txt):

main1,main2,main3
function1,function2,function3

Upvotes: 2

Related Questions