0

Firstly,i will give the shell code:

#!/bin/bash
filename=$1
if [ -e $filename ] ; then
    yesterday=`date -d yesterday +%Y%m%d`

    cp $filename $filename.$yesterday
    now=`date '+%Y-%m-%d%H:%M:%S'`
    echo "========split log at $now========" > $filename
    echo "========split log $filename to $filename.$yesterday  at $now========"
else
    echo "$filename not exist."
fi

The shell run successfully,and print the string ========split log at $now======== to the new created $filename.But below this string,many bytes of \0 are also written to the$filename,which is showed as follows:

My reputation score is less than 10,i can not post image,so i give the link of the picture:http://i.stack.imgur.com/QF0F2.jpg

4
  • Two pairs of 0x0D 0x0A look very strange.
    – nullptr
    Jan 28, 2015 at 2:02
  • There was a command of echo "" >> $filename after echo "========split log at $now========" > $filename in my original code,so you can see 0x0d and 0x0a.
    – yunnysunny
    Jan 28, 2015 at 2:46
  • In Linux, I would expect to see only 0x0A (no 0x0D). Which Linux do you use? Also, is the file local?
    – nullptr
    Jan 28, 2015 at 20:47
  • 0x0d is not the key point,i have changed the picturue,which has none 0x0d
    – yunnysunny
    Jan 30, 2015 at 3:13

1 Answer 1

1

i wrote the shell code aimed to truncate the log file created by nohup. The original of my start command like this : nohup $cmd > $logPath 2>&1 &, now i change it to nohup $cmd >> $logPath 2>&1 &.Someone told me that when use the mode of > the log writer program would remember the location of current log, and after truncating the log,the program will continue the location.

2
  • I think you've misunderstood the OP's issue. The OP is not using > after truncating the file, (s)he is using > to truncate the file (and write to it).
    – ruakh
    Jan 30, 2015 at 3:52
  • Wait, never mind, I see what you're saying. The problem is not in the script the OP posted, but in the process that's actually writing to these files. (Of course, we don't know for sure that that process is just using a Bash redirect, but it's quite possible.) +1
    – ruakh
    Jan 30, 2015 at 4:03

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.