Ignore n no with bash >>

Asked

Viewed 73 times

1

Hello I have a PHP script that I run on a server, I put it in a cron. For security and maintenance reasons I write all the return of STDERR in a file, but even if the file does not return me anything it writes a line break " n" in the file how can I proceed? Below is the execution model I use to write the STDERR to the file.

*Obs would like it to store only if there was a different return of " n"

usradm01@prod-analytics:~$ php /home/usradm01/monitoringservers.php >> /tmp/monitoring_servers_error.log 2>&1

2 answers

1

Hmm can be done in many ways, but >> will always increment the file

you can try any of the possible commands:

php /home/usradm01/monitoringservers.php | tr -d '\n' 

Or

php /home/usradm01/monitoringservers.php | xargs echo -n

Or

echo -n  `php /home/usradm01/monitoringservers.php`

See if any of the previous commands right with >> case wouldn’t it be better to do a bash script something like:

saida=$(php /home/usradm01/monitoringservers.php | xargs echo -n)

if [[ -n $saida ]]; then
    echo $saida >> saida.txt
fi
  • So I need to increment it, because if there are problems anoite or other time would have this information, but I would like it to be incremented only if there is some content.

  • I was able to make use of php /home/usradm01/monitoringservers.php | sed "/ s*$/d" >> /tmp/err.log

  • But I’m going to give you a positive answer because you put me on the right path of research ;)

  • you can put your answer as right

  • How ?? I don’t know..

  • 1

    creates an answer and puts the solution

  • Thanks again!!

Show 2 more comments

0


I managed to solve,

I write the file and soon after use the sed command to filter it.. follows example below

php /home/usradm01/monitoringservers.php >> /tmp/err.log 2>&1 && sed -r "/^\s*$/d" /tmp/err.log > /tmp/err.log

Browser other questions tagged

You are not signed in. Login or sign up in order to post.