Carriage return

Solved
enyrix Posted messages 167 Status Member -  
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   -
Hello,
I am programming an email that will contain the return of a command executed on a server. I am storing the return of my command in a variable, but I would like to preserve the carriage returns contained in my log file... How can I do that?

MSG=$?(tail -n 20 /var/log/apache2/error.log) DATA="Here is the content of the log file:\r\n\r\n$MSG"

6 answers

dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
hello
put between "
MSG="$?(tail -n 20 /var/log/apache2/error.log)" .. echo "$MSG"
1
enyrix Posted messages 167 Status Member 8
 
Does not work, it directly puts the line in text... I'm using it for an email, I want the content of my tail in there as text... it works but it doesn't include line breaks for the log file content, it sends me the content of the log file all at once, which is not very readable... Is there another way, like: command < filename to import ....? Thank you for your valuable help!
0
Char Snipeur Posted messages 10112 Registration date   Status Contributor Last intervention   1 331
 
Hello.
Try the unix2dos command, which, if it's not installed by default, should be in the official packages.
0
enyrix Posted messages 167 Status Member 8
 
Resolved! Find a solution and improve the script with the help of a programmer colleague!

 #!/bin/bash SUBJECT="Server Notification" MAILSERVER="relais.xxxx.xx" PORT="25" MAILFROM="xxxx@xxxx" MAILTO="xxxx@xxxx" #### SEND MAIL via RAW TCP ####### echo echo "Connecting to $MAILSERVER on Port $PORT"; echo "Please wait ... " echo exec 3<>/dev/tcp/$MAILSERVER/$PORT if [ $? -ne 0 ] ; then echo echo "ERROR: Cannot connect to the Mail Server"; echo "Please check the servername and/or the port number" exit else echo -en "HELO mail.email.com\r\n" >&3 echo -en "MAIL FROM:$MAILFROM\r\n" >&3 echo -en "RCPT TO:$MAILTO\r\n" >&3 echo -en "DATA\r\n" >&3 echo -en "Subject: $SUBJECT\r\n\r\n" >&3 echo -en "Here is the content of the log file:\r\n\r\n" >&3 tail -v -n 20 /var/log/apache2/error.log | while read line do echo -en "$line\r\n" >&3 done echo -en ".\r\n" >&3 echo -en "QUIT\r\n" >&3 cat <&3 echo echo echo "Mail sent!" echo fi 


Thank you for your help! Just so you know, unix2dos is now tofrodos for Ubuntu...
1
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
avec des " ça fonctionne, montre ton code
$ tail a2 aaa aaa bbb b b b ccc ccc $ MSG="$(tail a2)" $ echo $MSG aaa aaa bbb b b b ccc ccc $ echo "$MSG" aaa aaa bbb b b b ccc ccc $ $ DATA="Voici le contenu du fichier log:\r\n\r\n""$MSG" $ echo -e "$DATA" Voici le contenu du fichier log: aaa aaa bbb b b b ccc ccc $
0
enyrix Posted messages 167 Status Member 8
 
Voici le contenu du fichier log:

 #!/bin/bash MSG="$(tail -n 20 /var/log/apache2/error.log)" DATA= echo "Here is the log file content:\r\n\r\n$MSG" SUBJECT="Server Notification" MAILSERVER="XXXXXXXX" PORT="25" MAILFROM="XXXXXXXXX" MAILTO="XXXXXXXXXXX" #### SEND MAIL via RAW TCP ####### echo echo "Connecting to $MAILSERVER on Port $PORT"; echo "Please wait ... " echo exec 3<>/dev/tcp/$MAILSERVER/$PORT if [ $? -ne 0 ] ; then echo echo "ERROR: Cannot connect to the Mail Server"; echo "Please check the servername and/or the port number" exit fi echo -en "HELO mail.email.com\r\n" >&3 echo -en "MAIL FROM:$MAILFROM\r\n" >&3 echo -en "RCPT TO:$MAILTO\r\n" >&3 echo -en "DATA\r\n" >&3 echo -en "Subject: $SUBJECT\r\n\r\n" >&3 echo -en "$DATA\r\n" >&3 echo -en ".\r\n" >&3 echo -en "QUIT\r\n" >&3 cat<&3 echo echo echo "Mail sended!" echo 
0
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
I modified this line
DATA="Here is the content of the log file:\r\n\r\n""$MSG"
0
enyrix Posted messages 167 Status Member 8
 
When I use echo DATA= echo, there is nothing in my email and it only displays in the console...
0
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
no echo in DATA=
0
enyrix Posted messages 167 Status Member 8
 
No more! :(
MSG=$(tail -n 20 /var/log/apache2/error.log) DATA="Here is the content of the log file:\r\n\r\n"$MSG
0
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
en enlevant les >3, l'affichage est correct ?
0
enyrix Posted messages 167 Status Member 8
 
Not really... it doesn't send the email anymore, it gives an error:
500 5.5.1 Unknown command "Here is the content of the log file:" specified 500 5.5.1 Unknown command "" specified


I'm trying other ways that don't work either lol...
0
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
 
if it's the \r that's missing, and like this?
tail -v -n 20 /var/log/apache2/error.log | sed 's/$/\r/' >&3
0