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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Murali
  • 1
  • 2
    Rather than figuring out how to wrap the error, why not just test if the file exists before trying to move it? – Kusalananda Sep 12 '17 at 20:27
  • 1
    You could wrap that particular `mv` command, as [roaima has](https://unix.stackexchange.com/a/391878/117549), 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. – Jeff Schaller Sep 12 '17 at 22:20

3 Answers3

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
roaima
  • 107,089
  • 14
  • 139
  • 261
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
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
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
glenn jackman
  • 84,176
  • 15
  • 116
  • 168