0

I am executing the command

mv /prod/new/outputlog/error.log /prod/hist/new/outputlog/error.log.32423423424 

using shell script.

Since the error.log file is not available the command writing an error message in to the log file automatically. The error message mv: cannot rename /prod/new/outputlog/error.log /prod/hist/new/outputlog/error.log.32423423424 Not wrap in to two lines.

Since it is a system-generated error message I am unable to control the length of the error message for each line. I want the system to wrap the message after it reaches length 80.

2
  • 2
    Rather than figuring out how to wrap the error, why not just test if the file exists before trying to move it? Commented Sep 12, 2017 at 20:27
  • 1
    You could wrap that particular mv command, as roaima has, but it sounds like you might want to wrap any and all (error?) messages for the log file, in which case we'd need to see how you are sending output to that log file. Commented Sep 12, 2017 at 22:20

3 Answers 3

2

You could protect the mv attempt by ensuring the source file exists before you try to rename it:

test -f /prod/new/outputlog/error.log && mv /prod/new/outputlog/error.log /prod/hist/new/outputlog/error.log.32423423424 

Or you can catch the error message and attempt to split it over two lines:

mv /prod/new/outputlog/error.log /prod/hist/new/outputlog/error.log.32423423424 2>&1 | fmt -s -w80 >&2 
1

Instead of corrupting the error message, just test on the file you're moving before moving it:

if [ -f /prod/new/outputlog/error.log ]; then mv /prod/new/outputlog/error.log \ /prod/hist/new/outputlog/error.log.32423423424 fi 

or using short-cut logic:

[ -f /prod/new/outputlog/error.log ] && mv /prod/new/outputlog/error.log \ /prod/hist/new/outputlog/error.log.32423423424 

If the absence of the log file is an issue that needs to be reported, do that separately:

if [ -f /prod/new/outputlog/error.log ]; then mv /prod/new/outputlog/error.log \ /prod/hist/new/outputlog/error.log.32423423424 else echo 'ERROR: /prod/new/outputlog/error.log is missing' >&2 fi 
0

Instead of "corrupting" the error.log, why don't you truncate it when you want to view it:

cut -c 1-80 error.log | less 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.