1

I have a very simple ksh script and at certain points I want to write to a log file. I use the following commands in two places...

print "Directory listing 1:\n" > ${LogFile}
ll >> ${LogFile}

(Note: The second time this command is used print Directory listing 2)

My problem is, when I view the log file afterwards, only the second execution of these commands work! So there's no "Directory listing 1" and accompanying "ll" output.

I have tested and tested the script to ensure that there's nothing wrong my logic. I've added print test commands just before each so I know they get executed.

Is there something I've done wrong or I'm not realising?

  • Not a bug. You're telling it to start LogFile from scratch, no wonder it has no "Directory listing 1". – njsg Jan 25 '13 at 12:59
3

Whenever you do a redirection with > (your first line), the ${LogFile} is truncated to 0 and then written. If I understand right, you do the above twice, the first stuff is overwritten by the second.

What you have to do is along the lines:

> ${LogFile}    # This just truncates if there was anything there, writes nothing
 ...
echo "First round" >> ${LogFile}
ls -l >> ${LogFile}
 ...
echo -e "\nSecond round" >> ${LogFile}
ls -l >> ${LogFile}
 ...
  • That's exactly what my issue was - thanks for your help! – Rich Jan 25 '13 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.